Class Ok<V,​E>

  • Type Parameters:
    V - The value type
    E - The error type
    All Implemented Interfaces:
    Result<V,​E>

    public final class Ok<V,​E>
    extends java.lang.Object
    implements Result<V,​E>
    This class represents the Ok side of @{link Result}.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private V value  
    • Constructor Summary

      Constructors 
      Constructor Description
      Ok​(V value)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void accept​(ConsumerWithException<? super V> ok, ConsumerWithException<? super E> err)
      Terminal function that processes the result or the error
      <X> Result<X,​E> asError()  
      private <F> Result<V,​F> coerce()  
      java.util.Optional<E> error()
      Returns the error of this instance as an Optional.
      <U> Result<U,​E> flatMap​(FunctionWithException<? super V,​? extends Result<? extends U,​? extends E>> mapper)
      FlatMap the contained value if this is an Ok value.
      boolean isErr()
      Returns true if this instance represents an Err value, false otherwise.
      boolean isOk()
      Returns true if this instance represents an Ok value, false otherwise.
      <U> Result<U,​E> map​(FunctionWithException<? super V,​? extends U> mapper)
      Map the contained value if this is an Ok value.
      <F> Result<V,​F> mapErr​(FunctionWithException<? super E,​? extends F> mapper)
      Map the contained error if this is an Err value.
      V orElse​(V orElse)
      Returns the contained value if this is an Ok value.
      V orElseGet​(java.util.function.Supplier<? extends V> orElseSupplier)
      Returns the contained value if this is an Ok value.
      <R extends java.lang.Throwable>
      V
      orElseThrow​(FunctionWithException<? super E,​? extends R> throwableSupplier)
      Returns the contained value if this is an Ok value.
      Result<V,​E> recover​(FunctionWithException<? super E,​? extends V> recover)
      Recover the contained error if this is an Err value.
      java.lang.String toString()  
      V unwrap()
      Returns the contained value if this is an Ok value.
      V unwrap​(java.lang.String message)
      Express the expectation that this object is an Ok value.
      <X extends java.lang.Throwable>
      V
      unwrap​(java.util.function.Function<E,​X> constructor)  
      java.util.Optional<V> value()
      Returns the value of this instance as an Optional.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • value

        private final V value
    • Constructor Detail

      • Ok

        Ok​(V value)
        Constructor.
        Parameters:
        value - the value
    • Method Detail

      • isOk

        public boolean isOk()
        Returns true if this instance represents an Ok value, false otherwise.
        Specified by:
        isOk in interface Result<V,​E>
        Returns:
        true if this instance represents an Ok value, false otherwise.
      • isErr

        public boolean isErr()
        Returns true if this instance represents an Err value, false otherwise.
        Specified by:
        isErr in interface Result<V,​E>
        Returns:
        true if this instance represents an Err value, false otherwise.
      • value

        public java.util.Optional<V> value()
        Returns the value of this instance as an Optional. Returns Optional.empty() if this is an Err instance.
        Specified by:
        value in interface Result<V,​E>
        Returns:
        The value of this instance as an Optional. Returns Optional.empty() if this is an Err instance.
      • error

        public java.util.Optional<E> error()
        Returns the error of this instance as an Optional. Returns Optional.empty() if this is an Ok instance.
        Specified by:
        error in interface Result<V,​E>
        Returns:
        The error of this instance as an Optional. Returns Optional.empty() if this is an Ok instance.
      • unwrap

        public V unwrap()
        Returns the contained value if this is an Ok value. Otherwise throws a ResultException.
        Specified by:
        unwrap in interface Result<V,​E>
        Returns:
        The contained value
      • unwrap

        public V unwrap​(java.lang.String message)
                 throws ResultException
        Express the expectation that this object is an Ok value. Otherwise throws a ResultException with the specified message.
        Specified by:
        unwrap in interface Result<V,​E>
        Parameters:
        message - The message to pass to a potential ResultException.
        Throws:
        ResultException - If this is an Err instance.
      • orElse

        public V orElse​(V orElse)
        Returns the contained value if this is an Ok value. Otherwise returns the specified alternate value.
        Specified by:
        orElse in interface Result<V,​E>
        Parameters:
        orElse - The value to return if this is an Err instance.
        Returns:
        The contained value or the alternate value
      • orElseGet

        public V orElseGet​(java.util.function.Supplier<? extends V> orElseSupplier)
        Returns the contained value if this is an Ok value. Otherwise returns the alternate value supplied by the specified supplier.
        Specified by:
        orElseGet in interface Result<V,​E>
        Parameters:
        orElseSupplier - The supplier to supply an alternate value if this is an Err instance. Must not be null.
        Returns:
        The contained value or the alternate value
      • orElseThrow

        public <R extends java.lang.Throwable> V orElseThrow​(FunctionWithException<? super E,​? extends R> throwableSupplier)
                                                      throws R extends java.lang.Throwable
        Returns the contained value if this is an Ok value. Otherwise throws the exception supplied by the specified function.
        Specified by:
        orElseThrow in interface Result<V,​E>
        Type Parameters:
        R - The exception type.
        Parameters:
        throwableSupplier - The supplier to supply an exception if this is an Err instance. Must not be null. The supplier must return a non-null result.
        Returns:
        The contained value.
        Throws:
        R - If this is an Err instance.
        R extends java.lang.Throwable
      • map

        public <U> Result<U,​E> map​(FunctionWithException<? super V,​? extends U> mapper)
        Map the contained value if this is an Ok value. Otherwise return this.
        Specified by:
        map in interface Result<V,​E>
        Type Parameters:
        U - The new value type.
        Parameters:
        mapper - The function to map the contained value into a new value. Must not be null. The function must return a non-null value.
        Returns:
        A result containing the mapped value if this is an Ok value. Otherwise this.
      • coerce

        private <F> Result<V,​F> coerce()
      • mapErr

        public <F> Result<V,​F> mapErr​(FunctionWithException<? super E,​? extends F> mapper)
        Map the contained error if this is an Err value. Otherwise return this.
        Specified by:
        mapErr in interface Result<V,​E>
        Type Parameters:
        F - The new error type.
        Parameters:
        mapper - The function to map the contained error into a new error. Must not be null. The function must return a non-null error.
        Returns:
        A result containing the mapped error if this is an Err value. Otherwise this.
      • flatMap

        public <U> Result<U,​E> flatMap​(FunctionWithException<? super V,​? extends Result<? extends U,​? extends E>> mapper)
        FlatMap the contained value if this is an Ok value. Otherwise return this.
        Specified by:
        flatMap in interface Result<V,​E>
        Type Parameters:
        U - The new value type.
        Parameters:
        mapper - The function to flatmap the contained value into a new result. Must not be null. The function must return a non-null result.
        Returns:
        The flatmapped result if this is an Ok value. Otherwise this.
      • recover

        public Result<V,​E> recover​(FunctionWithException<? super E,​? extends V> recover)
        Recover the contained error if this is an Err value. Otherwise return this.
        Specified by:
        recover in interface Result<V,​E>
        Parameters:
        recover - The function to recover the contained error into a new value. Must not be null. The function must return a non-null value.
        Returns:
        A result containing the new value if this is an Err value. Otherwise this.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • accept

        public void accept​(ConsumerWithException<? super V> ok,
                           ConsumerWithException<? super E> err)
        Description copied from interface: Result
        Terminal function that processes the result or the error
        Specified by:
        accept in interface Result<V,​E>
        Parameters:
        ok - the consumer called when ok
        err - the consumer called when not ok
      • unwrap

        public <X extends java.lang.Throwable> V unwrap​(java.util.function.Function<E,​X> constructor)
        Specified by:
        unwrap in interface Result<V,​E>