Package io.github.suppierk.java.util
Class Try.Success<T>
- java.lang.Object
-
- io.github.suppierk.java.util.Try.Success<T>
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.github.suppierk.java.util.Try
Try.Failure<T>, Try.Success<T>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Try<T>filter(ThrowablePredicate<? super T> predicate)<U> Try<U>flatMap(ThrowableFunction<? super T,Try<U>> mapper)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.Try<T>orElseTry(ThrowableSupplier<T> supplier)ContinueTrycomposition by providing alternative ways to compute desired valuejava.util.Optional<T>toOptional()Convert thisTrytoOptionalwithout preserving exception information.
-
-
-
Constructor Detail
-
Success
protected Success(T value)
Constructs an instance with the value present.- Parameters:
value- the value to be present
-
-
Method Detail
-
get
public T 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:
Try.isSuccess(),Try.isFailure()
-
isSuccess
public boolean isSuccess()
-
isFailure
public boolean isFailure()
-
ifSuccess
public void ifSuccess(ThrowableConsumer<? super T> consumer)
If a value is present, invoke the specified consumer with the value, otherwise do nothing.
-
ifFailure
public void ifFailure(ThrowableConsumer<java.lang.Throwable> consumer)
If an exception is present, invoke the specified consumer with the value, otherwise do nothing.
-
filter
public 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.- 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
public <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.- 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
public <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 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
public java.util.Optional<T> toOptional()
Convert thisTrytoOptionalwithout preserving exception information.- Specified by:
toOptionalin interfaceTry<T>- Returns:
- new
Optionalcontaining value orOptional.empty()if there was an exception
-
-