Interface Result<T,E>

Type Parameters:
T - Value type
E - Error type

public sealed interface Result<T,E>
Monad that can either be an error or a success and holds either a value or an error.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T1,T2,E> Result<T2,E>
    err(Result<T1,E> res)
    Casting function to translate an erroneous result from one value type to another
    static <T,E> Result<T,E>
    err(E err)
    Creates an erroneous result out of the specified err error.
    Gets the result's error.
    <T2> Result<T2,E>
    flatMap(Function<T, Result<T2,E>> mapper)
    If this is a successful result, returns the mapper function's result.
    Gets the result's value or throws a ResultException.
    <X extends Exception>
    T
    getOrThrow(Function<E,X> factory)
    Gets the result's value or throws an exception specified by the factory function.
    ifError(Consumer<E> consumer)
    If this is an erroneous result, the specified consumer function is applied to the result's error value.
    ifSuccess(Consumer<T> consumer)
    If this a successful result, the specified consumer function is applied to the result's value.
    static <T> Result<T, DelphiException>
    Wraps an IO error in a result.
    boolean
    Tests if this result is erroneous.
    boolean
    Tests if this result is successful.
    <T2> Result<T2,E>
    map(Function<T,T2> mapper)
    If a value is present, returns a new result with the return value of the specified mapper function, otherwise, if this is an erroneous result, then nothing happens and this result is cast to the mapped type.
    <E2> Result<T,E2>
    mapError(Function<E,E2> mapper)
    If this is an erroneous result, returns the mapper functions result as a new error result.
    static <E> Result<Nothing,E>
    Get the successful 'nothing' result.
    static <T,E> Result<T,E>
    ok(T value)
    Creates a successful result with the specified non-null value.
    orElse(T defaultValue)
    If the result is an erroneous result, the defaultValue is returned, otherwise the result's value is returned.
    orElseGet(Supplier<T> getter)
    If the result is an erroneous result, the getter function is called to get the return value.
    Gets the value of the result.
  • Method Details

    • err

      static <T,E> Result<T,E> err(E err)
      Creates an erroneous result out of the specified err error.
      Parameters:
      err - Error value
      Returns:
      Created result
      Throws:
      NullPointerException - If err is null
    • err

      static <T1,T2,E> Result<T2,E> err(Result<T1,E> res)
      Casting function to translate an erroneous result from one value type to another
      Parameters:
      res - Result to cast
      Returns:
      Casted result
      Throws:
      IllegalArgumentException - If the specified result is non-erroneous
    • ok

      static <T,E> Result<T,E> ok(T value)
      Creates a successful result with the specified non-null value.
      Parameters:
      value - Value
      Returns:
      Created result
      Throws:
      NullPointerException - If value is null
    • ioError

      static <T> Result<T, DelphiException> ioError(IOException exc)
      Wraps an IO error in a result.
      Returned results
      Exception Types Returned error code
      NoSuchFileException DelphiException.ERR_NO_FILE
      AccessDeniedException DelphiException.ERR_ACCESS_DENIED
      Any other IO exception type DelphiException.ERR_IO_ERROR
      Parameters:
      exc - Exception to wrap
      Returns:
      Error result
    • nothing

      static <E> Result<Nothing,E> nothing()
      Get the successful 'nothing' result. A result which is successful but has no return value.
      Returns:
      Empty successful result.
    • value

      Optional<T> value()
      Gets the value of the result.
      Returns:
      An optional containing the value of this result, if it's not an error, or an empty optional if this is an erroneous result.
    • error

      Optional<E> error()
      Gets the result's error.
      Returns:
      An optional containing the error of this result, or an empty optional, if this is a successful result.
    • map

      <T2> Result<T2,E> map(Function<T,T2> mapper)
      If a value is present, returns a new result with the return value of the specified mapper function, otherwise, if this is an erroneous result, then nothing happens and this result is cast to the mapped type.

      If the mapper function returns null, a NullPointerException is thrown.

      Parameters:
      mapper - Mapping function
      Returns:
      Mapped result.
      Throws:
      NullPointerException - If mapper is null, or if the result of the mapper function is null.
    • flatMap

      <T2> Result<T2,E> flatMap(Function<T, Result<T2,E>> mapper)
      If this is a successful result, returns the mapper function's result. Otherwise, nothing happens and this result is cast to the mapped type and returned.

      If the mapper function return null, a NullPointerException is thrown.

      Parameters:
      mapper - Mapping function
      Returns:
      Mapped result
      Throws:
      NullPointerException - If mapper is null, or if the result of the mapper function is null.
    • mapError

      <E2> Result<T,E2> mapError(Function<E,E2> mapper)
      If this is an erroneous result, returns the mapper functions result as a new error result. Otherwise, nothing happens and this result is returned.

      If the mapper function returns null, a NullPointerException is thrown.

      Parameters:
      mapper - Error mapping function
      Returns:
      Error-mapped result
      Throws:
      NullPointerException - If mapper is null, or if the result of the mapper function is null.
    • isError

      boolean isError()
      Tests if this result is erroneous.
      Returns:
      true, if this result has a present error(), false otherwise.
    • isSuccess

      boolean isSuccess()
      Tests if this result is successful.
      Returns:
      true, if this result has a present value(). false otherwise
    • ifError

      Result<T,E> ifError(Consumer<E> consumer)
      If this is an erroneous result, the specified consumer function is applied to the result's error value. Otherwise, nothing happens.
      Parameters:
      consumer - Error consumer function
      Returns:
      this
    • ifSuccess

      Result<T,E> ifSuccess(Consumer<T> consumer)
      If this a successful result, the specified consumer function is applied to the result's value. Otherwise, nothing happens.
      Parameters:
      consumer - Value consumer function
      Returns:
      this
    • getOrThrow

      T getOrThrow() throws ResultException
      Gets the result's value or throws a ResultException.

      If the result's error is a Throwable then the thrown ResultException will have the throwable as its cause (Accessible with Throwable.getCause()).
      If the result's error is a string, then it will be used as the ResultException's message.
      Otherwise, a result exception will be thrown with a ResultException.getErrorObject() of this result's error value.

      Returns:
      The result's value
      Throws:
      ResultException - If the result is an erroneous result.
    • getOrThrow

      <X extends Exception> T getOrThrow(Function<E,X> factory) throws X
      Gets the result's value or throws an exception specified by the factory function.
      Parameters:
      factory - Exception factory
      Returns:
      The result's value
      Throws:
      X - If the result is an erroneous result.
    • orElse

      T orElse(@Nullable T defaultValue)
      If the result is an erroneous result, the defaultValue is returned, otherwise the result's value is returned.
      Parameters:
      defaultValue - Default value
      Returns:
      Result value, or the specified fallback value, if the result is erroneous.
    • orElseGet

      T orElseGet(Supplier<T> getter)
      If the result is an erroneous result, the getter function is called to get the return value. Otherwise, the result's value is returned.
      Parameters:
      getter - Default value supplier
      Returns:
      Result value, or the specified fallback value, if the result is erroneous.