Class Result<T>

java.lang.Object
org.klojang.check.extra.Result<T>
Type Parameters:
T - the type of the result value

public final class Result<T> extends Object
A simple value container that explicitly allows the value to be null. This class is meant to be used as the return value of methods that would otherwise return null both as the legitimate outcome of a computation and as a signal that the computation yielded no result. The HashMap class is a well-known example. If its get() method returns null, it is not clear whether the requested key was absent, or whether it was present, but associated with value null. If you wanted to create a Map implementation where this distinction is clear, you could use the Result class and return notAvailable() if the requested key was absent, and Result.of(null) (or nullResult()) if present but null.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if the specified object is a Result that either is this Result or contains the same value.
    get()
    Returns the result.
    int
    Returns the hashcode of the value contained in this Result, or 0 if no result was available.
    <X extends Throwable>
    void
    If available, passes the result to the specified consumer; else does nothing.
    boolean
    Returns true if the operation that produced this Result successfully computed the result.
    boolean
    Returns true if the operation that produced this Result successfully computed a result and the result value was not null.
    boolean
    Returns true if the operation that produced this Result successfully computed a result and the result value was null.
    boolean
    Returns true if the operation that produced this Result could not compute a result.
    boolean
    Returns true if the operation that produced this Result could not compute a result or the result was null.
    static <T> Result<T>
    Returns a special Result instance indicating the absence of a result.
    static <T> Result<T>
    Returns a Result containing null.
    static <T> Result<T>
    of(T value)
    Returns a Result containing the specified value (possibly null).
    orElse(T defaultValue)
    Returns the result value, if available, else the provided default value.
    orElseGet(Supplier<T> supplier)
    Returns this Result if available, else the provided Result.
    Returns a string representation analogous to the one provided by Optional.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static <T> Result<T> of(T value)
      Returns a Result containing the specified value (possibly null).
      Type Parameters:
      T - The type of the result value
      Parameters:
      value - The value
      Returns:
      a Result containing the specified value
    • notAvailable

      public static <T> Result<T> notAvailable()
      Returns a special Result instance indicating the absence of a result.
      Type Parameters:
      T - the type of the result value
      Returns:
      a special Result object indicating the absence of a result
    • nullResult

      public static <T> Result<T> nullResult()
      Returns a Result containing null.
      Type Parameters:
      T - the type of the result value
      Returns:
      a Result containing null
    • get

      public T get() throws NoSuchElementException
      Returns the result. You should have established first that a result value is available or a NoSuchElementException will be thrown.
      Returns:
      the value
      Throws:
      NoSuchElementException - if no result is available
    • isAvailable

      public boolean isAvailable()
      Returns true if the operation that produced this Result successfully computed the result. If so, the result value can be retrieved via the get() method. If not, calling get() method will result in a NoSuchElementException.
      Returns:
      true if a result could be computed
    • isUnavailable

      public boolean isUnavailable()
      Returns true if the operation that produced this Result could not compute a result.
      Returns:
      true if the operation that produced this Result could not compute a result
    • isUnavailableOrNull

      public boolean isUnavailableOrNull()
      Returns true if the operation that produced this Result could not compute a result or the result was null.
      Returns:
      true if a result could either not be computed or it was null
    • isAvailableAndNull

      public boolean isAvailableAndNull()
      Returns true if the operation that produced this Result successfully computed a result and the result value was null.
      Returns:
      true if a result could be computed, and it turned out to be null
    • isAvailableAndNotNull

      public boolean isAvailableAndNotNull()
      Returns true if the operation that produced this Result successfully computed a result and the result value was not null.
      Returns:
      true if a result could be computed and it was a non-null result
    • ifAvailable

      public <X extends Throwable> void ifAvailable(FallibleConsumer<T,X> consumer) throws X
      If available, passes the result to the specified consumer; else does nothing.
      Type Parameters:
      X - the type of the exception thrown by the consumer
      Parameters:
      consumer - the consumer of the result
      Throws:
      X - if the consumer experiences an error
    • orElse

      public T orElse(T defaultValue)
      Returns the result value, if available, else the provided default value.
      Parameters:
      defaultValue - the default value
      Returns:
      the result value, if available, else the provided default value
    • orElseGet

      public T orElseGet(Supplier<T> supplier) throws IllegalArgumentException
      Returns this Result if available, else the provided Result.
      Parameters:
      supplier - the value to return if this Result is notAvailable().
      Returns:
      this instance's value if available; else the value provided by the specified Supplier;
      Throws:
      IllegalArgumentException - if the specified Result is Result.notAvailable()
    • equals

      public boolean equals(Object obj)
      Returns true if the specified object is a Result that either is this Result or contains the same value.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare this instance with
      Returns:
      whether this instance equals the specified object.
    • hashCode

      public int hashCode()
      Returns the hashcode of the value contained in this Result, or 0 if no result was available.
      Overrides:
      hashCode in class Object
      Returns:
      the hashcode of the value contained in this Result, or 0 if no result was available
    • toString

      public String toString()
      Returns a string representation analogous to the one provided by Optional.
      Overrides:
      toString in class Object
      Returns:
      a string representation analogous to the one provided by Optional