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.voidifFailure(ThrowableConsumer<Throwable> consumer) If an exception is present, invoke the specified consumer with the value, otherwise do nothing.voidifSuccess(ThrowableConsumer<? super T> consumer) If a value is present, invoke the specified consumer with the value, otherwise do nothing.booleanboolean<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 aTrydescribing the result.orElseTry(ThrowableSupplier<T> supplier) ContinueTrycomposition 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:
getin 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. -
filter
If a value is present, and the value matches the given predicate, return aTrydescribing the value, otherwise return a failedTry.- Specified by:
filterin interfaceTry<T>- Parameters:
predicate- a predicate to apply to the value, if present- Returns:
- a
Try.Successdescribing the value of thisTryif 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 aTrydescribing the result. Otherwise, return a failedTry.- Specified by:
mapin 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.Successdescribing 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,flatMapdoes not wrap it with an additionalTry.- Specified by:
flatMapin interfaceTry<T>- Type Parameters:
U- The type parameter to theTryreturned 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:
toOptionalin interfaceTry<T>- Returns:
- new
Optionalcontaining value orOptional.empty()if there was an exception
-
orElseTry
ContinueTrycomposition by providing alternative ways to compute desired value
-