Class Result<V>

java.lang.Object
net.orbyfied.coldlib.util.Result<V>
Type Parameters:
V - The value type on success.

public class Result<V> extends Object
Construct to hold the result of an operation which could have thrown an error.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    An exception used by Result when unwrapping the result to signal the absence of a value.
    static class 
    For absent errors.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the captured error if present, or null if absent.
    static <V> Result<V>
    failed(Throwable throwable)
    Create a new result signaling that the operation failed with the given error, and no value is present.
    boolean
    Check if a SUCCESSFUL value is present.
    boolean
    Check if an error occurred.
    orElse(V def)
    Get the value if present or the provided default/fallback value if absent.
    Get the value if present or null if absent.
    Get the captured value if successful, or rethrow the captured error if failed.
    Rethrow the error if present, or exit and return this instance back if absent.
    static <V> Result<V>
    success(V value)
    Create a new success result, with a nullable value.
    Get and return the value if present, or throw with a description if absent.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • success

      public static <V> Result<V> success(V value)
      Create a new success result, with a nullable value. The returned instance will always have a value set and marked as set.
      Type Parameters:
      V - The value type.
      Parameters:
      value - The value (nullable).
      Returns:
      The result instance.
    • failed

      public static <V> Result<V> failed(Throwable throwable)
      Create a new result signaling that the operation failed with the given error, and no value is present. The returned instance will never have a value set or marked as set.
      Type Parameters:
      V - The value type (for compatibility).
      Parameters:
      throwable - The cause.
      Returns:
      The failed result instance.
    • isPresent

      public boolean isPresent()
      Check if a SUCCESSFUL value is present. You can assign a value and a thrown error, which means in some rare cases, entirely based on the source of this result, it can still return false while containing a value.
      Returns:
      If a value was successfully assigned and is present.
    • isSuccess

      public boolean isSuccess()
      Check if an error occurred. This just checks to see if a throwable was captured by this result.
      Returns:
      If a throwable was captured.
    • error

      public Throwable error()
      Returns the captured error if present, or null if absent. The field being set to NO_VALUE is considered absent, so null will be returned.
      Returns:
      The error or null if absent.
    • orNull

      public V orNull()
      Get the value if present or null if absent. This ignores if an and what error was thrown.
      Returns:
      The value or null if absent.
    • orElse

      public V orElse(V def)
      Get the value if present or the provided default/fallback value if absent. This uses isPresent() to check presence.
      Parameters:
      def - The fallback value.
      Returns:
      The value.
    • rethrowFailed

      public Result<V> rethrowFailed()
      Rethrow the error if present, or exit and return this instance back if absent. This uses Throwables.sneakyThrow(Throwable) to rethrow the error.
      Returns:
      This.
    • orRethrow

      public V orRethrow()
      Get the captured value if successful, or rethrow the captured error if failed. This uses Throwables.sneakyThrow(Throwable) to rethrow the error.
      Returns:
      The value.
    • unwrap

      public V unwrap()
      Get and return the value if present, or throw with a description if absent. If the value is simply absent, with no cause (error()), a blank Result.AbsentValueException will be thrown. If there is a cause, an error captured, it will be set as the cause for the Result.AbsentValueException thrown.
      Returns:
      The value.
      Throws:
      Result.AbsentValueException - If no value is present.