T
- Return type (success)X
- Base Error typepublic interface Try<T,X extends java.lang.Throwable> extends To<Try<T,X>>, java.util.function.Supplier<T>, MonadicValue<T>, ToStream<T>, Filterable<T>, Functor<T>, ApplicativeFunctor<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> |
ApplicativeFunctor.Applicatives, ApplicativeFunctor.ApplyFunctions<T>
Value.ValueImpl<T>
Convertable.SupplierToConvertable<T>
Modifier and Type | Method and Description |
---|---|
AnyMValue<T> |
anyM() |
AnyM<X> |
anyMFailure() |
AnyM<T> |
anyMSuccess() |
default <U> Try<U,X> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a stream 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 Combiner<T> |
combine(java.util.function.BinaryOperator<Combiner<T>> combiner,
Combiner<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 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) |
Try<T,X> |
flatten()
Flatten a nested Try Structure
|
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 stream
|
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,
TriFunction<? 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,
TriFunction<? super T,? super R1,? super R2,java.lang.Boolean> filterFunction,
TriFunction<? 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,
TriFunction<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3,
QuadFunction<? 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,
TriFunction<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3,
QuadFunction<? super T,? super R1,? super R2,? super R3,java.lang.Boolean> filterFunction,
QuadFunction<? 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() |
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 <R> Eval<R> |
matches(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> successCase,
java.util.function.Function<Matchable.CheckValue1<X,R>,Matchable.CheckValue1<X,R>> failureCase,
java.util.function.Supplier<? extends R> otherwise)
Pattern match on the value/s inside this Try
|
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 stream 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 <R> Try<R,X> |
patternMatch(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> case1,
java.util.function.Supplier<? extends R> otherwise)
Transform the elements of this Stream with a Pattern Matching case and default value
|
default Try<T,X> |
peek(java.util.function.Consumer<? super T> consumer)
Peek at the current value of this Functor, without transforming it
|
default Try<T,X> |
peekFailed(java.util.function.Consumer<? super X> consumer) |
Try.Success<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.Success<T,X> |
recoverWith(java.util.function.Function<? super X,? extends Try.Success<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
|
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 <T2,R> Try<R,X> |
zip(java.util.function.BiFunction<? super T,? super T2,? extends R> fn,
org.reactivestreams.Publisher<? extends T2> app)
Equivalent to ap, but accepts a Publisher and takes the first value
only from that publisher.
|
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 <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> |
zip(org.jooq.lambda.Seq<? extends U> other)
Zip (combine) this Zippable with the supplied Seq combining both into a Tuple2
|
default <U,R> Try<R,X> |
zip(org.jooq.lambda.Seq<? extends U> other,
java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zip (combine) this Zippable with the supplied Seq, using the supplied combining function
|
default <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> |
zip(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> |
zip(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
|
combineEager, flatMapIterable, flatMapPublisher
futureStream, getStreamable, isEmpty, jdkStream, reactiveSeq, reveresedJDKStream, reveresedStream
applyFunctions
collect, fold, fold, generate, iterate, mapReduce, mkString, newSubscriber, of, subscribe, test, toDequeX, toEvalAlways, toEvalLater, toEvalNow, toFeatureToggle, toFutureStream, toFutureStream, toLazyImmutable, toList, toListX, toMaybe, toMutable, toPBagX, toPOrderedSetX, toPQueueX, toPSetX, toPStackX, toPVectorX, toQueueX, toSetX, toSimpleReact, toSimpleReact, toSortedSetX, toTry, toTry, toXor, unapply
endsWith, endsWithIterable, findAny, findFirst, firstValue, foldable, foldRight, foldRight, foldRight, foldRightMapToType, get, groupBy, join, join, join, mapReduce, print, print, printErr, printOut, reduce, reduce, reduce, reduce, reduce, reduce, reduce, schedule, scheduleFixedDelay, scheduleFixedRate, single, single, singleOptional, startsWith, startsWithIterable, toConcurrentLazyCollection, toConcurrentLazyStreamable, toLazyCollection, validate, xMatch
collect, fromSupplier, orElseThrow, toAtomicReference, toCompletableFuture, toCompletableFutureAsync, toCompletableFutureAsync, toFutureW, toFutureWAsync, toFutureWAsync, toOptionalAtomicReference, 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> stream = ReactiveSeq.of(1,2,3);
Try<Integer,Throwable> attempt = Try.fromPublisher(stream, RuntimeException.class);
//Try[1]
pub
- Publisher to extract value fromstatic <T> Try<T,java.lang.Throwable> fromPublisher(org.reactivestreams.Publisher<T> pub)
ReactiveSeq<Integer> stream = ReactiveSeq.of(1,2,3);
Try<Integer,Throwable> attempt = Try.fromPublisher(stream);
//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> stream = ReactiveSeq.of(1,2,3);
Try<Integer,Throwable> attempt = Try.fromIterable(stream);
//Try[1]
iterable
- Iterable to extract value fromdefault <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, TriFunction<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3, QuadFunction<? 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, TriFunction<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3, QuadFunction<? super T,? super R1,? super R2,? super R3,java.lang.Boolean> filterFunction, QuadFunction<? 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, TriFunction<? 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, TriFunction<? super T,? super R1,? super R2,java.lang.Boolean> filterFunction, TriFunction<? 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 elementsdefault <R> Eval<R> matches(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> successCase, java.util.function.Function<Matchable.CheckValue1<X,R>,Matchable.CheckValue1<X,R>> failureCase, java.util.function.Supplier<? extends R> otherwise)
import static com.aol.cyclops.control.Matchable.otherwise;
import static com.aol.cyclops.control.Matchable.then;
import static com.aol.cyclops.control.Matchable.when;
import static com.aol.cyclops.util.function.Predicates.instanceOf;
Try.success(10)
.matches(c->c.is(when(10),then("hello")),
c->c.is(when(instanceOf(Throwable.class)), then("error")),
otherwise("miss"))
.get()
//"hello"
successCase
- Pattern matching function executed if this Try is a SuccessfailureCase
- Pattern matching function executed if this Try is a Failureotherwise
- Supplier used to provide a value if the selecting pattern matching function fails to find a matchX 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 Combiner<T> combine(java.util.function.BinaryOperator<Combiner<T>> combiner, Combiner<T> app)
Combiner
combine
in interface Combiner<T>
Semigroup
,
Semigroups
,
Monoid
,
To lift any Semigroup (or monoid) up to handling Applicatives use the combineApplicatives operator in Semigroups
{@see com.aol.cyclops.Semigroups#combineApplicatives(BiFunction) } or Monoids
{ {@see com.aol.cyclops.Monoids#combineApplicatives(java.util.function.Function, com.aol.cyclops.Monoid)
}
{@code
BinaryOperator> sumMaybes = Semigroups.combineScalarFunctors(Semigroups.intSum);
Maybe.just(1)
.combine(sumMaybes, Maybe.just(5))
//Maybe.just(6));
}
default <U> Try<U,X> cast(java.lang.Class<? extends U> type)
Functor
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)default <R> Try<R,X> trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Functor
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 Functor<T>
mapper
- TCO Transformation functiondefault <U> Maybe<U> ofType(java.lang.Class<? extends U> type)
Filterable
// (1, 2, 3) ReactiveSeq.of(1, "a", 2, "b",3).ofType(Integer.class)
ofType
in interface Filterable<T>
default Maybe<T> filterNot(java.util.function.Predicate<? super T> fn)
Filterable
of(1,2,3).filter(i->i>2);
//[1,2]
filterNot
in interface Filterable<T>
fn
- to filter elements by, retaining matchesdefault Maybe<T> notNull()
Filterable
of(1,2,null,4).nonNull();
//[1,2,4]
notNull
in interface Filterable<T>
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()
default <R> Try<R,X> patternMatch(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> case1, java.util.function.Supplier<? extends R> otherwise)
Functor
List<String> result = CollectionX.of(1,2,3,4)
.patternMatch(
c->c.valuesWhere(i->"even", (Integer i)->i%2==0 )
)
// CollectionX["odd","even","odd","even"]
patternMatch
in interface Functor<T>
case1
- Function to generate a case (or chain of cases as a single case)otherwise
- Value if supplied case doesn't match<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 FailureAnyMValue<T> anyM()
anyM
in interface MonadicValue<T>
T get()
get
in interface Convertable<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)
Functor
of(1,2,3).map(i->i*2)
//[2,4,6]
map
in interface ConvertableFunctor<T>
map
in interface Functor<T>
map
in interface MonadicValue<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)
Filterable
of(1,2,3).filter(i->i>2);
//[3]
filter
in interface Filterable<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.Success<T,X> recover(java.util.function.Function<? super X,? extends T> fn)
fn
- Recovery function - map from a failure to a Success.Try.Success<T,X> recoverWith(java.util.function.Function<? super X,? extends Try.Success<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)
Functor
of(1,2,3).map(System.out::println)
1
2
3
default 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)
Combiner
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
default <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> zip(java.util.function.BiFunction<? super T,? super T2,? extends R> fn, org.reactivestreams.Publisher<? extends T2> app)
default <U,R> Try<R,X> zip(org.jooq.lambda.Seq<? extends U> other, java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zippable
default <U,R> Try<R,X> zip(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> zip(java.util.stream.Stream<? extends U> other)
Zippable
default <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> zip(org.jooq.lambda.Seq<? extends U> other)
Zippable