Class Try<T>
- java.lang.Object
-
- pro.horde.os.cutils.value.Try<T>
-
- Type Parameters:
T- type
- All Implemented Interfaces:
Serializable
public abstract class Try<T> extends Object implements Serializable
TheTryclass offers the ability write safe code without focusing on try-catch blocks in the presence of exceptions.There are 3 states that determines the functionality of
Tryfrom the try operation performed.- Successful Operation with result.
- Successful Operation without result.
- Failed Operation.
Requesting for a result on a successful operation which returns no result or failed operation which has not result will throw an
IllegalStateException, using themap(Function)will vary depending on the state of the operation ... and so on. Review the methods description for more information.Furthermore, fatal exceptions aren't handle by
Try:- Since:
- v5
- Author:
- Bobai Kato
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Tget()Use this method to retrieve the try operation result.abstract ThrowablegetCause()Retrieve the Cause of try operation failure.abstract booleanisFailure()Use to check the state of the try operation.abstract booleanisResult()Use to check the state of a successful try operation if or not it has a result.abstract booleanisSuccess()Use to check the stage of the try operation.abstract <M> Try<M>map(Function<? super T,? extends M> mapper)If a try operation return a result, apply the provided mapping function to it, and return and instance ofTrywith the applied result.static <T> Try<T>of(Dealer<? extends T> operation)Accepts aDealertype function which is expected to return a result if operation was successful.static <T> Try<T>of(Executable operation)Accepts aExecutabletype function with no result expected if operation is successful.voidonFailure(Consumer<? super Throwable> exceptionConsumer)If try operations fails, invoke the specified consumer with the exception thrown during the execution, otherwise do nothing.voidonFailureOrElse(Consumer<? super Throwable> exceptionConsumer, Runnable action)Perform further operation on a failed try Exception, if the try succeeds, the second argument block is executed.voidonFailureOrElse(Consumer<? super Throwable> exceptionConsumer, Consumer<? super T> resultConsumer)Perform further operation on a failed try Exception, if the try succeeds, the second argument block is executed.voidonSuccess(Consumer<? super T> resultConsumer)If try is successful, invoke the specified consumer with the operation result, otherwise do nothing.voidonSuccessOrElse(Consumer<? super T> resultConsumer, Runnable action)Perform further operation on successful tryresult, if the try fails the second argument block is executed.voidonSuccessOrElse(Consumer<? super T> resultConsumer, Consumer<? super Throwable> exceptionConsumer)Perform further operation on successful tryresult, if the try fails the second argument block is executed.abstract TorElse(T other)Return the result if try operation is successful and has a result, otherwise returnotherif try operation fails.abstract TorElseGet(Supplier<? extends T> other)Return the result if available after the try operation, otherwise invokeotherand return the result of that invocation.abstract <X extends Throwable>
TorElseThrow(Supplier<? extends X> exceptionSupplier)Return try result if operation was successful, otherwise throw the exception supplied.
-
-
-
Method Detail
-
of
public static <T> Try<T> of(Dealer<? extends T> operation)
Accepts aDealertype function which is expected to return a result if operation was successful.
-
of
public static <T> Try<T> of(Executable operation)
Accepts aExecutabletype function with no result expected if operation is successful.- Type Parameters:
T- variable type- Parameters:
operation- the operation that will be tried, a variable ofExecutabletype.- Returns:
- instance of
Tryeither with aTry.SuccessorTry.Failurestate. - Since:
- v5
-
onSuccess
public void onSuccess(Consumer<? super T> resultConsumer)
If try is successful, invoke the specified consumer with the operation result, otherwise do nothing.- Parameters:
resultConsumer- block of operation to be executed with try result- Since:
- v5
- See Also:
Try.Success.get()
-
onFailure
public void onFailure(Consumer<? super Throwable> exceptionConsumer)
If try operations fails, invoke the specified consumer with the exception thrown during the execution, otherwise do nothing.- Parameters:
exceptionConsumer- operation to be executed if a an exception was thrown- Since:
- v5
- See Also:
Try.Failure.getCause()
-
onSuccessOrElse
public void onSuccessOrElse(Consumer<? super T> resultConsumer, Runnable action)
Perform further operation on successful tryresult, if the try fails the second argument block is executed.NOTE: This method will throw an
IllegalStateExceptionif the try operation executed doesn't return a result. UseTry.Success.isSuccess()instead to check the state of the try operation.- Parameters:
resultConsumer- block of operation ofConsumertype to be executed with try result.action- block to be executed if try operation fails.- Since:
- v5
-
onSuccessOrElse
public void onSuccessOrElse(Consumer<? super T> resultConsumer, Consumer<? super Throwable> exceptionConsumer)
Perform further operation on successful tryresult, if the try fails the second argument block is executed.NOTE: This method will throw an
IllegalStateExceptionif the try operation executed doesn't return a result. UseTry.Success.isSuccess()instead to check the state of the try operation.
-
onFailureOrElse
public void onFailureOrElse(Consumer<? super Throwable> exceptionConsumer, Runnable action)
Perform further operation on a failed try Exception, if the try succeeds, the second argument block is executed.- Parameters:
exceptionConsumer- block of operation ofConsumertype to be executed when the try operation succeeds.action- block to be executed if try operation fails.- Since:
- v5
-
onFailureOrElse
public void onFailureOrElse(Consumer<? super Throwable> exceptionConsumer, Consumer<? super T> resultConsumer)
Perform further operation on a failed try Exception, if the try succeeds, the second argument block is executed.
-
get
public abstract T get()
Use this method to retrieve the try operation result.- Returns:
- try operation result
- Throws:
IllegalStateException- Try state isTry.Successwithout an available result.UnsupportedOperationException- Try state isTry.Failurewhen a try operation fails- Since:
- v5
-
getCause
public abstract Throwable getCause()
Retrieve the Cause of try operation failure.- Returns:
- exception thrown during try operation.
- Throws:
UnsupportedOperationException- if try operation is successful- Since:
- v5
-
map
public abstract <M> Try<M> map(Function<? super T,? extends M> mapper)
If a try operation return a result, apply the provided mapping function to it, and return and instance ofTrywith the applied result.- Type Parameters:
M- The type of the result of the mapping function- Parameters:
mapper- a mapping function to apply to the result is available.- Returns:
- an instance of
Trydescribing the result of applying a mapping function to the result. - Throws:
NullPointerException- if the mapping function is nullIllegalStateException- if a try operation state isTry.Successbut without a result.UnsupportedOperationException- if a try operation state isTry.Failure.
-
orElse
public abstract T orElse(T other)
Return the result if try operation is successful and has a result, otherwise returnotherif try operation fails.- Parameters:
other- the value to be returned if there is no result available, it could also be a null.- Returns:
- the
result, if present, otherwiseother - Throws:
IllegalStateException- if a try was successful but returns no result.
-
orElseGet
public abstract T orElseGet(Supplier<? extends T> other)
Return the result if available after the try operation, otherwise invokeotherand return the result of that invocation.- Parameters:
other- aSupplierblock whose result is returned if try fails- Returns:
- the try result if available otherwise the result of
other.get() - Throws:
NullPointerException- if value is not present andotheris nullIllegalStateException- if a try was successful but returns no result.
-
orElseThrow
public abstract <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X extends Throwable
Return try result if operation was successful, otherwise throw the exception supplied.- Type Parameters:
X- Type of the exception to be thrown- Parameters:
exceptionSupplier- The supplier which will return the exception to be thrown- Returns:
- the present value
- Throws:
X- if there is no value presentIllegalStateException- if a try was successful but returns no result.X extends Throwable
-
isSuccess
public abstract boolean isSuccess()
Use to check the stage of the try operation.- Returns:
- a
Booleandepending on the state:trueif try was successful elsefalseif operation fails.
-
isResult
public abstract boolean isResult()
Use to check the state of a successful try operation if or not it has a result.- Returns:
- a
Booleandepending on the state:trueif try operation was successful and has a result orfalseif operation fails or successful but without a result.
-
isFailure
public abstract boolean isFailure()
Use to check the state of the try operation.- Returns:
- a
Booleandepending on the state:trueif try operation fails elsefalseif operation was successful..
-
-