Class Result.Extensions

java.lang.Object
ru.progrm_jarvis.javacommons.object.Result.Extensions
Enclosing interface:
Result<T,E>

public static final class Result.Extensions extends Object
Extensions for Result providing type-specific specializations which are impossible via generic virtual methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    any(@NotNull Result<? extends T,? extends T> result)
    Either returns the successful result's value ot the error result's value depending on what is present.
    static <T, E extends R, R>
    @NotNull Result<T,R>
    mapErrorIf(@NonNull Result<? extends T,? extends E> result, @NonNull Predicate<? super E> predicate, @NonNull Function<? super E,? extends R> mappingFunction)
    Conditionally maps the error value otherwise applying no changes.
    static <T, E extends R, R>
    @NotNull Result<T,R>
    mapErrorIfNot(@NonNull Result<? extends T,? extends E> result, @NonNull Predicate<? super E> predicate, @NonNull Function<? super E,? extends R> mappingFunction)
    Conditionally maps the error value otherwise applying no changes.
    static <T extends R, E, R>
    @NotNull Result<R,E>
    mapIf(@NonNull Result<? extends T,? extends E> result, @NonNull Predicate<? super T> predicate, @NonNull Function<? super T,? extends R> mappingFunction)
    Conditionally maps the successful value otherwise applying no changes.
    static <T extends R, E, R>
    @NotNull Result<R,E>
    mapIfNot(@NonNull Result<? extends T,? extends E> result, @NonNull Predicate<? super T> predicate, @NonNull Function<? super T,? extends R> mappingFunction)
    Conditionally maps the successful value otherwise applying no changes.
    static <T, X extends Throwable>
    T
    rethrow(@NotNull Result<? extends T,? extends @NotNull X> result)
    Rethrows the exception if it is an error result.
    static <@Any T, @Any E>
    Result<T,E>
    upcast(@NotNull Result<? extends @Any T,? extends @Any E> result)
    Converts the result to another result whose types are super-types of its current types.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • rethrow

      public static <T, X extends Throwable> T rethrow(@NotNull @NotNull Result<? extends T,? extends @NotNull X> result) throws X
      Rethrows the exception if it is an error result.
      Type Parameters:
      T - type of the result
      X - type of the throwable error
      Parameters:
      result - result to be handled
      Returns:
      successful result's value if it was a successful result
      Throws:
      X - if it was an error result
    • upcast

      public static <@Any T, @Any E> Result<T,E> upcast(@NotNull @NotNull Result<? extends @Any T,? extends @Any E> result)

      Converts the result to another result whose types are super-types of its current types.

      This is equivalent to the following:

      
       result
               .map(Function.<RT>identity())
               .mapError(Function.<RE>identity())
       
      Type Parameters:
      T - new (super-) type of the successful result
      E - new (super-) type of the error result
      Parameters:
      result - result whose type should be upcasted
      Returns:
      upcasted result
      API note
      this method could have been an instance method but its type cannot be described due to the absence of the ability to specify super-bounds on generic parameters relative to the other ones
    • any

      public static <T> T any(@NotNull @NotNull Result<? extends T,? extends T> result)
      Either returns the successful result's value ot the error result's value depending on what is present.
      Type Parameters:
      T - type of the resulting value common for the success and error values
      Parameters:
      result - given result
      Returns:
      either the success or the error value
    • mapIf

      @NotNull public static <T extends R, E, R> @NotNull Result<R,E> mapIf(@NonNull @NonNull Result<? extends T,? extends E> result, @NonNull @NonNull Predicate<? super T> predicate, @NonNull @NonNull Function<? super T,? extends R> mappingFunction)
      Conditionally maps the successful value otherwise applying no changes.
      Type Parameters:
      T - type of the successful result value
      E - type of the error result value
      R - type of the resulting successful value, super-type of <T>
      Parameters:
      result - given result
      predicate - condition on which to apply the mapping to the successful value
      mappingFunction - function used to map the successful value if it matches the predicate
      Returns:
      the result whose successful value is mapped if it matches the predicate
    • mapIfNot

      @NotNull public static <T extends R, E, R> @NotNull Result<R,E> mapIfNot(@NonNull @NonNull Result<? extends T,? extends E> result, @NonNull @NonNull Predicate<? super T> predicate, @NonNull @NonNull Function<? super T,? extends R> mappingFunction)
      Conditionally maps the successful value otherwise applying no changes.
      Type Parameters:
      T - type of the successful result value
      E - type of the error result value
      R - type of the resulting successful value, super-type of <T>
      Parameters:
      result - given result
      predicate - condition on which to not apply the mapping to the successful value
      mappingFunction - function used to map the successful value if it does not match the predicate
      Returns:
      the result whose successful value is mapped if it does not match the predicate
    • mapErrorIf

      @NotNull public static <T, E extends R, R> @NotNull Result<T,R> mapErrorIf(@NonNull @NonNull Result<? extends T,? extends E> result, @NonNull @NonNull Predicate<? super E> predicate, @NonNull @NonNull Function<? super E,? extends R> mappingFunction)
      Conditionally maps the error value otherwise applying no changes.
      Type Parameters:
      T - type of the successful result value
      E - type of the error result value
      R - type of the resulting error value, super-type of <E>
      Parameters:
      result - given result
      predicate - condition on which to apply the mapping to the error value
      mappingFunction - function used to map the error value if it matches the predicate
      Returns:
      the result whose error value is mapped if it matches the predicate
    • mapErrorIfNot

      @NotNull public static <T, E extends R, R> @NotNull Result<T,R> mapErrorIfNot(@NonNull @NonNull Result<? extends T,? extends E> result, @NonNull @NonNull Predicate<? super E> predicate, @NonNull @NonNull Function<? super E,? extends R> mappingFunction)
      Conditionally maps the error value otherwise applying no changes.
      Type Parameters:
      T - type of the successful result value
      E - type of the error result value
      R - type of the resulting error value, super-type of <E>
      Parameters:
      result - given result
      predicate - condition on which to not apply the mapping to the error value
      mappingFunction - function used to map the error value if it does not match the predicate
      Returns:
      the result whose error value is mapped if it does not match the predicate