Class Result.Extensions
java.lang.Object
ru.progrm_jarvis.javacommons.object.Result.Extensions
Extensions for Result providing type-specific specializations which are impossible via generic virtual methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TEither 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 theerror valueotherwise 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 theerror valueotherwise 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 thesuccessful valueotherwise 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 thesuccessful valueotherwise applying no changes.static <T,X extends Throwable>
TRethrows the exception if it is anerror result.Converts the result to another result whose types are super-types of its current types.
-
Method Details
-
rethrow
public static <T,X extends Throwable> T rethrow(@NotNull @NotNull Result<? extends T, ? extends @NotNull X> result) throws XRethrows the exception if it is anerror result.- Type Parameters:
T- type of the resultX- 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 anerror 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 resultE- 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
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, @NotNull Result<R,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 thesuccessful valueotherwise applying no changes.- Type Parameters:
T- type of the successful result valueE- type of the error result valueR- type of the resulting successful value, super-type of<T>- Parameters:
result- given resultpredicate- condition on which to apply the mapping to thesuccessful valuemappingFunction- function used to map thesuccessful valueif it matches the predicate- Returns:
- the result whose
successful valueis mapped if it matches the predicate
-
mapIfNot
@NotNull public static <T extends R,E, @NotNull Result<R,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 thesuccessful valueotherwise applying no changes.- Type Parameters:
T- type of the successful result valueE- type of the error result valueR- type of the resulting successful value, super-type of<T>- Parameters:
result- given resultpredicate- condition on which to not apply the mapping to thesuccessful valuemappingFunction- function used to map thesuccessful valueif it does not match the predicate- Returns:
- the result whose
successful valueis mapped if it does not match the predicate
-
mapErrorIf
@NotNull public static <T,E extends R, @NotNull Result<T,R> 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 theerror valueotherwise applying no changes.- Type Parameters:
T- type of the successful result valueE- type of the error result valueR- type of the resulting error value, super-type of<E>- Parameters:
result- given resultpredicate- condition on which to apply the mapping to theerror valuemappingFunction- function used to map theerror valueif it matches the predicate- Returns:
- the result whose
error valueis mapped if it matches the predicate
-
mapErrorIfNot
@NotNull public static <T,E extends R, @NotNull Result<T,R> 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 theerror valueotherwise applying no changes.- Type Parameters:
T- type of the successful result valueE- type of the error result valueR- type of the resulting error value, super-type of<E>- Parameters:
result- given resultpredicate- condition on which to not apply the mapping to theerror valuemappingFunction- function used to map theerror valueif it does not match the predicate- Returns:
- the result whose
error valueis mapped if it does not match the predicate
-