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 classA container object which contains exception.static classA container object which contains value. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Try<T>Returns aTrywith 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.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.static <T> Try<T>of(ThrowableSupplier<T> supplier) Returns aTryby invoking specified supplier.default TReturn the value if present, otherwise invokeotherand return the result of that invocation.default TReturn the value if present, otherwise returnother.orElseTry(ThrowableSupplier<T> supplier) ContinueTrycomposition by providing alternative ways to compute desired valuestatic <T> Try<T>success(T value) Returns aTrywith the specified value.
-
Method Details
-
of
Returns aTryby invoking specified supplier.- Type Parameters:
T- the class of the value- Parameters:
supplier- the supplier to retrieve the value- Returns:
- a
Try.Successwith the value retrieved successfully orTry.Failure
-
fromOptional
- Type Parameters:
T- the class of the value- Parameters:
optional- theOptionalto be present- Returns:
- a
Try.Successwith the value if it was present otherwise aTry.FailurewithNoSuchElementException - Throws:
NullPointerException- if optional is null
-
success
Returns aTrywith the specified value.- Type Parameters:
T- the class of the value- Parameters:
value- the value to be present- Returns:
- a
Try.Successwith the value - Throws:
NullPointerException- if value is null
-
failure
Returns aTrywith 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.Failurewith 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:
trueif there is a value present, otherwisefalse
-
isFailure
boolean isFailure()- Returns:
trueif 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
-
filter
If a value is present, and the value matches the given predicate, return aTrydescribing the value, otherwise return a failedTry.- 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.- 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 tomap(ThrowableFunction), but the provided mapper is one whose result is already aTry, and if invoked,flatMapdoes not wrap it with an additionalTry.- 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
- Returns:
- new
Optionalcontaining value orOptional.empty()if there was an exception
-
orElseTry
ContinueTrycomposition by providing alternative ways to compute desired value- Parameters:
supplier- to invoke in case of failure.- Returns:
- new
Tryinstance
-
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 invokeotherand return the result of that invocation.- Parameters:
other- aSupplierwhose 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 andotheris null
-