Interface Result<R>

Type Parameters:
R - the result value type
All Superinterfaces:
Serializable

public interface Result<R> extends Serializable
Represents the result of an operation that might fail, such as type conversion. A result may contain either a value, signifying a successful operation, or an error message in case of a failure.

Result instances are created using the factory methods ok(Object) and error(String), denoting success and failure respectively.

Unless otherwise specified, Result method arguments cannot be null.

Since:
1.0
  • Method Details

    • ok

      static <R> Result<R> ok(R value)
      Returns a successful result wrapping the given value.
      Type Parameters:
      R - the result value type
      Parameters:
      value - the result value, can be null
      Returns:
      a successful result
    • error

      static <R> Result<R> error(String message)
      Returns a failure result wrapping the given error message.
      Type Parameters:
      R - the result value type
      Parameters:
      message - the error message
      Returns:
      a failure result
    • of

      static <R> Result<R> of(SerializableSupplier<R> supplier, SerializableFunction<Exception,String> onError)
      Returns a Result representing the result of invoking the given supplier. If the supplier returns a value, returns a Result.ok of the value; if an exception is thrown, returns the message in a Result.error.
      Type Parameters:
      R - the result value type
      Parameters:
      supplier - the supplier to run
      onError - the function to provide the error message
      Returns:
      the result of invoking the supplier
    • map

      default <S> Result<S> map(SerializableFunction<R,S> mapper)
      If this Result has a value, returns a Result of applying the given function to the value. Otherwise, returns a Result bearing the same error as this one. Note that any exceptions thrown by the mapping function are not wrapped but allowed to propagate.
      Type Parameters:
      S - the type of the mapped value
      Parameters:
      mapper - the mapping function
      Returns:
      the mapped result
    • flatMap

      <S> Result<S> flatMap(SerializableFunction<R,Result<S>> mapper)
      If this Result has a value, applies the given Result-returning function to the value. Otherwise, returns a Result bearing the same error as this one. Note that any exceptions thrown by the mapping function are not wrapped but allowed to propagate.
      Type Parameters:
      S - the type of the mapped value
      Parameters:
      mapper - the mapping function
      Returns:
      the mapped result
    • handle

      void handle(SerializableConsumer<R> ifOk, SerializableConsumer<String> ifError)
      Invokes either the first callback or the second one, depending on whether this Result denotes a success or a failure, respectively.
      Parameters:
      ifOk - the function to call if success
      ifError - the function to call if failure
    • ifOk

      default void ifOk(SerializableConsumer<R> consumer)
      Applies the consumer if result is not an error.
      Parameters:
      consumer - consumer to apply in case it's not an error
    • ifError

      default void ifError(SerializableConsumer<String> consumer)
      Applies the consumer if result is an error.
      Parameters:
      consumer - consumer to apply in case it's an error
    • isError

      boolean isError()
      Checks if the result denotes an error.
      Returns:
      true if the result denotes an error, false otherwise
    • getMessage

      Optional<String> getMessage()
      Returns an Optional of the result message, or an empty Optional if none.
      Returns:
      the optional message
    • getOrThrow

      <X extends Throwable> R getOrThrow(SerializableFunction<String,? extends X> exceptionProvider) throws X
      Return the value, if the result denotes success, otherwise throw an exception to be created by the provided supplier.
      Type Parameters:
      X - Type of the exception to be thrown
      Parameters:
      exceptionProvider - The provider which will return the exception to be thrown based on the given error message
      Returns:
      the value
      Throws:
      X - if this result denotes an error