Class OptionalResult<T,E>

java.lang.Object
no.gorandalum.fluentresult.OptionalResult<T,E>
Type Parameters:
T - the type of the success value
E - the type of the error value

public final class OptionalResult<T,E> extends Object
A result object which either is in success state, where it may contain a non-null success value or be empty, or it may be in error state containing a non-null error value.

A variable whose type is OptionalResult should never itself be null, it should always point to an OptionalResult instance.

  • Method Details

    • success

      public static <T, E> OptionalResult<T,E> success(Optional<? extends T> maybeValue)
      Returns an OptionalResult in success state containing the given non-null value as success value.
      Type Parameters:
      T - the type of the success value which may be present in the given Optional
      E - the type of the error value
      Parameters:
      maybeValue - an Optional which may contain an success value, or may be empty
      Returns:
      an OptionalResult in success state which either contains a success value or is empty
      Throws:
      NullPointerException - if given Optional is null
    • success

      public static <T, E> OptionalResult<T,E> success(T value)
      Returns an OptionalResult in success state containing the given non-null value as success value.
      Type Parameters:
      T - the type of the success value
      E - the type of the error value
      Parameters:
      value - the success value, which must be non-null
      Returns:
      an OptionalResult in success state containing the given success value
      Throws:
      NullPointerException - if given success value is null
    • successNullable

      public static <T, E> OptionalResult<T,E> successNullable(T value)
      Returns an OptionalResult in success state either containing the given value as success value, or empty if the given value is null.
      Type Parameters:
      T - the type of the success value
      E - the type of the error value
      Parameters:
      value - the success value, which may be null
      Returns:
      an OptionalResult in success state containing the given success value if not null, otherwise an empty OptionalResult
    • empty

      public static <T, E> OptionalResult<T,E> empty()
      Returns an OptionalResult in success state, which is empty with no success value.
      Type Parameters:
      T - the type of the success value
      E - the type of the error value
      Returns:
      an empty OptionalResult in success state
    • error

      public static <T, E> OptionalResult<T,E> error(E value)
      Returns an OptionalResult in error state containing the given non-null value as error value.
      Type Parameters:
      T - the type of the success value
      E - the type of the error value
      Parameters:
      value - the error value, which must be non-null
      Returns:
      an OptionalResult in error state containing the given error value
      Throws:
      NullPointerException - if given error value is null
    • map

      public <N> Result<N,E> map(Function<Optional<T>,? extends N> function)
      If in success state, returns a Result containing the result of applying the given mapping function to the optional success value of this OptionalResult, otherwise returns a Result containing the error value of this OptionalResult.
      Type Parameters:
      N - the type of the value returned from the mapping function
      Parameters:
      function - the mapping function to apply to the optional success value, if success state
      Returns:
      a Result containing the result of applying the mapping function to the optional success value of this OptionalResult, if in success state, otherwise a Result containing the error value of this OptionalResult
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • mapToOptional

      public <N> OptionalResult<N,E> mapToOptional(Function<Optional<T>,? extends Optional<? extends N>> function)
      If in success state, returns a OptionalResult containing the result of applying the given mapping function to the optional success value, otherwise returns the unaltered OptionalResult in error state.
      Type Parameters:
      N - the type of the success value which may be present in the Optional returned from the mapping function
      Parameters:
      function - the mapping function to apply to the optional success value, if success state
      Returns:
      a OptionalResult containing the result of applying the mapping function to the optional success value of this OptionalResult, if in success state, otherwise the unaltered OptionalResult in error state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • mapToBoolean

      public BooleanResult<E> mapToBoolean(Function<Optional<T>,Boolean> function)
      If in success state, returns a BooleanResult containing the result of applying the given mapping function to the optional success value, otherwise returns a BooleanResult containing the error value of this OptionalResult.
      Parameters:
      function - the mapping function to apply to the optional success value, if success state
      Returns:
      a BooleanResult containing the result of applying the mapping function to the optional success value of this OptionalResult, if in success state, otherwise a BooleanResult containing the error value of this OptionalResult
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • mapError

      public <N> OptionalResult<T,N> mapError(Function<? super E,? extends N> function)
      If in error state, returns a OptionalResult containing the result of applying the given mapping function to the error value, otherwise returns the unaltered OptionalResult in success state.
      Type Parameters:
      N - the type of the value returned from the mapping function
      Parameters:
      function - the mapping function to apply to the error value, if error state
      Returns:
      a OptionalResult containing the result of applying the mapping function to the error value of this OptionalResult, if in error state, otherwise the unaltered OptionalResult in success state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • mapValue

      public <N> OptionalResult<N,E> mapValue(Function<? super T,? extends N> function)
      If in success state with a success value, returns an OptionalResult containing the result of applying the given mapping function to the success value, otherwise returns the unaltered OptionalResult which may be empty or in error state.

      If the given mapping function returns null, then the returned OptionalResult will be empty.

      Type Parameters:
      N - the type of the value returned from the mapping function
      Parameters:
      function - the mapping function to apply to the success value, if success state with a success value
      Returns:
      an OptionalResult containing the result of applying the mapping function to the success value of this OptionalResult, if in success state with a success value, otherwise the unaltered OptionalResult which may be empty or in error state
      Throws:
      NullPointerException - if the given mapping function is null
    • mapValueToOptional

      public <N> OptionalResult<N,E> mapValueToOptional(Function<? super T,Optional<N>> function)
      If in success state with a success value, returns an OptionalResult containing the result of applying the given mapping function to the success value, otherwise returns the unaltered OptionalResult which may be empty or in error state.

      If the given mapping function returns a non-empty Optional, then the returned OptionalResult will contain the Optional content as success value. If the given mapping function returns an empty Optional, then the returned OptionalResult will also be empty.

      Type Parameters:
      N - the type of the success value which may be present in the Optional returned from the mapping function
      Parameters:
      function - the mapping function to apply to the success value, if success state with a success value
      Returns:
      an OptionalResult containing the result of applying the mapping function to the success value of this OptionalResult, if in success state with a success value, otherwise the unaltered OptionalResult which may be empty or in error state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • flatMap

      public <N> Result<N,E> flatMap(Function<Optional<T>,Result<? extends N,? extends E>> function)
      If in success state, returns the Result from applying the given mapping function to the optional success value, otherwise returns a Result containing the error value of this OptionalResult.
      Type Parameters:
      N - the type of success value which may be present in the Result returned by the mapping function
      Parameters:
      function - the mapping function to apply to the optional success value, if success state
      Returns:
      the Result returned from the mapping function, if in success state, otherwise a Result containing the error value of this OptionalResult
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • flatMapToOptionalResult

      public <N> OptionalResult<N,E> flatMapToOptionalResult(Function<Optional<T>,OptionalResult<? extends N,? extends E>> function)
      If in success state, returns the OptionalResult from applying the given mapping function to the optional success value, otherwise returns the unaltered OptionalResult in error state.
      Type Parameters:
      N - the type of success value which may be present in the OptionalResult returned by the mapping function
      Parameters:
      function - the mapping function to apply to the optional success value, if success state
      Returns:
      the OptionalResult returned from the mapping function, if in success state, otherwise the unaltered OptionalResult in error state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • flatReplaceEmpty

      public <N> OptionalResult<N,E> flatReplaceEmpty(Supplier<OptionalResult<N,E>> supplier)
      If in empty success state, returns the OptionalResult from applying the given supplier, otherwise returns the unaltered OptionalResult in success state with value or error state.
      Type Parameters:
      N - the type of success value which may be present in the OptionalResult returned by this function.
      Parameters:
      supplier - the supplier to call if in empty success state.
      Returns:
      the OptionalResult returned from the supplier, if in empty state, otherwise the unaltered OptionalResult in success state with value or error state
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • flatReplaceEmptyWithResult

      public <N> Result<N,E> flatReplaceEmptyWithResult(Supplier<Result<N,E>> supplier)
      If in empty success state, returns the Result from applying the given supplier, otherwise returns a Result with the existing success value or error value.
      Type Parameters:
      N - the type of success value which may be present in the Result returned by this function.
      Parameters:
      supplier - the supplier to call if in empty success state.
      Returns:
      the Result returned from the supplier, if in empty state, otherwise a Result with the existing success value or error value.
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • flatMapToBooleanResult

      public BooleanResult<E> flatMapToBooleanResult(Function<Optional<? extends T>,BooleanResult<? extends E>> function)
      If in success state, returns the BooleanResult from applying the given mapping function to the optional success value, otherwise returns a BooleanResult containing the error value of this OptionalResult.
      Parameters:
      function - the mapping function to apply to the optional success value, if success state
      Returns:
      the BooleanResult returned from the mapping function, if in success state, otherwise a BooleanResult containing the error value of this OptionalResult
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • flatMapToVoidResult

      public VoidResult<E> flatMapToVoidResult(Function<Optional<? extends T>,VoidResult<? extends E>> function)
      If in success state, returns the VoidResult from applying the given mapping function to the optional success value, otherwise returns a VoidResult containing the error value of this OptionalResult.
      Parameters:
      function - the mapping function to apply to the optional success value, if success state
      Returns:
      the VoidResult returned from the mapping function, if in success state, otherwise a VoidResult containing the error value of this OptionalResult
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • flatMapValueWithResult

      public <N> OptionalResult<N,E> flatMapValueWithResult(Function<? super T,Result<? extends N,? extends E>> function)
      If in success state with a success value, returns an OptionalResult from applying the given mapping function to the success value, otherwise returns the unaltered OptionalResult which may be empty or in error state.

      If the Result returned from the mapping function is in success state. the returned OptionalResult will contain the success value from the Result. If the Result is in error state, the returned OptionalResult will contain the error value from the Result.

      Type Parameters:
      N - the type of success value which may be present in the OptionalResult after applying the mapping function
      Parameters:
      function - the mapping function to apply to the success value, if success state with a success value
      Returns:
      an OptionalResult after applying the mapping function, if in success state with a success value, otherwise the unaltered OptionalResult which may be empty or in error state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • flatMapValueWithOptionalResult

      public <N> OptionalResult<N,E> flatMapValueWithOptionalResult(Function<? super T,OptionalResult<? extends N,? extends E>> function)
      If in success state with a success value, returns the OptionalResult from applying the given mapping function to the success value, otherwise returns the unaltered OptionalResult which may be empty or in error state.
      Type Parameters:
      N - the type of success value which may be present in the OptionalResult returned by the mapping function
      Parameters:
      function - the mapping function to apply to the success value, if success state with a success value
      Returns:
      the OptionalResult returned from the mapping function, if in success state with a success value, otherwise the unaltered OptionalResult which may be empty or in error state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • flatMapValueWithBooleanResult

      public OptionalResult<Boolean,E> flatMapValueWithBooleanResult(Function<? super T,BooleanResult<? extends E>> function)
      If in success state with a success value, returns an OptionalResult from applying the given mapping function to the success value, otherwise returns the unaltered OptionalResult which may be empty or in error state.

      If the BooleanResult returned from the mapping function is in success state. the returned OptionalResult will contain the boolean success value from the BooleanResult. If the BooleanResult is in error state, the returned OptionalResult will contain the error value from the BooleanResult.

      Parameters:
      function - the mapping function to apply to the success value, if success state with a success value
      Returns:
      an OptionalResult after applying the mapping function, if in success state with a success value, otherwise the unaltered OptionalResult which may be empty or in error state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • recover

      public OptionalResult<T,E> recover(Function<E,Optional<T>> function)
      If in error state, returns a OptionalResult with the success value from applying the given mapping function to the error value, otherwise returns the unaltered OptionalResult in success state.
      Parameters:
      function - the mapping function to apply to the error value to convert to a new success value, if error state
      Returns:
      A OptionalResult containing the value from the mapping function, if in error state, otherwise the unaltered OptionalResult in success state
    • flatRecover

      public <N> OptionalResult<N,E> flatRecover(Function<E,OptionalResult<? extends N,? extends E>> function)
      If in error state, returns the OptionalResult from applying the given mapping function to the error value, otherwise returns the unaltered OptionalResult in success state.
      Type Parameters:
      N - the type of success value which may be present in the OptionalResult returned by the mapping function
      Parameters:
      function - the mapping function to apply to the error value to convert to a new OptionalResult, if error state
      Returns:
      the OptionalResult returned from the mapping function, if in error state, otherwise the unaltered OptionalResult in success state
    • consume

      public OptionalResult<T,E> consume(Consumer<Optional<T>> consumer)
      If in success state, applies the optional success value to the given consumer, otherwise does nothing.
      Parameters:
      consumer - the consumer which accepts the optional success value
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given consumer is null
    • consumeValue

      public OptionalResult<T,E> consumeValue(Consumer<T> consumer)
      If in success state with a success value, applies the success value to the given consumer, otherwise does nothing.
      Parameters:
      consumer - the consumer which accepts the success value
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given consumer is null
    • consumeError

      public OptionalResult<T,E> consumeError(Consumer<? super E> errorConsumer)
      If in error state, applies the error value to the given consumer, otherwise does nothing.
      Parameters:
      errorConsumer - the consumer which accepts the error value
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given consumer is null
    • consumeEither

      public OptionalResult<T,E> consumeEither(Consumer<Optional<T>> successConsumer, Consumer<? super E> errorConsumer)
      If in success state, applies the optional success value to the given success consumer. If in error state, applies the error value to the given error consumer.
      Parameters:
      successConsumer - the consumer which accepts the optional success value
      errorConsumer - the consumer which accepts the error value
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if one of the given consumers is null
    • consumeEither

      public OptionalResult<T,E> consumeEither(Consumer<? super T> valueConsumer, Runnable emptyRunnable, Consumer<? super E> errorConsumer)
      If in success state with a success value, applies the success value to the given value consumer. If empty, run the given empty runnable. If in error state, applies the error value to the given error consumer.
      Parameters:
      valueConsumer - the consumer which accepts the optional success value
      emptyRunnable - the runnable to run if empty
      errorConsumer - the consumer which accepts the error value
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if one of the given consumers or runnable is null
    • flatConsume

      public OptionalResult<T,E> flatConsume(Function<Optional<T>,? extends VoidResult<? extends E>> function)
      If in success state, applies the optional success value to the given function. If the function returns a VoidResult in success state, the original OptionalResult is returned unaltered. If the function returns a VoidResult in error state, a OptionalResult containing the error value is returned. If in error state, the original OptionalResult is returned unaltered.
      Parameters:
      function - the function which accepts the optional success value
      Returns:
      the original OptionalResult unaltered if the given function returns success or the original OptionalResult is in error state, otherwise a OptionalResult containing the error value from the function result
      Throws:
      NullPointerException - if the given function is null or returns null
    • flatConsumeValue

      public OptionalResult<T,E> flatConsumeValue(Function<T,? extends VoidResult<? extends E>> function)
      If in success state, applies the success value to the given function. If the function returns a VoidResult in success state, the original OptionalResult is returned unaltered. If the function returns a VoidResult in error state, a OptionalResult containing the error value is returned. If in empty or error state, the original OptionalResult is returned unaltered.
      Parameters:
      function - the function which accepts the success value
      Returns:
      the original OptionalResult unaltered if the given function returns success or the original OptionalResult is in empty or error state, otherwise a OptionalResult containing the error value from the function result
      Throws:
      NullPointerException - if the given function is null or returns null
    • runIfSuccess

      public OptionalResult<T,E> runIfSuccess(Runnable runnable)
      If in success state, runs the given runnable, otherwise does nothing.
      Parameters:
      runnable - the runnable to run if success state
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runIfValue

      public OptionalResult<T,E> runIfValue(Runnable runnable)
      If in success state with a success value, runs the given runnable, otherwise does nothing.
      Parameters:
      runnable - the runnable to run if success state with a success value
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runIfNoValue

      public OptionalResult<T,E> runIfNoValue(Runnable runnable)
      If in empty success state or error state, runs the given runnable, otherwise does nothing.
      Parameters:
      runnable - the runnable to run if empty success state or error state
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runIfEmpty

      public OptionalResult<T,E> runIfEmpty(Runnable runnable)
      If in empty success state with no success value, runs the given runnable, otherwise does nothing.
      Parameters:
      runnable - the runnable to run if empty success state
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runIfError

      public OptionalResult<T,E> runIfError(Runnable runnable)
      If in error state, runs the given runnable, otherwise does nothing.
      Parameters:
      runnable - the runnable to run if error state
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runEither

      public OptionalResult<T,E> runEither(Runnable successRunnable, Runnable errorRunnable)
      If in success state, runs the given success runnable. If in error state, runs the given error runnable.
      Parameters:
      successRunnable - the runnable to run if success state
      errorRunnable - the runnable to run if error state
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if one of the given runnables is null
    • runEither

      public OptionalResult<T,E> runEither(Runnable valueRunnable, Runnable emptyRunnable, Runnable errorRunnable)
      If in success state with a success value, runs the given value runnable. If empty, runs the given empty runnable. If in error state, runs the given error runnable.
      Parameters:
      valueRunnable - the runnable to run if success state with a success value
      emptyRunnable - the runnable to run if empty
      errorRunnable - the runnable to run if error state
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if one of the given runnables is null
    • run

      @Deprecated public OptionalResult<T,E> run(Runnable runnable)
      Deprecated.
      use runAlways(java.lang.Runnable) instead for clarity
      Runs the given runnable, no matter the state.
      Parameters:
      runnable - the runnable to run
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runAlways

      public OptionalResult<T,E> runAlways(Runnable runnable)
      Runs the given runnable, no matter the state.
      Parameters:
      runnable - the runnable to run
      Returns:
      the original OptionalResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • flatRunIfSuccess

      public OptionalResult<T,E> flatRunIfSuccess(Supplier<? extends VoidResult<? extends E>> supplier)
      If in success state, runs the given supplier. If the supplier returns a VoidResult in success state, the original OptionalResult is returned unaltered. If the supplier returns a VoidResult in error state, a OptionalResult containing the error value is returned. If in error state, the original OptionalResult is returned unaltered.
      Parameters:
      supplier - the supplier to run
      Returns:
      the original OptionalResult unaltered if the given supplier returns success or the original OptionalResult is in error state, otherwise a OptionalResult containing the error value from the supplier result
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • flatRunIfValue

      public OptionalResult<T,E> flatRunIfValue(Supplier<? extends VoidResult<? extends E>> supplier)
      If in success state with a success value, runs the given supplier. If the supplier returns a VoidResult in success state, the original OptionalResult is returned unaltered. If the supplier returns a VoidResult in error state, a OptionalResult containing the error value is returned. If in error state or empty, the original OptionalResult is returned unaltered.
      Parameters:
      supplier - the supplier to run
      Returns:
      the original OptionalResult unaltered if the given supplier returns success or the original OptionalResult is in empty or error state, otherwise a OptionalResult containing the error value from the supplier result
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • verify

      public OptionalResult<T,E> verify(Predicate<Optional<T>> predicate, Supplier<? extends E> errorSupplier)
      If in success state, verifies the optional success value of this OptionalResult by testing it with the given predicate. If the predicate evaluates to false, a new OptionalResult is returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or the OptionalResult already was in error state, the original OptionalResult is returned unaltered.
      Parameters:
      predicate - the predicate used to verify the optional success value, if success state
      errorSupplier - supplier providing the error if predicate evaluates to false
      Returns:
      the original OptionalResult unaltered, unless the predicate evaluates to false, then a new OptionalResult in error state is returned containing the supplied error value
      Throws:
      NullPointerException - if the given predicate is null or returns null, or the given error supplier is null or returns null
    • verify

      public OptionalResult<T,E> verify(Function<Optional<T>,? extends VoidResult<? extends E>> function)
      If in success state, verifies the success value of this OptionalResult by mapping it to a VoidResult. If the returned VoidResult is in error state, a new OptionalResult is returned containing the error value of the VoidResult. If the VoidResult is in success state, or the OptionalResult already was in error state, the original OptionalResult is returned unaltered.
      Parameters:
      function - the function applied to the success value, if success state
      Returns:
      the original OptionalResult unaltered, unless the VoidResult returned by the mapping function is in error state, then a new OptionalResult in error state is returned containing the error value from the VoidResult
      Throws:
      NullPointerException - if the given function is null or returns null
    • verifyValue

      public OptionalResult<T,E> verifyValue(Predicate<? super T> predicate, Supplier<? extends E> errorSupplier)
      If in success state with a success value, verifies the success value of this OptionalResult by testing it with the given predicate. If the predicate evaluates to false, a new OptionalResult is returned containing the error value provided by the given error supplier. If the predicate evaluates to true, or the OptionalResult already was empty or in error state, the original OptionalResult is returned unaltered.
      Parameters:
      predicate - the predicate used to verify the success value, if success state with a success value
      errorSupplier - supplier providing the error if predicate evaluates to false
      Returns:
      the original OptionalResult unaltered, unless the predicate evaluates to false, then a new OptionalResult in error state is returned containing the supplied error value
      Throws:
      NullPointerException - if the given predicate is null or returns null, or the given error supplier is null or returns null
    • verifyValue

      public OptionalResult<T,E> verifyValue(Function<? super T,? extends VoidResult<? extends E>> function)
      If in non-empty success state, verifies the success value of this OptionalResult by mapping it to a VoidResult. If the returned VoidResult is in error state, a new OptionalResult is returned containing the error value of the VoidResult. If the VoidResult is in success state, or the OptionalResult already was empty or in error state, the original OptionalResult is returned unaltered.
      Parameters:
      function - the function applied to the success value, if non-empty success state
      Returns:
      the original OptionalResult unaltered, unless the VoidResult returned by the mapping function is in error state, then a new OptionalResult in error state is returned containing the error value from the VoidResult
      Throws:
      NullPointerException - if the given function is null or returns null
    • fold

      public <N> N fold(Function<Optional<T>,? extends N> successFunction, Function<? super E,? extends N> errorFunction)
      Retrieve a value from this OptionalResult by folding the states. If in success state, return the value of applying the success function to the optional success value. If in error state, return the value of applying the error function to the error value.
      Type Parameters:
      N - the type of the retrieved value
      Parameters:
      successFunction - the mapping function to apply to the optional success value, if success state, may return null
      errorFunction - the mapping function to apply to the error value, if error state, may return null
      Returns:
      the folded value mapped from either the success value or error value, may be null
      Throws:
      NullPointerException - if one of the given functions is null
    • fold

      public <N> N fold(Function<? super T,? extends N> valueFunction, Supplier<? extends N> emptySupplier, Function<? super E,? extends N> errorFunction)
      Retrieve a value from this OptionalResult by folding the states. If in success state with a success value, return the value of applying the value function to the success value. If empty, return the value provided by the empty-supplier. If in error state, return the value of applying the error function to the error value.
      Type Parameters:
      N - the type of the retrieved value
      Parameters:
      valueFunction - the mapping function to apply to the success value, if success state, may return null
      emptySupplier - the supplier to provide the value if empty, may return null
      errorFunction - the mapping function to apply to the error value, if error state, may return null
      Returns:
      the folded value mapped from either the success value or error value, may be null
      Throws:
      NullPointerException - if one of the given functions or the supplier is null
    • orElse

      public Optional<T> orElse(Optional<T> other)
      If in success state, returns the optional success value, otherwise returns other.
      Parameters:
      other - the value to be returned, if not in success state, may not be null
      Returns:
      the optional success value, if success state, otherwise other
      Throws:
      NullPointerException - if the other value is null
    • valueOrElse

      public T valueOrElse(T other)
      If in success state with a success value, returns the success value, otherwise returns other.
      Parameters:
      other - the value to be returned, if not in success state, may be null
      Returns:
      the success value, if success state with a success value, otherwise other
    • orElseGet

      public Optional<T> orElseGet(Function<? super E,? extends Optional<T>> function)
      If in success state, returns the optional success value, otherwise returns the value returned from the given function.
      Parameters:
      function - the mapping function to apply to the error value, if not in success state, it may not return null
      Returns:
      the optional success value, if success state, otherwise the result returned from the given function
      Throws:
      NullPointerException - if the given function is null or returns null
    • valueOrElseGet

      public T valueOrElseGet(Supplier<? extends T> supplier)
      If in success state with a success value, returns the success value, otherwise returns the value returned from the given function.
      Parameters:
      supplier - the supplier providing the return value, if not in success state with a success value, it may return null
      Returns:
      the success value, if success state with a success value, otherwise the result returned from the given function
      Throws:
      NullPointerException - if the given function is null
    • orElseThrow

      public <X extends Throwable> Optional<T> orElseThrow(Function<? super E,? extends X> function) throws X
      If in success state, returns the optional success value, otherwise throws the exception returned by the given function.
      Type Parameters:
      X - type of the exception to be thrown
      Parameters:
      function - the mapping function producing an exception by applying the error value, if not in success state
      Returns:
      the optional success value, if success state
      Throws:
      X - if in error state
      NullPointerException - if the given function is null or returns null
    • valueOrElseThrow

      public <X extends Throwable> T valueOrElseThrow(Supplier<? extends X> supplier) throws X
      If in success state with a success value, returns the success value, otherwise throws the exception returned by the given function.
      Type Parameters:
      X - type of the exception to be thrown
      Parameters:
      supplier - the supplier providing the return value, if not in success state with a success value
      Returns:
      the success value, if success state with success value
      Throws:
      X - if empty or in error state
      NullPointerException - if the given function is null or returns null
    • toResult

      public Result<T,E> toResult(Supplier<? extends E> errorSupplier)
      Transforms this OptionalResult to a Result. If in non-empty succes state, the Result will be in success state containing the success value of this OptionalResult. If empty success state, the Result will be in error state, containing the error value supplied by the given error supplier. If in error state, the Result will be in error state containing the error value of this OptionalResult.
      Parameters:
      errorSupplier - supplier providing the error value if empty success state
      Returns:
      a Result either in success state containing the success value from this OptionalResult, or in error state containing either the error value from the given error supplier or the error value present in this OptionalResult
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • toVoidResult

      public VoidResult<E> toVoidResult()
      Transforms this OptionalResult to a VoidResult. If in success state, the VoidResult will be in success state. If in error state, the VoidResult will be in error state containing the error value from this OptionalResult.
      Returns:
      a VoidResult either in success state or in error state containing the error value from this OptionalResult
    • handle

      public static <T> OptionalResult<T,Exception> handle(Callable<Optional<T>> callable)
      Handle the given Callable. If the Callable executes successfully, the OptionalResult will be in success state containing the returned value. If the Callable throws an exception, the OptionalResult will be in error state containing the thrown exception.
      Type Parameters:
      T - type of the return value of the Callable
      Parameters:
      callable - the Callable to handle
      Returns:
      a OptionalResult either in success state containing the value from the Callable, or in error state containing the exception thrown by the Callable
      Throws:
      NullPointerException - if the given callable is null or returns null
    • handle

      public static <T, E> OptionalResult<T,E> handle(Callable<Optional<T>> callable, Function<Exception,E> exceptionMapper)
      Handle the given Callable. If the Callable executes successfully, the OptionalResult will be in success state containing the returned value. If the Callable throws an exception, the OptionalResult will be in error state containing the result after mapping the exception with the given exception mapper function.
      Type Parameters:
      T - type of the return value of the Callable
      E - type of the error value after mapping a thrown exception
      Parameters:
      callable - the Callable to handle
      Returns:
      a OptionalResult either in success state containing the value from the Callable, or in error state containing the result after mapping the exception thrown by the Callable
      Throws:
      NullPointerException - if the given callable is null or returns null, or if the given exception mapper function is null or returns null
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object