Package io.github.suppierk.java.util
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 returntrueandget()will return the value. If exception occurred,isFailure()will returntrueandget()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) andifSuccess(ThrowableConsumer)(execute a block of code if the value is present) orifFailure(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 ofTrymay have unpredictable results and should be avoided.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classTry.Failure<T>A container object which contains exception.static classTry.Success<T>A container object which contains value.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <T> Try<T>failure(java.lang.Throwable throwable)Returns aTrywith the specified exception.Try<T>filter(ThrowablePredicate<? super T> predicate)<U> Try<U>flatMap(ThrowableFunction<? super T,Try<U>> mapper)static <T> Try<T>fromOptional(java.util.Optional<T> optional)Returns aTrycreated from specifiedOptional.Tget()If this isTry.Success, returns the value, if this isTry.Failure, throws the exception stored as unchecked.voidifFailure(ThrowableConsumer<java.lang.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.booleanisFailure()booleanisSuccess()<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 TorElse(java.util.function.Supplier<? extends T> other)Return the value if present, otherwise invokeotherand return the result of that invocation.default TorElse(T other)Return the value if present, otherwise returnother.Try<T>orElseTry(ThrowableSupplier<T> supplier)ContinueTrycomposition by providing alternative ways to compute desired valuestatic <T> Try<T>success(T value)Returns aTrywith the specified value.java.util.Optional<T>toOptional()Convert thisTrytoOptionalwithout preserving exception information.
-
-
-
Method Detail
-
of
static <T> Try<T> of(ThrowableSupplier<T> supplier)
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
static <T> Try<T> fromOptional(java.util.Optional<T> optional)
Returns aTrycreated from specifiedOptional.- 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:
java.lang.NullPointerException- if optional is null
-
success
static <T> Try<T> success(T value)
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:
java.lang.NullPointerException- if value is null
-
failure
static <T> Try<T> failure(java.lang.Throwable throwable)
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:
java.lang.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(),isFailure()
-
isSuccess
boolean isSuccess()
- Returns:
trueif there is a value present, otherwisefalse
-
isFailure
boolean isFailure()
- Returns:
trueif there is an exception present, otherwisefalse
-
ifSuccess
void ifSuccess(ThrowableConsumer<? super T> consumer)
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
void ifFailure(ThrowableConsumer<java.lang.Throwable> consumer)
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
Try<T> filter(ThrowablePredicate<? super T> predicate)
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
<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. 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
<U> Try<U> flatMap(ThrowableFunction<? super T,Try<U>> mapper)
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
java.util.Optional<T> toOptional()
Convert thisTrytoOptionalwithout preserving exception information.- Returns:
- new
Optionalcontaining value orOptional.empty()if there was an exception
-
orElseTry
Try<T> orElseTry(ThrowableSupplier<T> supplier)
ContinueTrycomposition by providing alternative ways to compute desired value- Parameters:
supplier- to invoke in case of failure.- Returns:
- new
Tryinstance
-
orElse
default T orElse(T other)
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
default T orElse(java.util.function.Supplier<? extends T> other)
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:
java.lang.NullPointerException- if value is not present andotheris null
-
-