T
- Value type in the case of success.public interface Try<T> extends Value<T>
Modifier and Type | Interface and Description |
---|---|
static interface |
Try.CheckedConsumer<T>
A Consumer which may throw.
|
static interface |
Try.CheckedFunction<T,R>
A Function which may throw.
|
static interface |
Try.CheckedPredicate<T>
A Predicate which may throw.
|
static interface |
Try.CheckedRunnable
A Runnable which may throw.
|
static interface |
Try.CheckedSupplier<R>
A Supplier which may throw.
|
static class |
Try.Failure<T>
A failed Try.
|
static class |
Try.FatalException
An unchecked wrapper for Fatal exceptions.
|
static class |
Try.NonFatalException
An unchecked wrapper for non-fatal/recoverable exceptions.
|
static class |
Try.Success<T>
A succeeded Try.
|
Modifier and Type | Method and Description |
---|---|
default Try<T> |
andThen(Consumer<? super T> consumer)
Shortcut for
andThenTry(consumer::accept) , see andThenTry(CheckedConsumer) . |
default Try<T> |
andThen(Runnable runnable)
Shortcut for
andThenTry(runnable::run) , see andThenTry(CheckedRunnable) . |
default Try<T> |
andThenTry(Try.CheckedConsumer<? super T> consumer)
Passes the result to the given
consumer if this is a Success . |
default Try<T> |
andThenTry(Try.CheckedRunnable runnable)
Runs the given runnable if this is a
Success , otherwise returns this Failure . |
boolean |
equals(Object o)
Clarifies that values have a proper equals() method implemented.
|
default Try<Throwable> |
failed()
Returns
Success(throwable) if this is a Failure(throwable) , otherwise
a Failure(new NoSuchElementException("Success.failed()")) if this is a Success. |
static <T> Try<T> |
failure(Throwable exception)
Creates a
Try.Failure that contains the given exception . |
default Try<T> |
filter(Predicate<? super T> predicate)
Shortcut for
filterTry(predicate::test) , see filterTry(CheckedPredicate) }. |
default Try<T> |
filterTry(Try.CheckedPredicate<? super T> predicate)
Returns
this if this is a Failure or this is a Success and the value satisfies the predicate. |
default <U> Try<U> |
flatMap(Function<? super T,? extends Try<? extends U>> mapper)
Shortcut for
flatMapTry(mapper::apply) , see flatMapTry(CheckedFunction) . |
default <U> Try<U> |
flatMapTry(Try.CheckedFunction<? super T,? extends Try<? extends U>> mapper)
FlatMaps the value of a Success or returns a Failure.
|
T |
get()
Gets the result of this Try if this is a Success or throws if this is a Failure.
|
Throwable |
getCause()
Gets the cause if this is a Failure or throws if this is a Success.
|
default T |
getOrElseGet(Function<? super Throwable,? extends T> other) |
default <X extends Throwable> |
getOrElseThrow(Function<? super Throwable,X> exceptionProvider) |
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
boolean |
isEmpty()
Checks whether this Try has no result, i.e.
|
boolean |
isFailure()
Checks if this is a Failure.
|
default boolean |
isSingleValued()
A
Try is a single-valued. |
boolean |
isSuccess()
Checks if this is a Success.
|
default Iterator<T> |
iterator()
Returns a rich
javaslang.collection.Iterator . |
default <U> Try<U> |
map(Function<? super T,? extends U> mapper)
Shortcut for
mapTry(mapper::apply) , see mapTry(CheckedFunction) . |
default <U> Try<U> |
mapTry(Try.CheckedFunction<? super T,? extends U> mapper)
Runs the given checked function if this is a
Success ,
passing the result of the current expression to it. |
static <T> Try<T> |
narrow(Try<? extends T> t)
Narrows a widened
Try<? extends T> to Try<T>
by performing a type safe-cast. |
static <T> Try<T> |
of(Try.CheckedSupplier<? extends T> supplier)
Creates a Try of a CheckedSupplier.
|
default Try<T> |
onFailure(Consumer<? super Throwable> action)
Consumes the throwable if this is a Failure.
|
default Try<T> |
onSuccess(Consumer<? super T> action)
Consumes the value if this is a Success.
|
default Try<T> |
orElse(Supplier<? extends Try<? extends T>> supplier) |
default Try<T> |
orElse(Try<? extends T> other) |
default void |
orElseRun(Consumer<? super Throwable> action) |
default Try<T> |
peek(Consumer<? super T> action)
Applies the action to the value of a Success or does nothing in the case of a Failure.
|
default Try<T> |
recover(Function<? super Throwable,? extends T> f)
Returns
this , if this is a Success , otherwise tries to recover the exception of the failure with f ,
i.e. |
default Try<T> |
recoverWith(Function<? super Throwable,? extends Try<? extends T>> f)
Returns
this , if this is a Success, otherwise tries to recover the exception of the failure with f ,
i.e. |
static Try<Void> |
run(Try.CheckedRunnable runnable)
Creates a Try of a CheckedRunnable.
|
static <T> Try<Seq<T>> |
sequence(Iterable<? extends Try<? extends T>> values)
Reduces many
Try s into a single Try by transforming an
Iterable<Try<? extends T>> into a Try<Seq<T>> . |
static <T> Try<T> |
success(T value)
Creates a
Try.Success that contains the given value . |
default Either<Throwable,T> |
toEither()
Converts this
Try to an Either . |
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
default <U> U |
transform(Function<? super Try<T>,? extends U> f)
Transforms this
Try . |
contains, corresponds, eq, exists, forAll, forEach, getOption, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, narrow, out, out, stderr, stdout, stringPrefix, toArray, toCharSeq, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaOptional, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toList, toMap, toOption, toQueue, toRight, toRight, toSet, toStack, toStream, toTree, toTry, toTry, toVector
spliterator
static <T> Try<T> of(Try.CheckedSupplier<? extends T> supplier)
T
- Component typesupplier
- A checked supplierSuccess(supplier.get())
if no exception occurs, otherwise Failure(throwable)
if an
exception occurs calling supplier.get()
.static Try<Void> run(Try.CheckedRunnable runnable)
runnable
- A checked runnableSuccess(null)
if no exception occurs, otherwise Failure(throwable)
if an exception occurs
calling runnable.run()
.static <T> Try<Seq<T>> sequence(Iterable<? extends Try<? extends T>> values)
Try
s into a single Try
by transforming an
Iterable<Try<? extends T>>
into a Try<Seq<T>>
. If any of
the Try
s are Try.Failure
, then this returns a Try.Failure
.T
- type of the Trysvalues
- An Iterable
of Try
sTry
of a Seq
of resultsNullPointerException
- if values
is nullstatic <T> Try<T> success(T value)
T
- Type of the given value
.value
- A value.Success
.static <T> Try<T> failure(Throwable exception)
T
- Component type of the Try
.exception
- An exception.Failure
.static <T> Try<T> narrow(Try<? extends T> t)
Try<? extends T>
to Try<T>
by performing a type safe-cast. This is eligible because immutable/read-only
collections are covariant.T
- Component type of the Try
.t
- A Try
.t
instance as narrowed type Try<T>
.default Try<T> andThen(Consumer<? super T> consumer)
andThenTry(consumer::accept)
, see andThenTry(CheckedConsumer)
.consumer
- A consumerTry
if this is a Failure
or the consumer succeeded, otherwise the
Failure
of the consumption.NullPointerException
- if consumer
is nulldefault Try<T> andThenTry(Try.CheckedConsumer<? super T> consumer)
consumer
if this is a Success
.
The main use case is chaining checked functions using method references:
Try.of(() -> 100)
.andThen(i -> System.out.println(i));
consumer
- A checked consumerTry
if this is a Failure
or the consumer succeeded, otherwise the
Failure
of the consumption.NullPointerException
- if consumer
is nulldefault Try<T> andThen(Runnable runnable)
andThenTry(runnable::run)
, see andThenTry(CheckedRunnable)
.runnable
- A runnableTry
if this is a Failure
or the runnable succeeded, otherwise the
Failure
of the run.NullPointerException
- if runnable
is nulldefault Try<T> andThenTry(Try.CheckedRunnable runnable)
Success
, otherwise returns this Failure
.
The main use case is chaining runnables using method references:
Try.run(A::methodRef).andThen(B::methodRef).andThen(C::methodRef);
Please note that these lines are semantically the same:
Try.run(this::doStuff)
.andThen(this::doMoreStuff)
.andThen(this::doEvenMoreStuff);
Try.run(() -> {
doStuff();
doMoreStuff();
doEvenMoreStuff();
});
runnable
- A checked runnableTry
if this is a Failure
or the runnable succeeded, otherwise the
Failure
of the run.NullPointerException
- if runnable
is nulldefault Try<Throwable> failed()
Success(throwable)
if this is a Failure(throwable)
, otherwise
a Failure(new NoSuchElementException("Success.failed()"))
if this is a Success.default Try<T> filter(Predicate<? super T> predicate)
filterTry(predicate::test)
, see filterTry(CheckedPredicate)
}.predicate
- A predicateTry
instanceNullPointerException
- if predicate
is nulldefault Try<T> filterTry(Try.CheckedPredicate<? super T> predicate)
this
if this is a Failure or this is a Success and the value satisfies the predicate.
Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception occurs testing the predicate.
predicate
- A checked predicateTry
instanceNullPointerException
- if predicate
is nulldefault <U> Try<U> flatMap(Function<? super T,? extends Try<? extends U>> mapper)
flatMapTry(mapper::apply)
, see flatMapTry(CheckedFunction)
.U
- The new component typemapper
- A mapperTry
NullPointerException
- if mapper
is nulldefault <U> Try<U> flatMapTry(Try.CheckedFunction<? super T,? extends Try<? extends U>> mapper)
U
- The new component typemapper
- A mapperTry
NullPointerException
- if mapper
is nullT get()
get
in interface Value<T>
Try.NonFatalException
- if this is a FailureThrowable getCause()
UnsupportedOperationException
- if this is a Successboolean isEmpty()
boolean isFailure()
default boolean isSingleValued()
Try
is a single-valued.isSingleValued
in interface Value<T>
true
boolean isSuccess()
default Iterator<T> iterator()
Value
javaslang.collection.Iterator
.default <U> Try<U> map(Function<? super T,? extends U> mapper)
mapTry(mapper::apply)
, see mapTry(CheckedFunction)
.map
in interface Value<T>
U
- The new component typemapper
- A checked functionTry
NullPointerException
- if mapper
is nulldefault <U> Try<U> mapTry(Try.CheckedFunction<? super T,? extends U> mapper)
Success
,
passing the result of the current expression to it.
If this expression is a Failure
then it'll return a new
Failure
of type R with the original exception.
The main use case is chaining checked functions using method references:
Try.of(() -> 0)
.map(x -> 1 / x); // division by zero
U
- The new component typemapper
- A checked functionTry
NullPointerException
- if mapper
is nulldefault Try<T> onFailure(Consumer<? super Throwable> action)
action
- An exception consumerNullPointerException
- if action
is nulldefault Try<T> onSuccess(Consumer<? super T> action)
action
- A value consumerNullPointerException
- if action
is nulldefault <X extends Throwable> T getOrElseThrow(Function<? super Throwable,X> exceptionProvider) throws X extends Throwable
X extends Throwable
default Try<T> peek(Consumer<? super T> action)
peek
in interface Value<T>
action
- A ConsumerTry
NullPointerException
- if action
is nulldefault Try<T> recover(Function<? super Throwable,? extends T> f)
this
, if this is a Success
, otherwise tries to recover the exception of the failure with f
,
i.e. calling Try.of(() -> f.apply(throwable))
.f
- A recovery function taking a ThrowableTry
NullPointerException
- if f
is nulldefault Try<T> recoverWith(Function<? super Throwable,? extends Try<? extends T>> f)
this
, if this is a Success, otherwise tries to recover the exception of the failure with f
,
i.e. calling f.apply(cause.getCause())
. If an error occurs recovering a Failure, then the new Failure is
returned.f
- A recovery function taking a ThrowableTry
NullPointerException
- if f
is nulldefault Either<Throwable,T> toEither()
Try
to an Either
.Either
default <U> U transform(Function<? super Try<T>,? extends U> f)
Try
.U
- Type of transformation resultf
- A transformationU
NullPointerException
- if f
is nullboolean equals(Object o)
Value
int hashCode()
Value
See Object.hashCode().
Copyright © 2016. All Rights Reserved.