Class Results

java.lang.Object
com.leakyabstractions.result.core.Results

public class Results extends Object
This class consists exclusively of static methods that return Result instances.
Author:
Guillermo Calvo
See Also:
  • Method Details

    • success

      public static <S, F> Result<S,F> success(S success)
      Creates a new successful Result.
      Type Parameters:
      S - the success type of the Result
      F - the failure type of the Result
      Parameters:
      success - the success value
      Returns:
      a successful Result holding success
      Throws:
      NullPointerException - if success is null
    • failure

      public static <S, F> Result<S,F> failure(F failure)
      Creates a new failed Result.
      Type Parameters:
      S - the success type of the Result
      F - the failure type of the Result
      Parameters:
      failure - the failure value
      Returns:
      a failed Result holding failure
      Throws:
      NullPointerException - if failure is null
    • ofNullable

      public static <S, F> Result<S,F> ofNullable(S success, F failure)
      Creates a new Result based on a possibly-null success value and a non-null failure value.
      Type Parameters:
      S - the success type of the Result
      F - the failure type of the Result
      Parameters:
      success - the success value to use if non-null
      failure - the failure value to use if success is null
      Returns:
      a successful Result holding success if non-null; otherwise, a failed Result holding failure
      Throws:
      NullPointerException - if both success and failure are null
    • ofNullable

      public static <S, F> Result<S,F> ofNullable(S success, Supplier<? extends F> failureSupplier)
      Creates a new Result based on a possibly-null success value and a failure Supplier.
      Type Parameters:
      S - the success type of the Result
      F - the failure type of the Result
      Parameters:
      success - the success value to use if non-null
      failureSupplier - the Supplier that produces a failure value
      Returns:
      a successful Result holding success if non-null; otherwise, a failed Result holding the value produced by failureSupplier
      Throws:
      NullPointerException - if both success and failureSupplier are null, or if failureSupplier returns null
    • ofOptional

      public static <S, F> Result<S,F> ofOptional(Optional<S> success, F failure)
      Creates a new Result based on an Optional success value and a non-null failure value.
      Type Parameters:
      S - the success type of the Result
      F - the failure type of the Result
      Parameters:
      success - the Optional success value to use if non-empty
      failure - the failure value to use if success is empty
      Returns:
      a successful Result holding the Optional success value if non-empty; otherwise, a failed Result holding failure
      Throws:
      NullPointerException - if success is null; or if success is empty and failure is null
    • ofOptional

      public static <S, F> Result<S,F> ofOptional(Optional<S> success, Supplier<? extends F> failureSupplier)
      Creates a new Result based on an Optional success value and a failure Supplier.
      Type Parameters:
      S - the success type of the result
      F - the failure type of the result
      Parameters:
      success - the Optional success value to use if non-empty
      failureSupplier - the Supplier that produces a failure value
      Returns:
      a successful Result holding the Optional success value if non-empty; otherwise, a failed Result holding the value produced by failureSupplier
      Throws:
      NullPointerException - if success is null; or if success is empty and failureSupplier is null or returns null
    • ofCallable

      public static <S> Result<S,Exception> ofCallable(Callable<? extends S> task)
      Creates a new Result based on a Callable task.
      Type Parameters:
      S - the success type of the Result
      Parameters:
      task - the Callable that produces a success value, or throws an Exception if unable to do so
      Returns:
      a successful Result holding the value produced by task if it completed as intended; otherwise a failed Result holding the Exception thrown by task
      Throws:
      NullPointerException - if task is null or returns null
    • ofCallable

      public static <S, T> Result<S,Exception> ofCallable(Callable<? extends T> task, Function<? super T,? extends S> mapper)
      Creates a new Result based on a Callable task and a mapping Function.
      Type Parameters:
      S - the success type of the Result
      T - the type returned by the task
      Parameters:
      task - the Callable that produces a possibly-null value, or throws an Exception if unable to do so
      mapper - the mapping Function that transforms the value produced by task into a success value, or throws an Exception if unable to do so
      Returns:
      a successful Result holding the value produced by task and transformed by mapper if both completed as intended; otherwise a failed Result holding the Exception thrown by either one
      Throws:
      NullPointerException - if either task or mapper is null, or if mapper returns null