Class VoidResult<E>

java.lang.Object
no.gorandalum.fluentresult.VoidResult<E>
Type Parameters:
E - the type of the error value

public final class VoidResult<E> extends Object
A result object which either is in success state with no value, or in error state containing a non-null error value.

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

  • Method Details

    • success

      public static <E> VoidResult<E> success()
      Returns a VoidResult in success state.
      Type Parameters:
      E - the type of the error value
      Returns:
      a VoidResult in success state
    • error

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

      public <N> VoidResult<N> mapError(Function<? super E,? extends N> function)
      If in error state, returns a VoidResult containing the result of applying the given mapping function to the error value, otherwise returns the unaltered VoidResult 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 VoidResult containing the result of applying the mapping function to the error value of this VoidResult, if in error state, otherwise the unaltered VoidResult in success state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • replace

      public <N> Result<N,E> replace(Supplier<? extends N> supplier)
      If in success state, returns a Result containing the value provided by the given supplier, otherwise returns a Result containing the error value of this VoidResult.
      Type Parameters:
      N - the type of the value provided by the supplier
      Parameters:
      supplier - the supplier to provide the value if success state, may not be null
      Returns:
      a Result containing the value provided by the given supplier, if in success state, otherwise a Result containing the error value of this VoidResult
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • replaceWithOptional

      public <N> OptionalResult<N,E> replaceWithOptional(Supplier<Optional<? extends N>> supplier)
      If in success state, returns a OptionalResult containing the optional value provided by given supplier, otherwise returns a OptionalResult containing the error value of this VoidResult.
      Type Parameters:
      N - the type of the value which may be present in the Optional provided by the supplier
      Parameters:
      supplier - the supplier to provide the optional value if success state, may not be null
      Returns:
      a OptionalResult containing the optional value provided by the given supplier, if in success state, otherwise a OptionalResult containing the error value of this VoidResult
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • replaceWithBoolean

      public BooleanResult<E> replaceWithBoolean(Supplier<Boolean> supplier)
      If in success state, returns a BooleanResult containing the boolean value provided by the given supplier, otherwise returns a BooleanResult containing the error value of this VoidResult.
      Parameters:
      supplier - the supplier to provide the boolean value if success state, may not be null
      Returns:
      a BooleanResult containing the boolean value provided by the given supplier, if in success state, otherwise a BooleanResult containing the error value of this VoidResult
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • flatReplace

      public <N> Result<N,E> flatReplace(Supplier<Result<? extends N,? extends E>> supplier)
      If in success state, returns the Result provided by the given supplier, otherwise returns a Result containing the error value of this VoidResult.
      Type Parameters:
      N - the type of success value which may be present in the Result provided by the supplier
      Parameters:
      supplier - the supplier to provide the Result, if success state
      Returns:
      the Result provided by the supplier, if in success state, otherwise a Result containing the error value of this VoidResult
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • flatReplaceToOptionalResult

      public <N> OptionalResult<N,E> flatReplaceToOptionalResult(Supplier<OptionalResult<? extends N,? extends E>> supplier)
      If in success state, returns the OptionalResult provided by the given supplier, otherwise returns an OptionalResult containing the error value of this VoidResult.
      Type Parameters:
      N - the type of success value which may be present in the OptionalResult provided by the supplier
      Parameters:
      supplier - the supplier to provide the OptionalResult, if success state
      Returns:
      the OptionalResult provided by the supplier, if in success state, otherwise an OptionalResult containing the error value of this VoidResult
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • flatReplaceToBooleanResult

      public BooleanResult<E> flatReplaceToBooleanResult(Supplier<BooleanResult<? extends E>> supplier)
      If in success state, returns the BooleanResult provided by the given supplier, otherwise returns a BooleanResult containing the error value of this VoidResult.
      Parameters:
      supplier - the supplier to provide the BooleanResult, if success state
      Returns:
      the BooleanResult provided by the supplier, if in success state, otherwise a BooleanResult containing the error value of this VoidResult
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • flatReplaceToVoidResult

      public VoidResult<E> flatReplaceToVoidResult(Supplier<VoidResult<? extends E>> supplier)
      If in success state, returns the VoidResult provided by the given supplier, otherwise returns the unaltered VoidResult in error state.
      Parameters:
      supplier - the supplier to provide the VoidResult, if success state
      Returns:
      the VoidResult provided by the supplier, if in success state, otherwise the unaltered VoidResult in error state
      Throws:
      NullPointerException - if the given mapping function is null or returns null
    • recover

      public VoidResult<E> recover()
      If in error state, returns a new VoidResult in success state, otherwise returns the unaltered VoidResult in success state.
      Returns:
      a VoidResult in success state
    • flatRecover

      public VoidResult<E> flatRecover(Function<E,VoidResult<? extends E>> function)
      If in error state, returns the VoidResult from applying the given mapping function to the error value, otherwise returns the unaltered VoidResult in success state.
      Parameters:
      function - the mapping function to apply to the error value to convert to a new VoidResult, if error state
      Returns:
      the VoidResult returned from the mapping function, if in error state, otherwise the unaltered VoidResult in success state
    • consumeError

      public VoidResult<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 VoidResult unaltered
      Throws:
      NullPointerException - if the given consumer is null
    • consumeEither

      public VoidResult<E> consumeEither(Runnable successRunnable, Consumer<? super E> errorConsumer)
      If in success state, runs the success-runnable. If in error state, applies the error value to the given error-consumer.
      Parameters:
      successRunnable - the runnable to run if success state
      errorConsumer - the consumer which accepts the error value
      Returns:
      the original VoidResult unaltered
      Throws:
      NullPointerException - if either the given runnable or consumer is null
    • runIfSuccess

      public VoidResult<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 VoidResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runIfError

      public VoidResult<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 VoidResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runEither

      public VoidResult<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 VoidResult unaltered
      Throws:
      NullPointerException - if one of the given runnables is null
    • run

      @Deprecated public VoidResult<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 VoidResult unaltered
      Throws:
      NullPointerException - if the given runnable is null
    • runAlways

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

      public VoidResult<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 VoidResult is returned unaltered. If the supplier returns a VoidResult in error state, a VoidResult containing the error value is returned. If in error state, the original VoidResult is returned unaltered.
      Parameters:
      supplier - the supplier to run
      Returns:
      the original VoidResult unaltered if the given supplier returns success or the original VoidResult is in error state, otherwise a VoidResult containing the error value from the supplier result
      Throws:
      NullPointerException - if the given supplier is null or returns null
    • fold

      public <N> N fold(Supplier<? extends N> valueSupplier, Function<? super E,? extends N> errorFunction)
      Retrieve a value from this VoidResult by folding the states. If in success state, return the value provided by the value-supplier. If in error state, return the value of applying the error-function to the error value.
      Type Parameters:
      N - the type of retrieved value
      Parameters:
      valueSupplier - supplier to provide the 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 either the given supplier or function is null
    • orElseThrow

      public <X extends Throwable> void orElseThrow(Function<? super E,? extends X> function) throws X
      If in success state, does nothing, 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
      Throws:
      X - if in error state
      NullPointerException - if the given function is null or returns null
    • toOptionalResult

      public <N> OptionalResult<N,E> toOptionalResult()
      Transforms this VoidResult to an OptionalResult. If in success state, the OptionalResult will be in empty success state. If in error state, the OptionalResult will be in error state containing the error value from this VoidResult.

      The returned OptionalResult will never have a value.

      Type Parameters:
      N - the type of the success value in the returned OptionalResult, inferred from the variable
      Returns:
      an OptionalResult in empty success state or in error state containing the error value from this VoidResult
    • handle

      public static VoidResult<Exception> handle(CheckedRunnable runnable)
      Handle the given CheckedRunnable. If the CheckedRunnable executes successfully, the VoidResult will be in success state. If the CheckedRunnable throws an exception, the VoidResult will be in error state containing the thrown exception. Note! A custom CheckedRunnable is used here instead of Runnable to allow handling of checked exceptions.
      Parameters:
      runnable - the CheckedRunnable to handle
      Returns:
      a VoidResult either in success state, or in error state containing the exception thrown by the CheckedRunnable
      Throws:
      NullPointerException - if the given runnable is null
    • handle

      public static <E> VoidResult<E> handle(CheckedRunnable runnable, Function<Exception,E> exceptionMapper)
      Handle the given CheckedRunnable. If the CheckedRunnable executes successfully, the VoidResult will be in success state. If the CheckedRunnable throws an exception, the VoidResult will be in error state containing the result after mapping the exception with the given exception mapper function. Note! A custom CheckedRunnable is used here instead of Runnable to allow handling of checked exceptions.
      Type Parameters:
      E - type of the error value after mapping a thrown exception
      Parameters:
      runnable - the CheckedRunnable to handle
      Returns:
      a VoidResult either in success state, or in error state containing the result after mapping the exception thrown by the CheckedRunnable
      Throws:
      NullPointerException - if the given runnable is null or 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