T - Return type (success)X - Base Error typepublic class Try<T,X extends java.lang.Throwable> extends java.lang.Object implements To<Try<T,X>>, RecoverableFrom<X,T>, MonadicValue<T>, Higher2<Witness.tryType,X,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("public");
//"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->"public);
By public Try does not catch exception within it's operators such as map / flatMap, to catch Exceptions in ongoing operations use @see Try#of(Object, Class...)
Try.of(2, RuntimeException.class)
.map(i->{throw new RuntimeException();});
//Failure[RuntimeException]
| Modifier and Type | Class 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 interface |
Try.Init<X extends java.lang.Throwable> |
static class |
Try.Instances |
static class |
Try.MyFinallyBlock<T,V,X extends java.lang.Throwable> |
static interface |
Try.TryCatch<V,X extends java.lang.Throwable> |
Value.ValueImpl<T>Convertable.SupplierToConvertable<T>Fn0.FunctionalOperations<R>, Fn0.SupplierKind<R>| Constructor and Description |
|---|
Try() |
| Modifier and Type | Method and Description |
|---|---|
static <ST extends java.lang.Throwable,PT,R> |
accumulateFailures(CollectionX<Try<PT,ST>> xors,
java.util.function.Function<? super ST,R> mapper,
Monoid<R> reducer)
Accumulate the results only from those Trys which have a Secondary type present, using the supplied mapping function to
convert the data from each Try before reducing them using the supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
cyclops2.Monoids .. |
static <ST extends java.lang.Throwable,PT,R> |
accumulateFailures(CollectionX<Try<PT,ST>> xors,
Reducer<R> reducer)
Accumulate the result of the Secondary types in the Collection of Trys provided using the supplied Reducer
cyclops2.Reducers. |
static <ST extends java.lang.Throwable,PT> |
accumulateFailures(Monoid<ST> reducer,
CollectionX<Try<PT,ST>> xors)
Accumulate the results only from those Trys which have a Secondary type present, using the supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
cyclops2.Monoids . |
static <ST extends java.lang.Throwable,PT,R> |
accumulateSuccesses(CollectionX<Try<PT,ST>> xors,
java.util.function.Function<? super PT,R> mapper,
Monoid<R> reducer)
\
Accumulate the results only from those Iors which have a Primary type present, using the supplied mapping function to
convert the data from each Try before reducing them using the supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
cyclops2.Monoids .. |
static <ST extends java.lang.Throwable,PT,R> |
accumulateSuccesses(CollectionX<Try<PT,ST>> xors,
Reducer<R> reducer)
Accumulate the result of the Primary types in the Collection of Trys provided using the supplied Reducer
cyclops2.Reducers. |
static <ST extends java.lang.Throwable,PT> |
accumulateSuccesses(Monoid<PT> reducer,
CollectionX<Try<PT,ST>> xors)
Accumulate the results only from those Trys which have a Primary type present, using the supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
cyclops2.Monoids . |
Active<Higher<Witness.tryType,X>,T> |
allTypeclasses() |
AnyMValue2<Witness.tryType,X,T> |
anyM() |
int |
arity() |
Xor<X,T> |
asXor() |
<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.
|
<R> Try<R,X> |
coflatMap(java.util.function.Function<? super MonadicValue<T>,R> mapper)
Perform a coflatMap operation.
|
Try<T,X> |
combine(Monoid<T> monoid,
Try<? extends T,X> v2)
Combine this Try with another using the supplied Monoid as a combiner
|
<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
|
Try<T,X> |
combineEager(Monoid<T> monoid,
MonadicValue<? extends T> v2)
Eagerly combine two MonadicValues using the supplied monoid (@see ApplicativeFunctor for type appropraite i.e.
|
<W1> Coproduct<W1,Higher<Witness.tryType,X>,T> |
coproduct(InstanceDefinitions<W1> def2) |
boolean |
equals(java.lang.Object o) |
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.
|
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) |
<R> Try<R,X> |
flatMapI(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> mapper)
A flattening transformation operation that takes the first value from the returned Iterable.
|
<R> Try<R,X> |
flatMapP(java.util.function.Function<? super T,? extends org.reactivestreams.Publisher<? extends R>> mapper)
A flattening transformation operation that takes the first value from the returned Publisher.
|
<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) |
<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
|
<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
|
<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
|
<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
|
<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
|
<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 singleUnsafe 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 singleUnsafe value extracted from the supplied reactiveBuffer-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 singleUnsafe value extracted from the supplied reactiveBuffer-streams Publisher, will catch any Exceptions
of the provided types
|
static <T,X extends java.lang.Throwable> |
fromXor(Xor<X,T> pub) |
T |
get() |
int |
hashCode() |
boolean |
isFailure() |
boolean |
isPresent() |
boolean |
isSuccess() |
java.util.Iterator<T> |
iterator() |
static <X extends java.lang.Throwable,T> |
kindCokleisli() |
static <X extends java.lang.Throwable,T> |
kindKleisli() |
<R> Try<R,X> |
map(java.util.function.Function<? super T,? extends R> fn)
Transform this functor using the supplied transformation function
|
<W2,R> Nested<Higher<Witness.tryType,X>,W2,R> |
mapM(java.util.function.Function<? super T,? extends Higher<W2,R>> fn,
InstanceDefinitions<W2> defs) |
java.lang.String |
mkString()
Returns the class name and the name of the subclass, if there is any value, the value is showed between square brackets.
|
static <T,X extends java.lang.Throwable> |
narrowK(Higher<Higher<Witness.tryType,X>,T> t) |
static <T,X extends java.lang.Throwable> |
narrowK2(Higher2<Witness.tryType,X,T> t) |
Try<MonadicValue<T>,X> |
nest()
cojoin pattern.
|
static <W1,X extends java.lang.Throwable,T> |
nested(Try<Higher<W1,T>,X> nested,
InstanceDefinitions<W1> def2) |
Maybe<T> |
notNull()
Filter elements retaining only values which are not null
|
<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 zero or
contains a null value. |
Try<T,X> |
peek(java.util.function.Consumer<? super T> consumer)
Peek at the current value of this Transformable, without transforming it
|
Try<T,X> |
peekFailed(java.util.function.Consumer<? super X> consumer) |
<W1> Product<Higher<Witness.tryType,X>,W1,T> |
product(Active<W1,T> active) |
Try<T,X> |
recover(java.util.function.Function<? super X,? extends T> fn) |
Try<T,X> |
recover(java.util.function.Supplier<? extends T> s) |
Try<T,X> |
recoverFlatMap(java.util.function.Function<? super X,? extends Try<T,X>> fn)
flatMap recovery
|
Try<T,X> |
recoverFlatMapFor(java.lang.Class<? extends X> t,
java.util.function.Function<? super X,? extends Try<T,X>> 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
|
<R> Try<R,X> |
retry(java.util.function.Function<? super T,? extends R> fn)
Retry a transformation if it fails.
|
<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.
|
static <ST extends java.lang.Throwable,PT> |
sequenceFailures(CollectionX<Try<PT,ST>> xors)
Turn a list of Trys into a single Try with Lists of values.
|
static <ST extends java.lang.Throwable,PT> |
sequenceSuccess(CollectionX<Try<PT,ST>> xors)
Turn a toX of Trys into a single Ior with Lists of values.
|
ReactiveSeq<T> |
stream() |
void |
subscribe(org.reactivestreams.Subscriber<? super T> sub) |
static <T,X extends java.lang.Throwable> |
success(T value)
Construct a Success instance (an implementation of Try)
|
static <T,X extends java.lang.Throwable> |
success(T value,
java.lang.Class<? extends java.lang.Throwable>... classes) |
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() |
java.lang.String |
toString() |
Trampoline<Xor<X,T>> |
toTrampoline() |
Try<T,java.lang.Throwable> |
toTry() |
Xor<X,T> |
toXor() |
Xor<X,T> |
toXorWithError() |
<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
|
<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 <X extends java.lang.Throwable,T> |
widen(Try<T,X> narrow) |
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.
|
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 SemigroupK and Monoids for a large number of canned combinations).
|
<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.
|
<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
|
<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
|
<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) |
<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
|
<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) |
<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.
|
<U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> |
zipP(org.reactivestreams.Publisher<? extends U> other) |
<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
|
<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
|
<R> Try<R,X> |
zipWith(java.lang.Iterable<java.util.function.Function<? super T,? extends R>> fn) |
<R> Try<R,X> |
zipWithP(org.reactivestreams.Publisher<java.util.function.Function<? super T,? extends R>> fn) |
<R> Try<R,X> |
zipWithS(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn) |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitapply, generate, iterate, newSubscriber, of, test, toEither, toEval, toEvalAlways, toEvalLater, toEvalNow, toLeft, toMaybe, toMaybeEager, toRight, toTry, toTry, toXorendsWith, 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, singleUnsafe, startsWith, startsWithIterable, takeOne, xMatchcollect, fromSupplier, orElseThrow, toCompletableFuture, toCompletableFutureAsync, toFuture, toFutureAsync, toStream, visitpublic static <X extends java.lang.Throwable,T> Kleisli<Higher<Witness.tryType,X>,Try<T,X>,T> kindKleisli()
public static <X extends java.lang.Throwable,T> Higher<Higher<Witness.tryType,X>,T> widen(Try<T,X> narrow)
public static <X extends java.lang.Throwable,T> Cokleisli<Higher<Witness.tryType,X>,T,Try<T,X>> kindCokleisli()
public Active<Higher<Witness.tryType,X>,T> allTypeclasses()
public <W2,R> Nested<Higher<Witness.tryType,X>,W2,R> mapM(java.util.function.Function<? super T,? extends Higher<W2,R>> fn, InstanceDefinitions<W2> defs)
public Trampoline<Xor<X,T>> toTrampoline()
public void subscribe(org.reactivestreams.Subscriber<? super T> sub)
public static <ST extends java.lang.Throwable,PT> Xor<ListX<PT>,ListX<ST>> sequenceFailures(CollectionX<Try<PT,ST>> xors)
Try<Integer,NoSuchElementException> just = Try.success(10);
Try<Integer,NoSuchElementException> none = Try.failure(new NoSuchElementException());
Xor<ListX<Integer>,ListX<NoSuchElementException>> xors =Try.sequenceFailures(ListX.of(just,none,Try.success(1)));
//[Primary[NoSuchElementException]]
xors - Trys to sequencepublic static <ST extends java.lang.Throwable,PT,R> Xor<?,R> accumulateFailures(CollectionX<Try<PT,ST>> xors, Reducer<R> reducer)
cyclops2.Reducers.
Try<Integer,NoSuchElementException> just = Try.success(10);
Try<Integer,NoSuchElementException> none = Try.failure(new NoSuchElementException());
Xor<?,PersistentSetX<String>> xors = Try.accumulateFailures(ListX.of(just,none,Try.success(1)),Reducers.<String>toPersistentSetX());
//Primary[PersistentSetX[NoSuchElementException]]]
xors - Collection of Iors to accumulate failure valuesreducer - Reducer to accumulate resultspublic static <ST extends java.lang.Throwable,PT,R> Xor<?,R> accumulateFailures(CollectionX<Try<PT,ST>> xors, java.util.function.Function<? super ST,R> mapper, Monoid<R> reducer)
cyclops2.Monoids ..
Try<Integer,NoSuchElementException> just = Try.success(10);
Try<Integer,NoSuchElementException> none = Try.failure(new NoSuchElementException());
Xor<?,String> xors = Try.accumulateFailures(ListX.of(just,none,Try.failure("1")),i->""+i,Monoids.stringConcat);
//Primary[NoSuchElementException]]
xors - Collection of Iors to accumulate failure valuesmapper - Mapping function to be applied to the result of each Iorreducer - Semigroup to combine values from each Iorpublic static <ST extends java.lang.Throwable,PT> Xor<ListX<ST>,ListX<PT>> sequenceSuccess(CollectionX<Try<PT,ST>> xors)
Try<Integer,NoSuchElementException> just = Try.success(10);
Try<Integer,NoSuchElementException> none = Try.failure(new NoSuchElementException());
Xor<ListX<String>,ListX<Integer>> xors =Try.sequenceSuccess(ListX.of(just,none,Try.success(1)));
//Primary(ListX.of(10,1)));
iors - Trys to sequencepublic static <ST extends java.lang.Throwable,PT,R> Xor<?,R> accumulateSuccesses(CollectionX<Try<PT,ST>> xors, Reducer<R> reducer)
cyclops2.Reducers.
Try<Integer,NoSuchElementException> just = Try.success(10);
Try<Integer,NoSuchElementException> none = Try.failure(new NoSuchElementException());
Try<PersistentSetX<Integer>,Throwable> xors =Try.accumulateSuccesses(ListX.of(just,none,Try.success(1)),Reducers.toPersistentSetX());
//Primary[PersistentSetX[10,1]]
Trys - Collection of Iors to accumulate success valuesreducer - Reducer to accumulate resultspublic static <ST extends java.lang.Throwable,PT,R> Xor<?,R> accumulateSuccesses(CollectionX<Try<PT,ST>> xors, java.util.function.Function<? super PT,R> mapper, Monoid<R> reducer)
cyclops2.Monoids ..
Try<Integer,NoSuchElementException> just = Try.success(10);
Try<Integer,NoSuchElementException> none = Try.failure(new NoSuchElementException());
Xor<?,String> iors = Try.accumulateSuccesses(ListX.of(just,none,Try.success(1)),i->""+i,Monoids.stringConcat);
//Primary["101"]
xors - Collection of Iors to accumulate success valuesmapper - Mapping function to be applied to the result of each Iorreducer - Reducer to accumulate resultspublic static <ST extends java.lang.Throwable,PT> Xor<?,PT> accumulateSuccesses(Monoid<PT> reducer, CollectionX<Try<PT,ST>> xors)
cyclops2.Monoids .
Try<Integer,NoSuchElementException> just = Try.success(10);
Try<Integer,NoSuchElementException> none = Try.failure(new NoSuchElementException());
Try<?,Integer> xors = Try.accumulateSuccesses(Monoids.intSum,ListX.of(just,none,Try.success(1)));
//Primary[11]
xors - Collection of Trys to accumulate success valuesreducer - Reducer to accumulate resultspublic static <ST extends java.lang.Throwable,PT> Xor<?,ST> accumulateFailures(Monoid<ST> reducer, CollectionX<Try<PT,ST>> xors)
cyclops2.Monoids .
Try.accumulateFailures(ListX.of(Try.failure(new NoSuchElementException());,
Try.failure(new NoSuchElementException());
Try.success("success")),SemigroupK.stringConcat)
//Primary[NoSuchElementException, NoSuchElementException]
xors - Collection of Trys to accumulate failure valuesreducer - Semigroup to combine values from each Trypublic Try<T,X> recover(java.util.function.Supplier<? extends T> s)
recover in interface Recoverable<T>@SafeVarargs public 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> recover = Try.fromPublisher(reactiveStream, RuntimeException.class);
//Try[1]
pub - Publisher to extract value frompublic static <T> Try<T,java.lang.Throwable> fromPublisher(org.reactivestreams.Publisher<T> pub)
ReactiveSeq<Integer> reactiveStream = ReactiveSeq.of(1,2,3);
Try<Integer,Throwable> recover = Try.fromPublisher(reactiveStream);
//Try[1]
pub - Publisher to extract value frompublic static <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> recover = Try.fromIterable(reactiveStream);
//Try[1]
iterable - Iterable to extract value frompublic <R> Try<R,X> zipWith(java.lang.Iterable<java.util.function.Function<? super T,? extends R>> fn)
public <R> Try<R,X> zipWithS(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn)
public <R> Try<R,X> zipWithP(org.reactivestreams.Publisher<java.util.function.Function<? super T,? extends R>> fn)
public <R> Try<R,X> retry(java.util.function.Function<? super T,? extends R> fn)
Transformable
given(serviceMock.applyHKT(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 failspublic <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> zipP(org.reactivestreams.Publisher<? extends U> other)
public <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.applyHKT(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 delaypublic <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(CyclopsCollectors.toList());
// [[1,100,'a'],[2,200,'b'],[3,300,'c']]
public <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)
public <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(CyclopsCollectors.toList());
// [[1,100,'a',"hello"],[2,200,'b',"world"]]
public <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)
public <R> Try<R,X> flatMapS(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> mapper)
flatMapS in interface MonadicValue<T>public Try<T,X> combineEager(Monoid<T> monoid, MonadicValue<? extends T> v2)
MonadicValue
Monoid<Integer> add = Monoid.of(1,SemigroupK.intSum);
Maybe.of(10).combineEager(add,Maybe.none());
//Maybe[10]
Maybe.none().combineEager(add,Maybe.of(10));
//Maybe[10]
Maybe.none().combineEager(add,Maybe.none());
//Maybe.none()
Maybe.of(10).combineEager(add,Maybe.of(10));
//Maybe[20]
Monoid<Integer> firstNonNull = Monoid.of(null , SemigroupK.firstNonNull());
Maybe.of(10).combineEager(firstNonNull,Maybe.of(10));
//Maybe[10]
combineEager in interface MonadicValue<T>public <R> Try<R,X> flatMapI(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> mapper)
MonadicValue
Maybe.just(1).map(i->i+2).flatMapI(i->Arrays.asList(()->i*3,20);
//Maybe[9]
flatMapI in interface MonadicValue<T>mapper - transformation functionpublic <R> Try<R,X> flatMapP(java.util.function.Function<? super T,? extends org.reactivestreams.Publisher<? extends R>> mapper)
MonadicValue
Future.ofResult(1).map(i->i+2).flatMapP(i->Flux.just(()->i*3,20);
//Future[9]
flatMapP in interface MonadicValue<T>mapper - transformation functionpublic <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 elementspublic <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)
MonadicValueMaybe.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 applyHKT 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 elementspublic <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 elementspublic <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 applyHKT 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 elementspublic <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 elementspublic <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 applyHKT 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 elementspublic X failureGet()
public Ior<X,T> toIor()
Valuepublic <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 functionpublic Try<MonadicValue<T>,X> nest()
MonadicValuenest in interface MonadicValue<T>public 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,SemigroupK.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 , SemigroupK.firstNonNull());
assertThat(just.combine(firstNonNull,Try.success(null)),equalTo(just));
monoid - Combinerv2 - Try to combine withpublic Try<T,X> zip(java.util.function.BinaryOperator<Zippable<T>> combiner, Zippable<T> app)
Zippablezip in interface Zippable<T>Semigroup,
Semigroups,
Monoid,
To lift any Semigroup (or monoid) up to handling Applicatives use the combineApplicatives operator in SemigroupK
{@see com.aol.cyclops2.SemigroupK#combineApplicatives(BiFunction) } or Monoids
{ {@see com.aol.cyclops2.Monoids#combineApplicatives(java.util.function.Function, com.aol.cyclops2.function.Monoid)
}
{@code
BinaryOperator> sumMaybes = SemigroupK.combineScalarFunctors(SemigroupK.intSum);
Maybe.just(1).zip(sumMaybes, Maybe.just(5))
//Maybe.just(6));
}
public <U> Try<U,X> cast(java.lang.Class<? extends U> type)
TransformableClassCastException.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)cast in interface Transformable<T>public <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 functionpublic <U> Maybe<U> ofType(java.lang.Class<? extends U> type)
Filters
// (1, 2, 3) ReactiveSeq.of(1, "a", 2, "b",3).ofType(Integer.class)
public Maybe<T> filterNot(java.util.function.Predicate<? super T> fn)
Filters
of(1,2,3).filter(i->i>2);
//[1,2]
public Maybe<T> notNull()
Filters
of(1,2,null,4).nonNull();
//[1,2,4]
public static <T,X extends java.lang.Throwable> Try<T,X> failure(X error)
Failure<Exception> failure = Try.failure(new RuntimeException());
error - for Failurepublic static <T,X extends java.lang.Throwable> Try<T,X> success(T value)
Success<Integer> success = Try.success(new RuntimeException());
value - Successful valuepublic static <T,X extends java.lang.Throwable> Try<T,X> success(T value, java.lang.Class<? extends java.lang.Throwable>... classes)
public Xor<X,T> toXorWithError()
public <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 Failurepublic AnyMValue2<Witness.tryType,X,T> anyM()
public T get()
get in interface Convertable<T>get in interface java.util.function.Supplier<T>public void throwException()
public T orElse(T value)
ConvertableorElse in interface Convertable<T>value - Return value supplied if Failure, otherwise return Success valuepublic T orElseGet(java.util.function.Supplier<? extends T> value)
ConvertableOptional.orElseGet(Supplier)
Returns a value generated by the provided Supplier if this Convertable is zero or
contains a null value.orElseGet in interface Convertable<T>value - from supplied Supplier if Failure otherwise return Success valuepublic <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 Transformable<T>map in interface MonadicValue<T>fn - Map success value from T to R. Do nothing if Failure (return this)public <R> Try<R,X> flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends R>> fn)
MonadicValueOptional.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)public 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 Failurepublic Try<T,X> onFail(java.util.function.Consumer<? super X> consumer)
consumer - Accept Exception if present (Failure)public 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 matchpublic Try<T,X> recover(java.util.function.Function<? super X,? extends T> fn)
recover in interface RecoverableFrom<X extends java.lang.Throwable,T>fn - Recovery function - map from a failure to a Success.public Try<T,X> recoverFlatMap(java.util.function.Function<? super X,? extends Try<T,X>> fn)
fn - Recovery FlatMap function. Map from a failure to a Successpublic Try<T,X> recoverFlatMapFor(java.lang.Class<? extends X> t, java.util.function.Function<? super X,? extends Try<T,X>> fn)
public Try<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 functionpublic java.util.Optional<T> toOptional()
toOptional in interface Convertable<T>public ReactiveSeq<T> stream()
public java.util.Optional<X> toFailedOptional()
public java.util.stream.Stream<X> toFailedStream()
public boolean isSuccess()
public boolean isFailure()
public void forEach(java.util.function.Consumer<? super T> consumer)
forEach in interface java.lang.Iterable<T>consumer - Accept value if Success / not called on Failurepublic void forEachFailed(java.util.function.Consumer<? super X> consumer)
consumer - Accept value if Failure / not called on Failurepublic boolean isPresent()
isPresent in interface Convertable<T>public 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 Successpublic Try<T,X> peekFailed(java.util.function.Consumer<? super X> consumer)
consumer - Accept Exception if Failurepublic java.util.Iterator<T> iterator()
@SafeVarargs public 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 recover to executeclasses - Exception types to catch (or java.lang.Exception if none specified)@SafeVarargs public 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 recover to executeclasses - Exception types to catch (or java.lang.Exception if none specified)@SafeVarargs public static <X extends java.lang.Throwable> Try.Init<X> catchExceptions(java.lang.Class<? extends X>... classes)
classes - Exception types to catchpublic static <T,X extends java.lang.Throwable> Try<T,X> flatten(Try<? extends Try<T,X>,X> nested)
public <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 thempublic <T2,R> Try<R,X> zip(java.lang.Iterable<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
public <T2,R> Try<R,X> zipP(org.reactivestreams.Publisher<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
public <U,R> Try<R,X> zipS(java.util.stream.Stream<? extends U> other, java.util.function.BiFunction<? super T,? super U,? extends R> zipper)
Zippablepublic <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> zipS(java.util.stream.Stream<? extends U> other)
Zippablepublic <U> Try<org.jooq.lambda.tuple.Tuple2<T,U>,X> zip(java.lang.Iterable<? extends U> other)
Zippablepublic java.lang.String mkString()
Valuepublic java.lang.String toString()
toString in class java.lang.Objectpublic boolean equals(java.lang.Object o)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic int arity()
arity in interface MonadicValue<T>public static <T,X extends java.lang.Throwable> Try<T,X> narrowK2(Higher2<Witness.tryType,X,T> t)
public static <T,X extends java.lang.Throwable> Try<T,X> narrowK(Higher<Higher<Witness.tryType,X>,T> t)
public static <W1,X extends java.lang.Throwable,T> Nested<Higher<Witness.tryType,X>,W1,T> nested(Try<Higher<W1,T>,X> nested, InstanceDefinitions<W1> def2)
public <W1> Coproduct<W1,Higher<Witness.tryType,X>,T> coproduct(InstanceDefinitions<W1> def2)