T
- Return type (success)X
- Base Error typepublic interface Try<T,X extends java.lang.Throwable> extends To<Try<T,X>>, MonadicValue<T>
Try.success("return-value");
Create a failure value
Try.failure(new MyException("error details"));
Exceute methods that may throw exceptions
Non-void methods
Try.withCatch(()-> exceptional2())
.map(i->i+" woo!")
.onFail(System.out::println)
.orElse("default");
//"hello world woo!"
private String exceptional2() throws RuntimeException{
return "hello world";
}
Void methods
//Only catch IOExceptions
Try.runWithCatch(this::exceptional,IOException.class)
.onFail(System.err::println);
private void exceptional() throws IOException{
throw new IOException();
}
Try with resources
Try.catchExceptions(FileNotFoundException.class,IOException.class)
.init(()->new BufferedReader(new FileReader("file.txt")))
.tryWithResources(this::read)
.map(this::processData)
.recover(e->"default);
By default Try does not catch exception within it's operators such as map / flatMap, to catch Exceptions in ongoing operations use @see of(Object, Class...)
Try.of(2, RuntimeException.class)
.map(i->{throw new RuntimeException();});
//Failure[RuntimeException]
Modifier and Type | Interface and Description |
---|---|
static interface |
Try.AndFinally<T,V,X extends java.lang.Throwable> |
static interface |
Try.CheckedConsumer<T,X extends java.lang.Throwable> |
static interface |
Try.CheckedFunction<T,R,X extends java.lang.Throwable> |
static interface |
Try.CheckedRunnable<X extends java.lang.Throwable> |
static interface |
Try.CheckedSupplier<T,X extends java.lang.Throwable> |
static class |
Try.Failure<T,X extends java.lang.Throwable>
Class that represents the Failure of a Try
|
static interface |
Try.Init<X extends java.lang.Throwable> |
static class |
Try.MyFinallyBlock<T,V,X extends java.lang.Throwable> |
static class |
Try.MyInit<X extends java.lang.Throwable> |
static class |
Try.MyTryCatch<V,X extends java.lang.Throwable> |
static class |
Try.Success<T,X extends java.lang.Throwable>
Class that represents a Successful Try
|
static interface |
Try.TryCatch<V,X extends java.lang.Throwable> |
Value.ValueImpl<T>
Convertable.SupplierToConvertable<T>
Fn0.FunctionalOperations<R>, Fn0.Instances, Fn0.SupplierKind<R>
Modifier and Type | Method and Description |
---|---|
default AnyMValue<Witness.tryType,T> |
anyM() |
default <U> Try<U,X> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a reactiveStream to a given type, possibly throwing a
ClassCastException . |
static <X extends java.lang.Throwable> |
catchExceptions(java.lang.Class<? extends X>... classes)
Fluent step builder for Try / Catch / Finally and Try with resources equivalents.
|
default <R> Try<R,X> |
coflatMap(java.util.function.Function<? super MonadicValue<T>,R> mapper)
Perform a coflatMap operation.
|
default Try<T,X> |
combine(Monoid<T> monoid,
Try<? extends T,X> v2)
Combine this Try with another using the supplied Monoid as a combiner
|
default <T2,R> Try<R,X> |
combine(Value<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Lazily combine this ApplicativeFunctor with the supplied value via the supplied BiFunction
Example
|
static <T,X extends java.lang.Throwable> |
failure(X error)
Construct a Failure instance from a throwable (an implementation of Try)
|
X |
failureGet() |
Maybe<T> |
filter(java.util.function.Predicate<? super T> p)
Keep only elements for which the supplied predicates hold
e.g.
|
default Maybe<T> |
filterNot(java.util.function.Predicate<? super T> fn)
Remove any elements for which the predicate holds (inverse operation to filter)
e.g.
|
<R> Try<R,X> |
flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends R>> fn)
A flattening transformation operation (@see
Optional.flatMap(Function) |
default <R> Try<R,X> |
flatMapS(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> mapper) |
static <T,X extends java.lang.Throwable> |
flatten(Try<? extends Try<T,X>,X> nested) |
void |
forEach(java.util.function.Consumer<? super T> consumer) |
default <R1,R> Try<R,X> |
forEach2(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
Perform a two level nested internal iteration over this MonadicValue and the
supplied MonadicValue
|
default <R1,R> Try<R,X> |
forEach2(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,java.lang.Boolean> filterFunction,
java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
Perform a two level nested internal iteration over this MonadicValue and the
supplied reactiveStream
|
default <T2,R1,R2,R> |
forEach3(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2,
Fn3<? super T,? super R1,? super R2,? extends R> yieldingFunction)
Perform a three level nested internal iteration over this MonadicValue and the
supplied MonadicValues
|
default <T2,R1,R2,R> |
forEach3(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2,
Fn3<? super T,? super R1,? super R2,java.lang.Boolean> filterFunction,
Fn3<? super T,? super R1,? super R2,? extends R> yieldingFunction)
Perform a three level nested internal iteration over this MonadicValue and the
supplied MonadicValues
|
default <T2,R1,R2,R3,R> |
forEach4(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2,
Fn3<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3,
Fn4<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
Perform a four level nested internal iteration over this MonadicValue and the
supplied MonadicValues
|
default <T2,R1,R2,R3,R> |
forEach4(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2,
Fn3<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3,
Fn4<? super T,? super R1,? super R2,? super R3,java.lang.Boolean> filterFunction,
Fn4<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
Perform a four level nested internal iteration over this MonadicValue and the
supplied MonadicValues
Maybe.of(3)
.forEach4(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->Maybe.none(),
(a,b,c,d)->a+b+c<100,
(a,b,c,d)->a+b+c+d);
//Maybe.none
|
void |
forEachFailed(java.util.function.Consumer<? super X> consumer) |
static <T,X extends java.lang.Throwable> |
fromIterable(java.lang.Iterable<T> iterable)
Construct a Try that contains a single value extracted from the supplied Iterable
|
static <T> Try<T,java.lang.Throwable> |
fromPublisher(org.reactivestreams.Publisher<T> pub)
Construct a Try that contains a single value extracted from the supplied reactive-streams Publisher
|
static <T,X extends java.lang.Throwable> |
fromPublisher(org.reactivestreams.Publisher<T> pub,
java.lang.Class<X>... classes)
Construct a Try that contains a single value extracted from the supplied reactive-streams Publisher, will catch any Exceptions
of the provided types
|
T |
get()
Apply this function to the arguments.
|
boolean |
isFailure() |
default boolean |
isPresent() |
boolean |
isSuccess() |
default java.util.Iterator<T> |
iterator() |
<R> Try<R,X> |
map(java.util.function.Function<? super T,? extends R> fn)
Transform this functor using the supplied transformation function
|
default Try<MonadicValue<T>,X> |
nest()
cojoin pattern.
|
default Maybe<T> |
notNull()
Filter elements retaining only values which are not null
|
static <T,X extends java.lang.Throwable> |
of(T value,
java.lang.Class<? extends java.lang.Throwable>... classes)
Return a Try that will catch specified exceptions when map / flatMap called
For use with liftM / liftM2 and For Comprehensions (when Try is at the top level)
|
default <U> Maybe<U> |
ofType(java.lang.Class<? extends U> type)
Keep only those elements in a reactiveStream that are of a given type.
|
Try<T,X> |
onFail(java.lang.Class<? extends X> t,
java.util.function.Consumer<X> consumer) |
Try<T,X> |
onFail(java.util.function.Consumer<? super X> consumer) |
T |
orElse(T value)
Get the contained value or else the provided alternative
|
T |
orElseGet(java.util.function.Supplier<? extends T> value)
An equivalent operation to
Optional.orElseGet(Supplier)
Returns a value generated by the provided Supplier if this Convertable is empty or
contains a null value. |
default Try<T,X> |
peek(java.util.function.Consumer<? super T> consumer)
Peek at the current value of this Transformable, without transforming it
|
default Try<T,X> |
peekFailed(java.util.function.Consumer<? super X> consumer) |
Try<T,X> |
recover(java.util.function.Function<? super X,? extends T> fn) |
Try<T,X> |
recoverFor(java.lang.Class<? extends X> t,
java.util.function.Function<? super X,? extends T> fn)
Recover if exception is of specified type
|
Try<T,X> |
recoverWith(java.util.function.Function<? super X,? extends Try<T,X>> fn)
flatMap recovery
|
Try<T,X> |
recoverWithFor(java.lang.Class<? extends X> t,
java.util.function.Function<? super X,? extends Try.Success<T,X>> fn)
FlatMap recovery function if exception is of specified type
|
default <R> Try<R,X> |
retry(java.util.function.Function<? super T,? extends R> fn)
Retry a transformation if it fails.
|
default <R> Try<R,X> |
retry(java.util.function.Function<? super T,? extends R> fn,
int retries,
long delay,
java.util.concurrent.TimeUnit timeUnit)
Retry a transformation if it fails.
|
static <X extends java.lang.Throwable> |
runWithCatch(Try.CheckedRunnable<X> cf,
java.lang.Class<? extends X>... classes)
Try to execute supplied Runnable and will Catch specified Excpetions or java.lang.Exception
if none specified.
|
ReactiveSeq<T> |
stream() |
static <T,X extends java.lang.Throwable> |
success(T value)
Construct a Success instance (an implementation of Try)
|
void |
throwException()
Throw exception if Failure, do nothing if success
|
java.util.Optional<X> |
toFailedOptional() |
java.util.stream.Stream<X> |
toFailedStream() |
Ior<X,T> |
toIor()
Return an Ior that can be this object or a Ior.primary or Ior.secondary
|
java.util.Optional<T> |
toOptional() |
default Try<T,java.lang.Throwable> |
toTry() |
Xor<X,T> |
toXor() |
default Xor<X,T> |
toXorWithError() |
default <R> Try<R,X> |
trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Performs a map operation that can call a recursive method without running out of stack space
|
default <T> Try<T,X> |
unit(T value) |
<R> R |
visit(java.util.function.Function<? super T,? extends R> success,
java.util.function.Function<? super X,? extends R> failure)
Execute one function conditional on Try state (Success / Failure)
|
static <T,X extends java.lang.Throwable> |
withCatch(Try.CheckedSupplier<T,X> cf,
java.lang.Class<? extends X>... classes)
Try to execute supplied Supplier and will Catch specified Excpetions or java.lang.Exception
if none specified.
|
default Try<T,X> |
zip(java.util.function.BinaryOperator<Zippable<T>> combiner,
Zippable<T> app)
Combine two applicatives together using the provided BinaryOperator (Semigroup, Monoid and Reducer all
extend BinaryOperator - checkout Semigroups and Monoids for a large number of canned combinations).
|
default <T2,R> Try<R,X> |
zip(java.lang.Iterable<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Equivalent to ap, but accepts an Iterable and takes the first value
only from that iterable.
|
default <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> |
zip(java.lang.Iterable<? extends U> other)
Zip (combine) this Zippable with the supplied Iterable combining both into a Tuple2
|
default <S,U> Try<org.jooq.lambda.tuple.Tuple3<T,S,U>,X> |
zip3(java.lang.Iterable<? extends S> second,
java.lang.Iterable<? extends U> third)
zip 3 Streams into one
|
default <S,U,R> Try<R,X> |
zip3(java.lang.Iterable<? extends S> second,
java.lang.Iterable<? extends U> third,
Fn3<? super T,? super S,? super U,? extends R> fn3) |
default <T2,T3,T4> Try<org.jooq.lambda.tuple.Tuple4<T,T2,T3,T4>,X> |
zip4(java.lang.Iterable<? extends T2> second,
java.lang.Iterable<? extends T3> third,
java.lang.Iterable<? extends T4> fourth)
zip 4 Streams into 1
|
default <T2,T3,T4,R> |
zip4(java.lang.Iterable<? extends T2> second,
java.lang.Iterable<? extends T3> third,
java.lang.Iterable<? extends T4> fourth,
Fn4<? super T,? super T2,? super T3,? super T4,? extends R> fn) |
default <T2,R> Try<R,X> |
zipP(org.reactivestreams.Publisher<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Equivalent to ap, but accepts a Publisher and takes the first value
only from that publisher.
|
default <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> |
zipP(org.reactivestreams.Publisher<? extends U> other) |
default <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> |
zipS(java.util.stream.Stream<? extends U> other)
Zip (combine) this Zippable with the supplied Stream combining both into a Tuple2
|
default <U,R> Try<R,X> |
zipS(java.util.stream.Stream<? extends U> other,
java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zip (combine) this Zippable with the supplied Stream, using the supplied combining function
|
default <R> Try<R,X> |
zipWith(java.lang.Iterable<java.util.function.Function<? super T,? extends R>> fn) |
default <R> Try<R,X> |
zipWithP(org.reactivestreams.Publisher<java.util.function.Function<? super T,? extends R>> fn) |
default <R> Try<R,X> |
zipWithS(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn) |
combineEager, flatMapI, flatMapP
apply, generate, iterate, mkString, newSubscriber, of, subscribe, test, toEvalAlways, toEvalLater, toEvalNow, toMaybe, toTry, toTry, toXor
endsWith, endsWithIterable, findAny, findFirst, firstValue, foldLeft, foldLeft, foldLeft, foldLeft, foldRight, foldRight, foldRight, foldRightMapToType, get, groupBy, join, join, join, mapReduce, mapReduce, print, print, printErr, printOut, reduce, reduce, reduce, reduce, reduce, reduce, reduce, schedule, scheduleFixedDelay, scheduleFixedRate, single, single, singleOptional, startsWith, startsWithIterable, toConcurrentLazyCollection, toConcurrentLazyStreamable, toLazyCollection, xMatch
futureStream, getStreamable, isEmpty, jdkStream, reactiveSeq, reveresedJDKStream, reveresedStream
collect, fromSupplier, orElseThrow, toCompletableFuture, toCompletableFutureAsync, toCompletableFutureAsync, toFuture, toFutureWAsync, toFutureWAsync, toStream, visit
@SafeVarargs static <T,X extends java.lang.Throwable> Try<T,X> fromPublisher(org.reactivestreams.Publisher<T> pub, java.lang.Class<X>... classes)
ReactiveSeq<Integer> reactiveStream = ReactiveSeq.of(1,2,3);
Try<Integer,Throwable> attempt = Try.fromPublisher(reactiveStream, RuntimeException.class);
//Try[1]
pub
- Publisher to extract value fromstatic <T> Try<T,java.lang.Throwable> fromPublisher(org.reactivestreams.Publisher<T> pub)
ReactiveSeq<Integer> reactiveStream = ReactiveSeq.of(1,2,3);
Try<Integer,Throwable> attempt = Try.fromPublisher(reactiveStream);
//Try[1]
pub
- Publisher to extract value fromstatic <T,X extends java.lang.Throwable> Try<T,X> fromIterable(java.lang.Iterable<T> iterable)
ReactiveSeq<Integer> reactiveStream = ReactiveSeq.of(1,2,3);
Try<Integer,Throwable> attempt = Try.fromIterable(reactiveStream);
//Try[1]
iterable
- Iterable to extract value fromdefault <R> Try<R,X> zipWith(java.lang.Iterable<java.util.function.Function<? super T,? extends R>> fn)
default <R> Try<R,X> zipWithS(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn)
default <R> Try<R,X> zipWithP(org.reactivestreams.Publisher<java.util.function.Function<? super T,? extends R>> fn)
default <R> Try<R,X> retry(java.util.function.Function<? super T,? extends R> fn)
Transformable
given(serviceMock.apply(anyInt())).willThrow(
new RuntimeException(new SocketException("First")),
new RuntimeException(new IOException("Second"))).willReturn(
"42");
String result = ReactiveSeq.of( 1, 2, 3)
.retry(serviceMock)
.firstValue();
//result = 42
retry
in interface Transformable<T>
fn
- Function to retry if failsdefault <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> zipP(org.reactivestreams.Publisher<? extends U> other)
default <R> Try<R,X> retry(java.util.function.Function<? super T,? extends R> fn, int retries, long delay, java.util.concurrent.TimeUnit timeUnit)
Transformable
given(serviceMock.apply(anyInt())).willThrow(
new RuntimeException(new SocketException("First")),
new RuntimeException(new IOException("Second"))).willReturn(
"42");
String result = ReactiveSeq.of( 1, 2, 3)
.retry(serviceMock, 7, 2, TimeUnit.SECONDS)
.firstValue();
//result = 42
retry
in interface Transformable<T>
fn
- Function to retry if failsretries
- Number of retriesdelay
- Delay in TimeUnitstimeUnit
- TimeUnit to use for delaydefault <S,U> Try<org.jooq.lambda.tuple.Tuple3<T,S,U>,X> zip3(java.lang.Iterable<? extends S> second, java.lang.Iterable<? extends U> third)
Zippable
List<Tuple3<Integer, Integer, Character>> list = of(1, 2, 3, 4, 5, 6).zip3(of(100, 200, 300, 400), of('a', 'b', 'c')).collect(Collectors.toList());
// [[1,100,'a'],[2,200,'b'],[3,300,'c']]
default <S,U,R> Try<R,X> zip3(java.lang.Iterable<? extends S> second, java.lang.Iterable<? extends U> third, Fn3<? super T,? super S,? super U,? extends R> fn3)
default <T2,T3,T4> Try<org.jooq.lambda.tuple.Tuple4<T,T2,T3,T4>,X> zip4(java.lang.Iterable<? extends T2> second, java.lang.Iterable<? extends T3> third, java.lang.Iterable<? extends T4> fourth)
Zippable
List<Tuple4<Integer, Integer, Character, String>> list = of(1, 2, 3, 4, 5, 6).zip4(of(100, 200, 300, 400), of('a', 'b', 'c'), of("hello", "world"))
.collect(Collectors.toList());
// [[1,100,'a',"hello"],[2,200,'b',"world"]]
default <T2,T3,T4,R> Try<R,X> zip4(java.lang.Iterable<? extends T2> second, java.lang.Iterable<? extends T3> third, java.lang.Iterable<? extends T4> fourth, Fn4<? super T,? super T2,? super T3,? super T4,? extends R> fn)
default <R> Try<R,X> flatMapS(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> mapper)
flatMapS
in interface MonadicValue<T>
default <T2,R1,R2,R3,R> Try<R,X> forEach4(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2, Fn3<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3, Fn4<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach4(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->Maybe.none(),
(a,b,c,d)->a+b+c+d);
//Maybe.none
forEach4
in interface MonadicValue<T>
value1
- Nested MonadicValue to iterate overvalue2
- Nested MonadicValue to iterate overvalue3
- Nested MonadicValue to iterate overyieldingFunction
- Function with pointers to the current element from both
MonadicValue that generates the new elementsdefault <T2,R1,R2,R3,R> Try<R,X> forEach4(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2, Fn3<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3, Fn4<? super T,? super R1,? super R2,? super R3,java.lang.Boolean> filterFunction, Fn4<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach4(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->Maybe.none(),
(a,b,c,d)->a+b+c<100,
(a,b,c,d)->a+b+c+d);
//Maybe.none
forEach4
in interface MonadicValue<T>
value1
- Nested MonadicValue to iterate overvalue2
- Nested MonadicValue to iterate overvalue3
- Nested MonadicValue to iterate overfilterFunction
- Filter to apply over elements before passing non-filtered
values to the yielding functionyieldingFunction
- Function with pointers to the current element from both
MonadicValue that generates the new elementsdefault <T2,R1,R2,R> Try<R,X> forEach3(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2, Fn3<? super T,? super R1,? super R2,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach3(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->a+b+c<100,
(a,b,c)->a+b+c);
//Maybe[32]
forEach3
in interface MonadicValue<T>
value1
- Nested MonadicValue to iterate overvalue2
- Nested MonadicValue to iterate overyieldingFunction
- Function with pointers to the current element from both
MonadicValue that generates the new elementsdefault <T2,R1,R2,R> Try<R,X> forEach3(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2, Fn3<? super T,? super R1,? super R2,java.lang.Boolean> filterFunction, Fn3<? super T,? super R1,? super R2,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach3(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->a+b+c<100,
(a,b,c)->a+b+c);
//Maybe[32]
forEach3
in interface MonadicValue<T>
value1
- Nested MonadicValue to iterate overvalue2
- Nested MonadicValue to iterate overfilterFunction
- Filter to apply over elements before passing non-filtered
values to the yielding functionyieldingFunction
- Function with pointers to the current element from both
MonadicValue that generates the new elementsdefault <R1,R> Try<R,X> forEach2(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach2(a->Maybe.none(),
(a,b)->a+b);
//Maybe.none()
forEach2
in interface MonadicValue<T>
value1
- Nested Monadic Type to iterate overyieldingFunction
- Function with pointers to the current element from both
monad types that generates the new elementsdefault <R1,R> Try<R,X> forEach2(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,java.lang.Boolean> filterFunction, java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach2(a->Maybe.none(),
a->b-> a<3 && b>10,
(a,b)->a+b);
//Maybe.none()
forEach2
in interface MonadicValue<T>
filterFunction
- Filter to apply over elements before passing non-filtered
values to the yielding functionyieldingFunction
- Function with pointers to the current element from both
Streams that generates the new elementsX failureGet()
Ior<X,T> toIor()
Value
default <R> Try<R,X> coflatMap(java.util.function.Function<? super MonadicValue<T>,R> mapper)
MonadicValue
Maybe.none().coflatMap(m -> m.isPresent() ? m.get() : 10);
//Maybe[10]
coflatMap
in interface MonadicValue<T>
mapper
- Mapping / transformation functiondefault Try<MonadicValue<T>,X> nest()
MonadicValue
nest
in interface MonadicValue<T>
default Try<T,X> combine(Monoid<T> monoid, Try<? extends T,X> v2)
Try<Integer> just = Try.success(10);
Try<Integer> none = Try.failure(new RuntimeException());
Monoid<Integer> add = Monoid.of(0,Semigroups.intSum);
assertThat(just.combine(add,none),equalTo(Try.success(10)));
assertThat(none.combine(add,just),equalTo(Try.success(0)));
assertThat(none.combine(add,none),equalTo(Try.success(0)));
assertThat(just.combine(add,Try.success(10)),equalTo(Try.success(20)));
Monoid<Integer> firstNonNull = Monoid.of(null , Semigroups.firstNonNull());
assertThat(just.combine(firstNonNull,Try.success(null)),equalTo(just));
monoid
- Combinerv2
- Try to combine withdefault Try<T,X> zip(java.util.function.BinaryOperator<Zippable<T>> combiner, Zippable<T> app)
Zippable
zip
in interface Zippable<T>
Semigroup
,
Semigroups
,
Monoid
,
To lift any Semigroup (or monoid) up to handling Applicatives use the combineApplicatives operator in Semigroups
{@see com.aol.cyclops2.Semigroups#combineApplicatives(BiFunction) } or Monoids
{ {@see com.aol.cyclops2.Monoids#combineApplicatives(java.util.function.Function, com.aol.cyclops2.function.Monoid)
}
{@code
BinaryOperator> sumMaybes = Semigroups.combineScalarFunctors(Semigroups.intSum);
Maybe.just(1).zip(sumMaybes, Maybe.just(5))
//Maybe.just(6));
}
default <U> Try<U,X> cast(java.lang.Class<? extends U> type)
Transformable
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)cast
in interface Transformable<T>
default <R> Try<R,X> trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Transformable
ReactiveSeq.of(10,20,30,40)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
Trampoline<Long> fibonacci(int i){
return fibonacci(i,1,0);
}
Trampoline<Long> fibonacci(int n, long a, long b) {
return n == 0 ? Trampoline.done(b) : Trampoline.more( ()->fibonacci(n-1, a+b, a));
}
55
6765
832040
102334155
ReactiveSeq.of(10_000,200_000,3_000_000,40_000_000)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
completes successfully
trampoline
in interface Transformable<T>
mapper
- TCO Transformation functiondefault <U> Maybe<U> ofType(java.lang.Class<? extends U> type)
Filters
// (1, 2, 3) ReactiveSeq.of(1, "a", 2, "b",3).ofType(Integer.class)
default Maybe<T> filterNot(java.util.function.Predicate<? super T> fn)
Filters
of(1,2,3).filter(i->i>2);
//[1,2]
default Maybe<T> notNull()
Filters
of(1,2,null,4).nonNull();
//[1,2,4]
static <T,X extends java.lang.Throwable> Try.Failure<T,X> failure(X error)
Failure<Exception> failure = Try.failure(new RuntimeException());
error
- for Failurestatic <T,X extends java.lang.Throwable> Try.Success<T,X> success(T value)
Success<Integer> success = Try.success(new RuntimeException());
value
- Successful valuedefault Xor<X,T> toXorWithError()
<R> R visit(java.util.function.Function<? super T,? extends R> success, java.util.function.Function<? super X,? extends R> failure)
Try<Integer> result = this.execute();
this.logState(result.visit(t->"value is " +t,e->"error is "+e.getMessage());
success
- Function to execute if this Try is a Successfailure
- Funcion to execute if this Try is a Failuredefault AnyMValue<Witness.tryType,T> anyM()
T get()
org.jooq.lambda.function.Function0
get
in interface Convertable<T>
get
in interface org.jooq.lambda.function.Function0<T>
get
in interface java.util.function.Supplier<T>
void throwException()
T orElse(T value)
Convertable
orElse
in interface Convertable<T>
value
- Return value supplied if Failure, otherwise return Success valueT orElseGet(java.util.function.Supplier<? extends T> value)
Convertable
Optional.orElseGet(Supplier)
Returns a value generated by the provided Supplier if this Convertable is empty or
contains a null value.orElseGet
in interface Convertable<T>
value
- from supplied Supplier if Failure otherwise return Success value<R> Try<R,X> map(java.util.function.Function<? super T,? extends R> fn)
Transformable
of(1,2,3).map(i->i*2)
//[2,4,6]
map
in interface MonadicValue<T>
map
in interface Transformable<T>
fn
- Map success value from T to R. Do nothing if Failure (return this)<R> Try<R,X> flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends R>> fn)
MonadicValue
Optional.flatMap(Function)
Eval.now(1).map(i->i+2).flatMap(i->Eval.later(()->i*3);
//Eval[9]
flatMap
in interface MonadicValue<T>
fn
- FlatMap success value or Do nothing if Failure (return this)Maybe<T> filter(java.util.function.Predicate<? super T> p)
Filters
of(1,2,3).filter(i->i>2);
//[3]
filter
in interface Filters<T>
filter
in interface MonadicValue<T>
p
- Convert a Success to a Failure (with a null value for Exception) if predicate does not hold.
Do nothing to a FailureTry<T,X> onFail(java.util.function.Consumer<? super X> consumer)
consumer
- Accept Exception if present (Failure)Try<T,X> onFail(java.lang.Class<? extends X> t, java.util.function.Consumer<X> consumer)
t
- Class type of match Exception againstconsumer
- Accept Exception if present (Failure) and if class types matchTry<T,X> recover(java.util.function.Function<? super X,? extends T> fn)
fn
- Recovery function - map from a failure to a Success.Try<T,X> recoverWith(java.util.function.Function<? super X,? extends Try<T,X>> fn)
fn
- Recovery FlatMap function. Map from a failure to a SuccessTry<T,X> recoverFor(java.lang.Class<? extends X> t, java.util.function.Function<? super X,? extends T> fn)
t
- Type of exception to match againstfn
- Recovery functionTry<T,X> recoverWithFor(java.lang.Class<? extends X> t, java.util.function.Function<? super X,? extends Try.Success<T,X>> fn)
t
- Type of exception to match againstfn
- Recovery FlatMap function. Map from a failure to a Successjava.util.Optional<T> toOptional()
toOptional
in interface Convertable<T>
ReactiveSeq<T> stream()
java.util.Optional<X> toFailedOptional()
java.util.stream.Stream<X> toFailedStream()
boolean isSuccess()
boolean isFailure()
void forEach(java.util.function.Consumer<? super T> consumer)
forEach
in interface java.lang.Iterable<T>
consumer
- Accept value if Success / not called on Failurevoid forEachFailed(java.util.function.Consumer<? super X> consumer)
consumer
- Accept value if Failure / not called on Failuredefault boolean isPresent()
isPresent
in interface Convertable<T>
default Try<T,X> peek(java.util.function.Consumer<? super T> consumer)
Transformable
of(1,2,3).map(System.out::println)
1
2
3
peek
in interface Transformable<T>
consumer
- Accept value if Successdefault Try<T,X> peekFailed(java.util.function.Consumer<? super X> consumer)
consumer
- Accept Exception if Failuredefault java.util.Iterator<T> iterator()
@SafeVarargs static <T,X extends java.lang.Throwable> Try<T,X> of(T value, java.lang.Class<? extends java.lang.Throwable>... classes)
value
- Initial valueclasses
- Exceptions to catch during map / flatMap@SafeVarargs static <T,X extends java.lang.Throwable> Try<T,X> withCatch(Try.CheckedSupplier<T,X> cf, java.lang.Class<? extends X>... classes)
cf
- CheckedSupplier to attempt to executeclasses
- Exception types to catch (or java.lang.Exception if none specified)@SafeVarargs static <X extends java.lang.Throwable> Try<java.lang.Void,X> runWithCatch(Try.CheckedRunnable<X> cf, java.lang.Class<? extends X>... classes)
cf
- CheckedRunnable to attempt to executeclasses
- Exception types to catch (or java.lang.Exception if none specified)@SafeVarargs static <X extends java.lang.Throwable> Try.Init<X> catchExceptions(java.lang.Class<? extends X>... classes)
classes
- Exception types to catchdefault <T2,R> Try<R,X> combine(Value<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
MonadicValue
Maybe<Integer> some = Maybe.just(10);
just.combine(Eval.now(20), this::add);
//Some[30]
Maybe<Integer> none = Maybe.none();
none.combine(Eval.now(20), this::add);
//None
combine
in interface MonadicValue<T>
app
- Value to combine with this one.fn
- BiFunction to combine themdefault <T2,R> Try<R,X> zip(java.lang.Iterable<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
default <T2,R> Try<R,X> zipP(org.reactivestreams.Publisher<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
default <U,R> Try<R,X> zipS(java.util.stream.Stream<? extends U> other, java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zippable
default <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> zipS(java.util.stream.Stream<? extends U> other)
Zippable