Class TryVoid
- java.lang.Object
-
- io.github.oliviercailloux.exceptions.TryVoid
-
public class TryVoid extends java.lang.Object
An instance of this class represents either a “success” or a “failure”, in which case it encapsulates a cause of type
Throwable
.Instances of this class are immutable.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object o2)
Returnstrue
iff the given object is aTry
and, either: this object and the given object are both successes; or this object and the given object are both failures and they encapsulate equal causes.static TryVoid
failure(java.lang.Throwable cause)
Returns a failure encapsulating the given cause.java.lang.Throwable
getCause()
If this object is a failure, returns the cause it encapsulates.int
hashCode()
boolean
isFailure()
Returnstrue
iff this object represents a failure; equivalently, iff it encapsulates a cause.boolean
isSuccess()
Returnstrue
iff this object represents a success.static TryVoid
run(Throwing.Runnable<java.lang.Throwable> runnable)
Attempts to run the given runnable, and returns a success if it succeeds; or a failure encapsulating the exception thrown by the runnable if it threw one.static TryVoid
success()
Returns a success.java.lang.String
toString()
Returns a string representation of this object, suitable for debug.
-
-
-
Method Detail
-
run
public static TryVoid run(Throwing.Runnable<java.lang.Throwable> runnable)
Attempts to run the given runnable, and returns a success if it succeeds; or a failure encapsulating the exception thrown by the runnable if it threw one.- Returns:
- a success iff the given runnable did not throw an exception.
-
success
public static TryVoid success()
Returns a success.
-
failure
public static TryVoid failure(java.lang.Throwable cause)
Returns a failure encapsulating the given cause.
-
isSuccess
public boolean isSuccess()
Returnstrue
iff this object represents a success.- Returns:
true
iffisFailure()
returnsfalse
-
isFailure
public boolean isFailure()
Returnstrue
iff this object represents a failure; equivalently, iff it encapsulates a cause.- Returns:
true
iffisSuccess()
returnsfalse
-
getCause
public java.lang.Throwable getCause() throws java.lang.IllegalStateException
If this object is a failure, returns the cause it encapsulates.- Throws:
java.lang.IllegalStateException
- if this object is a success, thus, encapsulates no cause.- See Also:
isFailure()
-
equals
public boolean equals(java.lang.Object o2)
Returnstrue
iff the given object is aTry
and, either: this object and the given object are both successes; or this object and the given object are both failures and they encapsulate equal causes.- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
Returns a string representation of this object, suitable for debug.- Overrides:
toString
in classjava.lang.Object
-
-