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>, Value<T>, Unit<T>, Transformable<T>, Filters<T>, Sealed2<T,X>, OrElseValue<T,Try<T,X>>, Higher2<DataWitness.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.withResources(()->new BufferedReader(new FileReader("file.txt")),
this::read,
FileNotFoundException.class,IOException.class)
.map(this::processData)
.recover(e->"public);
By public Try does not catch exception within it's operators such as transform / flatMap, to catch Exceptions in ongoing operations use @see success(Object, Class...)
Try.success(2, RuntimeException.class)
.map(i->{throw new RuntimeException();});
//Failure[RuntimeException]
Modifier and Type | Class and Description |
---|---|
static interface |
Try.CheckedBiFunction<T1,T2,R,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> |
Constructor and Description |
---|
Try() |
Modifier and Type | Method and Description |
---|---|
int |
arity() |
Either<X,T> |
asEither() |
Try<T,X> |
bipeek(java.util.function.Consumer<? super T> c1,
java.util.function.Consumer<? super X> c2) |
<R> Try<R,X> |
coflatMap(java.util.function.Function<? super Try<T,X>,R> mapper) |
static <T> Try<T,java.lang.Throwable> |
CofromPublisher(org.reactivestreams.Publisher<T> pub)
Construct a Try that contains a single value extracted from the supplied reactive-streams Publisher
|
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)
|
Option<X> |
failureGet() |
Maybe<T> |
filter(java.util.function.Predicate<? super T> p)
Keep only elements for which the supplied predicates hold
e.g.
|
Try<T,X> |
filter(java.util.function.Predicate<? super T> test,
java.util.function.Function<? super T,? extends X> errorGenerator) |
Option<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 Try<? extends R,X>> fn) |
<R> Try<R,X> |
flatMapOrCatch(Try.CheckedFunction<? super T,? extends Try<? extends R,X>,X> fn,
java.lang.Class<? extends X>... classes)
Perform a flatMapping operation that may catch the supplied Exception types
The supplied Exception types are only applied during this map operation
|
static <T,X extends java.lang.Throwable> |
flatten(Try<? extends Try<T,X>,X> nested) |
<R> R |
fold(java.util.function.Function<? super T,? extends R> fn1,
java.util.function.Function<? super X,? extends R> fn2) |
<R> R |
fold(java.util.function.Function<? super T,? extends R> present,
java.util.function.Supplier<? extends R> absent) |
void |
forEach(java.util.function.Consumer<? super T> consumer) |
<R1,R> Try<R,X> |
forEach2(java.util.function.Function<? super T,? extends Try<R1,X>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction) |
<T2,R1,R2,R> |
forEach3(java.util.function.Function<? super T,? extends Try<R1,X>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends Try<R2,X>> value2,
Function3<? super T,? super R1,? super R2,? extends R> yieldingFunction) |
<T2,R1,R2,R3,R> |
forEach4(java.util.function.Function<? super T,? extends Try<R1,X>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends Try<R2,X>> value2,
Function3<? super T,? super R1,? super R2,? extends Try<R3,X>> value3,
Function4<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction) |
void |
forEachFailed(java.util.function.Consumer<? super X> consumer) |
static <T,X extends java.lang.Throwable> |
fromEither(Either<X,T> pub) |
static <T,X extends java.lang.Throwable> |
fromIterable(java.lang.Iterable<T> iterable,
T alt)
Construct a Try that contains a single value extracted from the supplied Iterable
|
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
|
Option<T> |
get() |
int |
hashCode() |
boolean |
isFailure() |
boolean |
isPresent() |
boolean |
isSuccess() |
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
|
<XR extends java.lang.Throwable> |
mapFailure(java.util.function.Function<? super X,? extends XR> fn) |
<R> Try<R,X> |
mapOrCatch(Try.CheckedFunction<? super T,? extends R,X> fn,
java.lang.Class<? extends X>... classes)
Perform a mapping operation that may catch the supplied Exception types
The supplied Exception types are only applied during this map operation
|
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<DataWitness.tryType,X>,T> t) |
static <T,X extends java.lang.Throwable> |
narrowK2(Higher2<DataWitness.tryType,X,T> t) |
Option<T> |
notNull()
Filter elements retaining only values which are not null
|
<U> Option<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) |
T |
orElseGet(java.util.function.Supplier<? extends T> 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) |
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,java.lang.Throwable> |
retry(java.util.function.Function<? super T,? extends R> fn)
Retry a transformation if it fails.
|
<R> Try<R,java.lang.Throwable> |
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() |
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) |
static <X extends java.lang.Throwable,T,R> |
tailRec(T initial,
java.util.function.Function<? super T,? extends Try<? extends Either<T,R>,X>> fn) |
Either<X,T> |
toEither() |
Either<X,T> |
toEitherWithError() |
Option<X> |
toFailedOption() |
java.util.stream.Stream<X> |
toFailedStream() |
Ior<X,T> |
toIor() |
java.util.Optional<T> |
toOptional() |
java.lang.String |
toString() |
Trampoline<Either<X,T>> |
toTrampoline() |
Try<T,java.lang.Throwable> |
toTry() |
<T> Try<T,X> |
unit(T value) |
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> |
withExceptions(java.lang.Class<? extends X>... toCatch) |
static <T extends java.lang.AutoCloseable,R> |
withResources(CheckedSupplier<T> rs,
CheckedFunction<? super T,? extends R> fn) |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,R> |
withResources(CheckedSupplier<T1> rs1,
CheckedSupplier<T2> rs2,
CheckedBiFunction<? super T1,? super T2,? extends R> fn) |
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,R,X extends java.lang.Throwable> |
withResources(Try.CheckedSupplier<T1,X> rs1,
Try.CheckedSupplier<T2,X> rs2,
Try.CheckedBiFunction<? super T1,? super T2,? extends R,X> fn,
java.lang.Class<? extends X>... classes) |
static <T extends java.lang.AutoCloseable,R,X extends java.lang.Throwable> |
withResources(Try.CheckedSupplier<T,X> rs,
Try.CheckedFunction<? super T,? extends R,X> fn,
java.lang.Class<? extends X>... classes) |
<T2,R> Try<R,X> |
zip(Either<X,T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn) |
<T2,R> Try<R,X> |
zip(Ior<X,T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn) |
<T2,R> Try<R,X> |
zip(Try<T2,X> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn) |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
orElseUse, orElseUse
asSupplier, collect, fold, generate, iterate, nonEmptyList, nonEmptyListGet, print, print, printErr, printOut, toEither, toLazyEither, toLeft, toMaybe, toOption, toRight, toTry, toTry
public final int arity()
@SafeVarargs public final Try<T,X> withExceptions(java.lang.Class<? extends X>... toCatch)
public static <X extends java.lang.Throwable,T,R> Try<R,X> tailRec(T initial, java.util.function.Function<? super T,? extends Try<? extends Either<T,R>,X>> fn)
public static <X extends java.lang.Throwable,T> Higher<Higher<DataWitness.tryType,X>,T> widen(Try<T,X> narrow)
public Trampoline<Either<X,T>> toTrampoline()
public <R> Try<R,java.lang.Throwable> retry(java.util.function.Function<? super T,? extends R> fn)
fn
- Function to retry if failspublic <R> Try<R,java.lang.Throwable> retry(java.util.function.Function<? super T,? extends R> fn, int retries, long delay, java.util.concurrent.TimeUnit timeUnit)
fn
- Function to retry if failsretries
- Number of retriesdelay
- Delay in TimeUnitstimeUnit
- TimeUnit to use for delaypublic void subscribe(org.reactivestreams.Subscriber<? super T> sub)
public 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> stream = Spouts.of(1,2,3);
Try<Integer,Throwable> recover = Try.fromPublisher(stream, RuntimeException.class);
//Try[1]
pub
- Publisher to extract value frompublic static <T> Try<T,java.lang.Throwable> CofromPublisher(org.reactivestreams.Publisher<T> pub)
ReactiveSeq<Integer> stream = Spouts.of(1,2,3);
Try<Integer,Throwable> recover = Try.fromPublisher(stream);
//Try[1]
pub
- Publisher to extract value frompublic static <T,X extends java.lang.Throwable> Try<T,X> fromIterable(java.lang.Iterable<T> iterable, T alt)
ReactiveSeq<Integer> stream = ReactiveSeq.of(1,2,3);
Try<Integer,Throwable> recover = Try.fromIterable(stream);
//Try[1]
iterable
- Iterable to extract value frompublic <T2,R1,R2,R3,R> Try<R,X> forEach4(java.util.function.Function<? super T,? extends Try<R1,X>> value1, java.util.function.BiFunction<? super T,? super R1,? extends Try<R2,X>> value2, Function3<? super T,? super R1,? super R2,? extends Try<R3,X>> value3, Function4<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
public <T2,R1,R2,R> Try<R,X> forEach3(java.util.function.Function<? super T,? extends Try<R1,X>> value1, java.util.function.BiFunction<? super T,? super R1,? extends Try<R2,X>> value2, Function3<? super T,? super R1,? super R2,? extends R> yieldingFunction)
public <R1,R> Try<R,X> forEach2(java.util.function.Function<? super T,? extends Try<R1,X>> value1, java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
public <U> Option<U> ofType(java.lang.Class<? extends U> type)
Filters
// (1, 2, 3) ReactiveSeq.of(1, "a", 2, "b",3).ofType(Integer.class)
public Option<T> filterNot(java.util.function.Predicate<? super T> fn)
Filters
of(1,2,3).filter(i->i>2);
//[1,2]
public Option<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 value@SafeVarargs public static <T,X extends java.lang.Throwable> Try<T,X> success(T value, java.lang.Class<? extends java.lang.Throwable>... classes)
public <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>
fn
- Transformation function@SafeVarargs public final <R> Try<R,X> mapOrCatch(Try.CheckedFunction<? super T,? extends R,X> fn, java.lang.Class<? extends X>... classes)
R
- return type of mapping functionfn
- mapping functionclasses
- exception types to catchpublic <XR extends java.lang.Throwable> Try<T,XR> mapFailure(java.util.function.Function<? super X,? extends XR> fn)
public <R> Try<R,X> flatMap(java.util.function.Function<? super T,? extends Try<? extends R,X>> fn)
fn
- FlatMap success value or do nothing if Failure (return this)@SafeVarargs public final <R> Try<R,X> flatMapOrCatch(Try.CheckedFunction<? super T,? extends Try<? extends R,X>,X> fn, java.lang.Class<? extends X>... classes)
R
- return type of mapping functionfn
- flatMapping functionclasses
- exception types to catchpublic Maybe<T> filter(java.util.function.Predicate<? super T> p)
Filters
of(1,2,3).filter(i->i>2);
//[3]
public Try<T,X> filter(java.util.function.Predicate<? super T> test, java.util.function.Function<? super T,? extends X> errorGenerator)
public 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 Exception to handleconsumer
- Accept Exception if present (Failure) and if class types foldpublic 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 - transform 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)
@SafeVarargs public static <T extends java.lang.AutoCloseable,R,X extends java.lang.Throwable> Try<R,X> withResources(Try.CheckedSupplier<T,X> rs, Try.CheckedFunction<? super T,? extends R,X> fn, java.lang.Class<? extends X>... classes)
@SafeVarargs public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,R,X extends java.lang.Throwable> Try<R,X> withResources(Try.CheckedSupplier<T1,X> rs1, Try.CheckedSupplier<T2,X> rs2, Try.CheckedBiFunction<? super T1,? super T2,? extends R,X> fn, java.lang.Class<? extends X>... classes)
public static <T extends java.lang.AutoCloseable,R> Try<R,java.lang.Throwable> withResources(CheckedSupplier<T> rs, CheckedFunction<? super T,? extends R> fn)
public static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,R> Try<R,java.lang.Throwable> withResources(CheckedSupplier<T1> rs1, CheckedSupplier<T2> rs2, CheckedBiFunction<? super T1,? super T2,? extends R> 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 fold againstfn
- Recovery functionpublic java.util.Optional<T> toOptional()
toOptional
in interface Value<T>
public ReactiveSeq<T> stream()
public java.util.stream.Stream<X> toFailedStream()
public boolean isSuccess()
public boolean isFailure()
public void forEach(java.util.function.Consumer<? super T> consumer)
public void forEachFailed(java.util.function.Consumer<? super X> consumer)
consumer
- Accept value if Failure / not called on Failurepublic <T2,R> Try<R,X> zip(Try<T2,X> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
public <T2,R> Try<R,X> zip(Either<X,T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
public <T2,R> Try<R,X> zip(Ior<X,T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
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)public <R> R fold(java.util.function.Function<? super T,? extends R> fn1, java.util.function.Function<? super X,? extends R> fn2)
public <R> R fold(java.util.function.Function<? super T,? extends R> present, java.util.function.Supplier<? extends R> absent)
public Try<T,X> bipeek(java.util.function.Consumer<? super T> c1, java.util.function.Consumer<? super X> c2)
public static <T,X extends java.lang.Throwable> Try<T,X> flatten(Try<? extends Try<T,X>,X> nested)
public java.lang.String mkString()
Value
public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public static <T,X extends java.lang.Throwable> Try<T,X> narrowK2(Higher2<DataWitness.tryType,X,T> t)
public static <T,X extends java.lang.Throwable> Try<T,X> narrowK(Higher<Higher<DataWitness.tryType,X>,T> t)