Package io.github.suppierk.java
Class Try.Failure<T>
java.lang.Object
io.github.suppierk.java.Try.Failure<T>
- Type Parameters:
T
- the class of the value
- All Implemented Interfaces:
Try<T>
A container object which contains exception.
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.suppierk.java.Try
Try.Failure<T>, Try.Success<T>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfilter
(ThrowablePredicate<? super T> predicate) <U> Try<U>
flatMap
(ThrowableFunction<? super T, Try<U>> mapper) 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.orElseTry
(ThrowableSupplier<T> supplier) ContinueTry
composition by providing alternative ways to compute desired value
-
Constructor Details
-
Failure
Constructs an instance with the exception present.- Parameters:
exception
- the non-null exception to be present- Throws:
NullPointerException
- if exception is null
-
-
Method Details
-
get
If this isTry.Success
, returns the value, if this isTry.Failure
, throws the exception stored as unchecked.- Specified by:
get
in interfaceTry<T>
- Returns:
- the value held by this
Try.Success
- See Also:
-
isSuccess
public boolean isSuccess() -
isFailure
public boolean isFailure() -
ifSuccess
If a value is present, invoke the specified consumer with the value, otherwise do nothing. -
ifFailure
If an exception is present, invoke the specified consumer with the value, otherwise do nothing. -
ifSuccessOrElse
public 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.- Specified by:
ifSuccessOrElse
in interfaceTry<T>
- 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
.- Specified by:
filter
in interfaceTry<T>
- 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
.- Specified by:
map
in interfaceTry<T>
- 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 toTry.map(ThrowableFunction)
, but the provided mapper is one whose result is already aTry
, and if invoked,flatMap
does not wrap it with an additionalTry
.- Specified by:
flatMap
in interfaceTry<T>
- 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
- Specified by:
toOptional
in interfaceTry<T>
- Returns:
- new
Optional
containing value orOptional.empty()
if there was an exception
-
orElseTry
ContinueTry
composition by providing alternative ways to compute desired value
-