Package com.github.skopylov58.functional
Interface FPUtils
public interface FPUtils
Collection of useful higher-order functions to handle exceptions in functional style.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfacestatic interfaceFunction that may throw an exception.static interfaceSupplier that may throw an exception.static final recordTry Result for Java 14+static classTry Result for Java 8+ -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,R> Function<T, R> static <T,R> Function<T, R> static <T> Optional<T>static <T,R> Function<T, R> catchingMapper(FPUtils.CheckedFunction<T, R> func, BiFunction<T, Exception, R> errorMapper) static <T,R> Function<T, R> catchingMapper(FPUtils.CheckedFunction<T, R> func, Supplier<R> supplier) static <T> UnaryOperator<T>Consumer to UnaryOperator conversion.static FPUtils.BackoffexponentialBackoff(long min, long max) Backoff strategy that starts with min value, increasing delay twice on each step, with max upper threshold.static FPUtils.BackofffixedDelay(Duration delay) Fixed delay backoff strategystatic Durationstatic <T,R> Function<T, R> static <T,R> Function<T, R> static <T> Supplier<T>static <T> Supplier<T>memoize(Supplier<T> supplier, AtomicReference<T> ref) static RunnableRuns runnable exactly once.static Runnableonce(Runnable runnable, AtomicBoolean runFlag) default <T> FPUtils.Result<T>onSuccessCatching(FPUtils.Result<T> result, Try.CheckedConsumer<T> cons) static <V> Optional<V>retry(Callable<V> callable, long numOfRetries, FPUtils.Backoff backoff) Retry with backoff strategy.static <V> Optional<V>retry(Callable<V> callable, long numOfRetries, FPUtils.Backoff backoff, Predicate<V> isSuccess) static <V> Optional<V>Retry with fixed delay.static <V> Optional<V>retry2(Callable<V> callable, LongPredicate lp, FPUtils.Backoff backoff, Predicate<V> isSuccess) simpleJitter(Duration duration) Simple jitter using system timer.static booleanstatic <E extends Throwable>
voidstatic <T,R> Function<T, R> throwingMapper(FPUtils.CheckedFunction<T, R> func) toEither(FPUtils.CheckedFunction<T, R> func) Higher-order function to convert partial functionT=>Rto total functionT=>Either<R, Exception>static <T,R> Function<T, CompletableFuture<R>> toFuture(FPUtils.CheckedFunction<T, R> func) toOptional(FPUtils.CheckedFunction<T, R> func) Higher-order function to convert partial functionT=>Rto total functionT=>Optional<R>toOptional(FPUtils.CheckedFunction<T, R> func, BiConsumer<T, Exception> consumer) Higher-order function to convert partial functionT=>Rto total functionT=>Optional<R>Gives access to Exception and corresponding input value.toOptional(FPUtils.CheckedSupplier<T> supplier) Higher-order function to convert partial supplier()=>Tto total supplier()=>Optional<T>toOptional(FPUtils.CheckedSupplier<T> supplier, Consumer<Exception> consumer) Higher-order function to convert partial supplier()=>Tto total supplier()=>Optional<T>static <T,R> Function<T, FPUtils.Result<R>> toResult(FPUtils.CheckedFunction<T, R> func) Higher-order function to convert partial functionT=>Rto total functionT=>Result<R>static <T,R> Function<T, FPUtils.ResultJava8<R>> toResultJava8(FPUtils.CheckedFunction<T, R> func) static <T> Supplier<T>toSupplier(FPUtils.CheckedSupplier<T> supplier)
-
Method Details
-
toResult
Higher-order function to convert partial functionT=>Rto total functionT=>Result<R>- Type Parameters:
T- function input parameter typeR- function result type- Parameters:
func- partial functionT=>Rthat may throw checked exception- Returns:
- total function
T=>Result<R>
-
onSuccessCatching
default <T> FPUtils.Result<T> onSuccessCatching(FPUtils.Result<T> result, Try.CheckedConsumer<T> cons) -
toOptional
Higher-order function to convert partial functionT=>Rto total functionT=>Optional<R>- Type Parameters:
T- function input parameter typeR- function result type- Parameters:
func- partial functionT=>Rthat may throw checked exception- Returns:
- total function
T=>Optional<R>
-
toOptional
static <T,R> Function<T,Optional<R>> toOptional(FPUtils.CheckedFunction<T, R> func, BiConsumer<T, Exception> consumer) Higher-order function to convert partial functionT=>Rto total functionT=>Optional<R>Gives access to Exception and corresponding input value.- Type Parameters:
T- function input parameter typeR- function result type- Parameters:
func- partial functionT=>Rthat may throw checked exceptionconsumer- has access to exception and corresponding input value- Returns:
- total function
T=>Optional<R>
-
catchingMapper
static <T,R> Function<T,R> catchingMapper(FPUtils.CheckedFunction<T, R> func, BiFunction<T, Exception, R> errorMapper) -
throwingMapper
-
catchingMapper
-
toOptional
Higher-order function to convert partial supplier()=>Tto total supplier()=>Optional<T>- Type Parameters:
T- supplier result type- Parameters:
supplier-()=>Tthat may throw an exception- Returns:
- total supplier
()=>Optional<T>
-
toOptional
static <T> Supplier<Optional<T>> toOptional(FPUtils.CheckedSupplier<T> supplier, Consumer<Exception> consumer) Higher-order function to convert partial supplier()=>Tto total supplier()=>Optional<T>- Type Parameters:
T- supplier result type- Parameters:
supplier-()=>Tthat may throw an exceptionconsumer- error consumer- Returns:
- total supplier
()=>Optional<T>
-
toEither
Higher-order function to convert partial functionT=>Rto total functionT=>Either<R, Exception>- Type Parameters:
T- function input parameter typeR- function result type- Parameters:
func- partial functionT=>Rthat may throw an exception- Returns:
- total function
T=>Either<R, Exception>
-
toResultJava8
-
toFuture
-
sneakyThrow
- Throws:
E extends Throwable
-
toSupplier
-
measure
-
after
-
before
-
memoize
-
memoize
-
once
-
once
Runs runnable exactly once.- Parameters:
run- runnable to decorate- Returns:
- runnable that will run only once.
-
memoize
-
memoize
-
ctou
Consumer to UnaryOperator conversion.- Type Parameters:
T- type T- Parameters:
cons- consumer- Returns:
- UnaryOperator
-
simpleJitter
Simple jitter using system timer.- Parameters:
duration- maximum jitter value- Returns:
- random jitter between 0 and duration
-
exponentialBackoff
Backoff strategy that starts with min value, increasing delay twice on each step, with max upper threshold.- Parameters:
min- min delay in millisecondsmax- max delay in milliseconds- Returns:
- exponential backoff
-
fixedDelay
Fixed delay backoff strategy- Parameters:
delay- delay between tries- Returns:
- fixed backoff
-
retry
Retry with fixed delay.- Type Parameters:
V- resulting type- Parameters:
callable- callable to retrynumOfRetries- number of retriesdelay- delay between tries- Returns:
- Optional result
-
retry
Retry with backoff strategy.- Type Parameters:
V- resulting type- Parameters:
callable- callable to retrynumOfRetries- number of retriesbackoff- backoff strategy- Returns:
- Optional result
-
retry
static <V> Optional<V> retry(Callable<V> callable, long numOfRetries, FPUtils.Backoff backoff, Predicate<V> isSuccess) -
retry2
static <V> Optional<V> retry2(Callable<V> callable, LongPredicate lp, FPUtils.Backoff backoff, Predicate<V> isSuccess) -
sleep
-
call
-