Class Result<T>

java.lang.Object
net.blakez.bppmq.core.Result<T>

public class Result<T> extends Object
Author:
Peter Blakeley : @pblakez pb@blakez.org
  • Constructor Details

    • Result

      protected Result(boolean success, Optional<T> value, List<Error> errors)
      Protected constructor to create a Result instance.
      Parameters:
      success - Indicates if the operation was successful.
      value - An Optional containing the result value (present on success, potentially present on failure).
      errors - A list of errors (empty on success, non-empty on failure).
  • Method Details

    • success

      public static <T> Result<T> success(Optional<T> value)
      Creates a successful Result.
      Type Parameters:
      T - The type of the value.
      Parameters:
      value - An Optional containing the successful result value.
      Returns:
      A new successful Result instance.
    • fail

      public static <T> Result<T> fail(List<Error> errors)
      Creates a failed Result with a list of errors.
      Type Parameters:
      T - The type of the value (will be empty).
      Parameters:
      errors - A list of errors indicating the failure reason.
      Returns:
      A new failed Result instance.
    • fail

      public static <T> Result<T> fail(Error error)
      Creates a failed Result with a single error.
      Type Parameters:
      T - The type of the value (will be empty).
      Parameters:
      error - The error indicating the failure reason.
      Returns:
      A new failed Result instance.
    • fail

      public static <T> Result<T> fail(Optional<T> value, List<Error> errors)
      Creates a failed Result with a list of errors and potentially a partial value.
      Type Parameters:
      T - The type of the value.
      Parameters:
      value - An Optional containing a partial or default value associated with the failure.
      errors - A list of errors indicating the failure reason.
      Returns:
      A new failed Result instance.
    • fail

      public static <T> Result<T> fail(Optional<T> value, Error error)
      Creates a failed Result with a single error and potentially a partial value.
      Type Parameters:
      T - The type of the value.
      Parameters:
      value - An Optional containing a partial or default value associated with the failure.
      error - The error indicating the failure reason.
      Returns:
      A new failed Result instance.
    • value

      public Optional<T> value()
      Gets the value associated with the result.
      Returns:
      An Optional containing the value if present.
    • isSuccess

      public boolean isSuccess()
      Checks if the operation was successful.
      Returns:
      true if the operation succeeded, false otherwise.
    • errors

      public List<Error> errors()
      Gets the list of errors associated with a failed operation.
      Returns:
      An unmodifiable list of errors (empty if successful).
    • primaryError

      public Error primaryError()
      Gets the primary error from the list of errors. If the list is empty (which shouldn't happen for a failed result), returns an unknown error.
      Returns:
      The first Error in the list, or a default unknown error.