Class Error<T>

  • Type Parameters:
    T - Type of value.

    public class Error<T>
    extends java.lang.Object
    Error holds either a value of type T or an exception.

    The following example shows how to use Error to store either a value or an exception.

    
     class Sample {
         // Either returns an int or throws
         static int ReturnValueOrThrow() throws Exception {
             int value = new Random().nextInt(100);
     
             if (value < 50) throw new RuntimeException();
     
             return value; 
         }
     
         public static void Example() throws Exception {
             Error<Integer> value = Error.make(() -> { return ReturnValueOrThrow(); });
    
             if (value.hasValue()) {
                 value.setValue(2 * value.getValue());
                 System.out.println(value.getValue());
             } else { // value.isError()
                 // Get exception from value, can handle or rethrow
                 throw value.getError();
             }
         }
     }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      Error​(java.lang.Exception ex)
      Creates a new instance of Error from the given Exception.
      Error​(T value)
      Creates a new instance of Error from the given value.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <U> Error<U> bind​(java.util.function.Function<T,​Error<U>> func)
      Binds the given function to the Error returning a new Error<U>.
      java.lang.Exception getError()
      Returns the Exception contained in the Error.
      T getValue()
      Returns the value contained in the Error.
      boolean hasValue()
      Returns true if the Error contains a value.
      boolean isError()
      Returns true if the Error contains a Exception.
      static <T> Error<T> make​(ThrowingSupplier<T> supplier)
      Makes an Error from the given ThrowingSupplier.
      static <T> Error<T> makeError​(java.lang.Exception ex)
      Makes an Error from the given Exception.
      static <T> Error<T> makeValue​(T value)
      Makes an Error from the given value.
      <U> Error<U> map​(java.util.function.Function<T,​U> func)
      Maps the given function over the Error returning a new Error<U>.
      void setError​(java.lang.Exception ex)
      Sets the Exception contained in this Error.
      void setValue​(T value)
      Sets the value of this Error.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Error

        public Error​(java.lang.Exception ex)
        Creates a new instance of Error from the given Exception.
        Parameters:
        ex - Exception to initialize Error with.
      • Error

        public Error​(T value)
        Creates a new instance of Error from the given value.
        Parameters:
        value - Value to initialize Error with.
    • Method Detail

      • hasValue

        public boolean hasValue()
        Returns true if the Error contains a value.
        Returns:
        true if the Error contains a value.
      • getValue

        public T getValue()
        Returns the value contained in the Error.
        Returns:
        Value of type T.
      • setValue

        public void setValue​(T value)
        Sets the value of this Error.
        Parameters:
        value - will be placed in the Error.
      • isError

        public boolean isError()
        Returns true if the Error contains a Exception.
        Returns:
        true if the Error contains an Exception.
      • getError

        public java.lang.Exception getError()
        Returns the Exception contained in the Error.
        Returns:
        Exception contained in the Error.
      • setError

        public void setError​(java.lang.Exception ex)
        Sets the Exception contained in this Error.
        Parameters:
        ex - Exception to place in Error.
      • map

        public <U> Error<U> map​(java.util.function.Function<T,​U> func)
        Maps the given function over the Error returning a new Error<U>.
        Type Parameters:
        U - Type of return Error.
        Parameters:
        func - Function to map over the Error.
        Returns:
        New Error after calling the given function.
      • bind

        public <U> Error<U> bind​(java.util.function.Function<T,​Error<U>> func)
        Binds the given function to the Error returning a new Error<U>.
        Type Parameters:
        U - Type of return Error.
        Parameters:
        func - Function to bind to the Error.
        Returns:
        New Error after calling the given function.
      • makeError

        public static <T> Error<T> makeError​(java.lang.Exception ex)
        Makes an Error from the given Exception.
        Type Parameters:
        T - Error value type.
        Parameters:
        ex - Exception to place in Error.
        Returns:
        New Error containing the given Exception.
      • makeValue

        public static <T> Error<T> makeValue​(T value)
        Makes an Error from the given value.
        Type Parameters:
        T - Error value type.
        Parameters:
        value - Value to place in Error.
        Returns:
        New Error containing the given value.
      • make

        public static <T> Error<T> make​(ThrowingSupplier<T> supplier)
        Makes an Error from the given ThrowingSupplier.

        Depending on whether the supplier throws or returns, the resulting Error could either contain a value of type T or an Exception.

        Type Parameters:
        T - Error value type.
        Parameters:
        supplier - Throwing supplier from which to attempt to get value.
        Returns:
        Error containing either a value or an Exception.