T
- The type contained on the Try withinpublic class TryT<T,X extends java.lang.Throwable>
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
MaybeT<T> |
filter(java.util.function.Predicate<? super T> test)
Filter the wrapped Try
|
<B> TryT<B,X> |
flatMap(java.util.function.Function<? super T,TryT<B,X>> f)
Flat Map the wrapped Try
|
static <A,X extends java.lang.Throwable> |
fromAnyM(AnyM<A> anyM)
|
static <U,R,X extends java.lang.Throwable> |
lift(java.util.function.Function<? super U,? extends R> fn)
Lift a function into one that accepts and returns an TryT
This allows multiple monad types to add functionality to existing functions and methods
e.g.
|
static <U1,U2,R,X extends java.lang.Throwable> |
lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
Lift a BiFunction into one that accepts and returns TryTs
This allows multiple monad types to add functionality to existing functions and methods
e.g.
|
<B> TryT<B,X> |
map(java.util.function.Function<? super T,? extends B> f)
Map the wrapped Try
|
static <A,X extends java.lang.Throwable> |
of(AnyM<Try<A,X>> monads)
Construct an TryT from an AnyM that wraps a monad containing Trys
|
TryT<T,X> |
peek(java.util.function.Consumer<? super T> peek)
Peek at the current value of the Try
|
java.lang.String |
toString() |
AnyM<Try<T,X>> |
unwrap() |
public TryT<T,X> peek(java.util.function.Consumer<? super T> peek)
TryT.of(AnyM.fromStream(Success.of(10))
.peek(System.out::println);
//prints 10
peek
- Consumer to accept current value of Trypublic MaybeT<T> filter(java.util.function.Predicate<? super T> test)
TryT.of(AnyM.fromStream(Success.of(10))
.filter(t->t!=10);
//TryT<AnyM<Stream<Optional.empty>>>
test
- Predicate to filter the wrapped Trypublic <B> TryT<B,X> map(java.util.function.Function<? super T,? extends B> f)
TryT.of(AnyM.fromStream(Success.of(10))
.map(t->t=t+1);
//TryT<AnyM<Stream<Success[11]>>>
f
- Mapping function for the wrapped Trypublic <B> TryT<B,X> flatMap(java.util.function.Function<? super T,TryT<B,X>> f)
TryT.of(AnyM.fromStream(Success.of(10))
.flatMap(t->Failure.of(new Exception());
//TryT<AnyM<Stream<Failure[Excption]>>>
f
- FlatMap functionpublic static <U,R,X extends java.lang.Throwable> java.util.function.Function<TryT<U,X>,TryT<R,X>> lift(java.util.function.Function<? super U,? extends R> fn)
Function<Integer,Integer> add2 = i -> i+2;
Function<TryT<Integer,RuntimeException>, TryT<Integer,RuntimeException>> optTAdd2 = TryT.lift(add2);
Stream<Integer> withNulls = Stream.of(1,2,null);
AnyM<Integer> stream = AnyM.ofMonad(withNulls);
AnyM<Try<Integer,RuntimeException>> streamOpt = stream.map(this::toTry);
List<Integer> results = optTAdd2.apply(TryT.of(streamOpt))
.unwrap()
.<Stream<Try<Integer,RuntimeException>>>unwrap()
.filter(Try::isSuccess)
.map(Try::get)
.collect(Collectors.toList());
//Arrays.asList(3,4);
fn
- Function to enhance with functionality from Try and another monad typepublic static <U1,U2,R,X extends java.lang.Throwable> java.util.function.BiFunction<TryT<U1,X>,TryT<U2,X>,TryT<R,X>> lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
BiFunction<Integer,Integer,Integer> add = (a,b) -> a+b;
BiFunction<TryT<Integer,RuntimeException>,TryT<Integer,RuntimeException>, TryT<Integer,RuntimeException>> optTAdd2 = TryT.lift2(add);
Stream<Integer> withNulls = Stream.of(1,2,null);
AnyM<Integer> stream = AnyM.ofMonad(withNulls);
AnyM<Try<Integer,RuntimeException>> streamOpt = stream.map(this::toTry);
CompletableFuture<Try<Integer,RuntimeException>> two = CompletableFuture.completedFuture(Try.of(2));
AnyM<Try<Integer,RuntimeException>> future= AnyM.ofMonad(two);
List<Integer> results = optTAdd2.apply(TryT.of(streamOpt),TryT.of(future))
.unwrap()
.<Stream<Try<Integer,RuntimeException>>>unwrap()
.filter(Try::isSuccess)
.map(Try::get)
.collect(Collectors.toList());
//Arrays.asList(3,4);
fn
- BiFunction to enhance with functionality from Try and another monad typepublic static <A,X extends java.lang.Throwable> TryT<A,X> fromAnyM(AnyM<A> anyM)
anyM
- AnyM that doesn't contain a monad wrapping an Trypublic static <A,X extends java.lang.Throwable> TryT<A,X> of(AnyM<Try<A,X>> monads)
monads
- AnyM that contains a monad wrapping an Trypublic java.lang.String toString()
toString
in class java.lang.Object