Class 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)
      Returns true iff the given object is a Try 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()
      Returns true iff this object represents a failure; equivalently, iff it encapsulates a cause.
      boolean isSuccess()
      Returns true 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.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • 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()
        Returns true iff this object represents a success.
        Returns:
        true iff isFailure() returns false
      • isFailure

        public boolean isFailure()
        Returns true iff this object represents a failure; equivalently, iff it encapsulates a cause.
        Returns:
        true iff isSuccess() returns false
      • 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)
        Returns true iff the given object is a Try 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 class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Returns a string representation of this object, suitable for debug.
        Overrides:
        toString in class java.lang.Object