Package io.github.suppierk.java
Interface Try<T>
- Type Parameters:
T
- the class of the value
- All Known Implementing Classes:
Try.Failure
,Try.Success
public interface Try<T>
A container object that contains either a value or exception.
If a value is present, isSuccess()
will return true
and get()
will
return the value. If exception occurred, isFailure()
will return true
and get()
will return first occurred exception.
Additional methods that depend on the presence or absence of a contained value are provided,
such as orElse()
(return a default value if value not present)
and ifSuccess(ThrowableConsumer)
(execute a block of code if the value is present) or
ifFailure(ThrowableConsumer)
(execute a block of code if the exception is present).
This is a value-based class; use of
identity-sensitive operations (including reference equality (==
), identity hash code, or
synchronization) on instances of Try
may have unpredictable results and should be
avoided.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic class
A container object which contains exception.static class
A container object which contains value. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Try<T>
Returns aTry
with the specified exception.filter
(ThrowablePredicate<? super T> predicate) <U> Try<U>
flatMap
(ThrowableFunction<? super T, Try<U>> mapper) static <T> Try<T>
fromOptional
(Optional<T> optional) get()
If this isTry.Success
, returns the value, if this isTry.Failure
, throws the exception stored as unchecked.void
ifFailure
(ThrowableConsumer<Throwable> consumer) If an exception is present, invoke the specified consumer with the value, otherwise do nothing.void
ifSuccess
(ThrowableConsumer<? super T> consumer) If a value is present, invoke the specified consumer with the value, otherwise do nothing.void
ifSuccessOrElse
(ThrowableConsumer<? super T> valueConsumer, ThrowableConsumer<Throwable> throwableConsumer) If a value is present, invoke the specified consumer with the value, otherwise invoke the specified consumer with the exception.boolean
boolean
<U> Try<U>
map
(ThrowableFunction<? super T, ? extends U> mapper) If a value is present, apply the provided mapping function to it, and if the result is non-null, return aTry
describing the result.static <T> Try<T>
of
(ThrowableSupplier<T> supplier) Returns aTry
by invoking specified supplier.default T
Return the value if present, otherwise invokeother
and return the result of that invocation.default T
Return the value if present, otherwise returnother
.orElseTry
(ThrowableSupplier<T> supplier) ContinueTry
composition by providing alternative ways to compute desired valuestatic <T> Try<T>
success
(T value) Returns aTry
with the specified value.
-
Method Details
-
of
Returns aTry
by invoking specified supplier.- Type Parameters:
T
- the class of the value- Parameters:
supplier
- the supplier to retrieve the value- Returns:
- a
Try.Success
with the value retrieved successfully orTry.Failure
-
fromOptional
- Type Parameters:
T
- the class of the value- Parameters:
optional
- theOptional
to be present- Returns:
- a
Try.Success
with the value if it was present otherwise aTry.Failure
withNoSuchElementException
- Throws:
NullPointerException
- if optional is null
-
success
Returns aTry
with the specified value.- Type Parameters:
T
- the class of the value- Parameters:
value
- the value to be present- Returns:
- a
Try.Success
with the value - Throws:
NullPointerException
- if value is null
-
failure
Returns aTry
with the specified exception.- Type Parameters:
T
- the class of the value- Parameters:
throwable
- the value to be present, which must be non-null- Returns:
- a
Try.Failure
with the exception - Throws:
NullPointerException
- if throwable is null
-
get
T get()If this isTry.Success
, returns the value, if this isTry.Failure
, throws the exception stored as unchecked.- Returns:
- the value held by this
Try.Success
- See Also:
-
isSuccess
boolean isSuccess()- Returns:
true
if there is a value present, otherwisefalse
-
isFailure
boolean isFailure()- Returns:
true
if there is an exception present, otherwisefalse
-
ifSuccess
If a value is present, invoke the specified consumer with the value, otherwise do nothing.- Parameters:
consumer
- block to be executed if a value is present
-
ifFailure
If an exception is present, invoke the specified consumer with the value, otherwise do nothing.- Parameters:
consumer
- block to be executed if an exception is present
-
ifSuccessOrElse
void ifSuccessOrElse(ThrowableConsumer<? super T> valueConsumer, ThrowableConsumer<Throwable> throwableConsumer) If a value is present, invoke the specified consumer with the value, otherwise invoke the specified consumer with the exception.- Parameters:
valueConsumer
- block to be executed if a value is presentthrowableConsumer
- block to be executed if an exception is present
-
filter
If a value is present, and the value matches the given predicate, return aTry
describing the value, otherwise return a failedTry
.- Parameters:
predicate
- a predicate to apply to the value, if present- Returns:
- a
Try.Success
describing the value of thisTry
if a value is present and the value matches the given predicate, otherwise aTry.Failure
-
map
If a value is present, apply the provided mapping function to it, and if the result is non-null, return aTry
describing the result. Otherwise, return a failedTry
.- Type Parameters:
U
- The type of the result of the mapping function- Parameters:
mapper
- a mapping function to apply to the value, if present- Returns:
- a
Try.Success
describing the result of applying a mapping function to the value of thisTry
, if a value is present, otherwise aTry.Failure
-
flatMap
If a value is present, apply the providedTry
-bearing mapping function to it, return that result, otherwise return a failedTry
. This method is similar tomap(ThrowableFunction)
, but the provided mapper is one whose result is already aTry
, and if invoked,flatMap
does not wrap it with an additionalTry
.- Type Parameters:
U
- The type parameter to theTry
returned by- Parameters:
mapper
- a mapping function to apply to the value, if present the mapping function- Returns:
- the result of applying a
Try
-bearing mapping function to the value of thisTry
, if a value is present, otherwise aTry.Failure
-
toOptional
- Returns:
- new
Optional
containing value orOptional.empty()
if there was an exception
-
orElseTry
ContinueTry
composition by providing alternative ways to compute desired value- Parameters:
supplier
- to invoke in case of failure.- Returns:
- new
Try
instance
-
orElse
Return the value if present, otherwise returnother
.- Parameters:
other
- the value to be returned if there is a failure, may be null- Returns:
- the value, if present, otherwise
other
-
orElse
Return the value if present, otherwise invokeother
and return the result of that invocation.- Parameters:
other
- aSupplier
whose result is returned if no value is present- Returns:
- the value if present otherwise the result of
other.get()
- Throws:
NullPointerException
- if value is not present andother
is null
-