Class Result<Success,Failure>

java.lang.Object
com.goodcode.online.result.Result<Success,Failure>

public class Result<Success,Failure> extends Object
A monad that represents the result of an operation, which can either be a success or a failure.
  • Method Details

    • success

      public static <Success, Failure> Result<Success,Failure> success(Success success)
      Creates a new success result.
      Type Parameters:
      Success - the type of the success result
      Failure - the type of the failure result
      Parameters:
      success - the success result value
      Returns:
      a new Result instance representing success
    • failure

      public static <Success, Failure> Result<Success,Failure> failure(Failure failure)
      Creates a new failure result.
      Type Parameters:
      Success - the type of the success result
      Failure - the type of the failure result
      Parameters:
      failure - the failure result value
      Returns:
      a new Result instance representing failure
    • getSuccess

      public Success getSuccess()
      Gets the success result, or null if this is a failure.
      Returns:
      the success result, or null if this is a failure
    • getFailure

      public Failure getFailure()
      Gets the failure result, or null if this is a success.
      Returns:
      the failure result, or null if this is a success
    • ifSuccess

      public Result<Success,Failure> ifSuccess(Consumer<Success> consumer)
      If this is a success result, applies the given consumer to the success value.
      Parameters:
      consumer - the consumer to apply to the success value
      Returns:
      this Result instance
    • isFailure

      public boolean isFailure()
      Checks if this is a failure result.
      Returns:
      true if this is a failure result, false if it is a success
    • isSuccess

      public boolean isSuccess()
      Checks if this is a success result.
      Returns:
      true if this is a success result, false if it is a failure
    • ifFailure

      public Result<Success,Failure> ifFailure(Consumer<Failure> consumer)
      If this is a failure result, applies the given consumer to the failure value.
      Parameters:
      consumer - the consumer to apply to the failure value
      Returns:
      this Result instance
    • get

      public <Value> Value get(Function<Success,Value> successMapper, Function<Failure,Value> failureMapper)
      Applies the appropriate function to the success or failure value and returns the result.
      Type Parameters:
      Value - the type of the return value
      Parameters:
      successMapper - the function to apply to the success value if this is a success
      failureMapper - the function to apply to the failure value if this is a failure
      Returns:
      the result of applying the appropriate function
    • mapSuccess

      public <NewSuccess> Result<NewSuccess,Failure> mapSuccess(Function<Success,NewSuccess> mapper)
      Maps the success value to a new success value of type <NewSuccess> using the given mapper function, if this is a success result. If it is a failure result, the original failure is retained.
      Type Parameters:
      NewSuccess - the type of the new success value
      Parameters:
      mapper - the function to map the success value
      Returns:
      a new Result instance with the mapped success value, or the original failure
    • mapFailure

      public <NewFailure> Result<Success,NewFailure> mapFailure(Function<Failure,NewFailure> mapper)
      Maps the failure value to a new failure value of type <NewFailure> using the given mapper function, if this is a failure result. If it is a success result, the original success is retained.
      Type Parameters:
      NewFailure - the type of the new failure value
      Parameters:
      mapper - the function to map the failure value
      Returns:
      a new Result instance with the original success or the mapped failure value
    • map

      public <NewSuccess, NewFailure> Result<NewSuccess,NewFailure> map(Function<Success,NewSuccess> successMapper, Function<Failure,NewFailure> failureMapper)
      Maps both the success and failure values to new values using the given mapper functions, depending on whether this is a success or failure result.
      Type Parameters:
      NewSuccess - the type of the new success value
      NewFailure - the type of the new failure value
      Parameters:
      successMapper - the function to map the success value
      failureMapper - the function to map the failure value
      Returns:
      a new Result instance with the mapped success or failure values
    • flatSuccess

      public <NewSuccess> Result<NewSuccess,Failure> flatSuccess(Function<Success,Result<NewSuccess,Failure>> mapper)
      Flat maps the success value to a new Success <NewSuccess> the given mapper function, if this is a success result. If it is a failure result, the original failure is retained.
      Type Parameters:
      NewSuccess - the type of the new success value
      Parameters:
      mapper - the function to flat map the success value
      Returns:
      the result of applying the mapper function if this is a success, or the original failure
    • flatFailure

      public <NewFailure> Result<Success,NewFailure> flatFailure(Function<Failure,Result<Success,NewFailure>> mapper)
      Flat maps the failure value to a new failure <NewFailure> using the given mapper function, if this is a failure result. If it is a success result, the original success is retained.
      Type Parameters:
      NewFailure - the type of the new failure value
      Parameters:
      mapper - the function to flat map the failure value
      Returns:
      the result of applying the mapper function if this is a failure, or the original success
    • flat

      public <NewSuccess, NewFailure> Result<NewSuccess,NewFailure> flat(Function<Success,Result<NewSuccess,NewFailure>> successMapper, Function<Failure,Result<NewSuccess,NewFailure>> failureMapper)
      Flat maps both the success and failure values to new values using the given mapper functions, depending on whether this is a success or failure result.
      Type Parameters:
      NewSuccess - the type of the new success value
      NewFailure - the type of the new failure value
      Parameters:
      successMapper - the function to flat map the success value if this is a success
      failureMapper - the function to flat map the failure value if this is a failure
      Returns:
      the result of applying the appropriate mapper function