Interface FPUtils


public interface FPUtils
Collection of useful higher-order functions to handle exceptions in functional style.
  • Method Details

    • toResult

      static <T, R> Function<T,FPUtils.Result<R>> toResult(FPUtils.CheckedFunction<T,R> func)
      Higher-order function to convert partial function T=>R to total function T=>Result<R>
      Type Parameters:
      T - function input parameter type
      R - function result type
      Parameters:
      func - partial function T=>R that 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

      static <T, R> Function<T,Optional<R>> toOptional(FPUtils.CheckedFunction<T,R> func)
      Higher-order function to convert partial function T=>R to total function T=>Optional<R>
      Type Parameters:
      T - function input parameter type
      R - function result type
      Parameters:
      func - partial function T=>R that 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 function T=>R to total function T=>Optional<R> Gives access to Exception and corresponding input value.
      Type Parameters:
      T - function input parameter type
      R - function result type
      Parameters:
      func - partial function T=>R that may throw checked exception
      consumer - 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

      static <T, R> Function<T,R> throwingMapper(FPUtils.CheckedFunction<T,R> func)
    • catchingMapper

      static <T, R> Function<T,R> catchingMapper(FPUtils.CheckedFunction<T,R> func, Supplier<R> supplier)
    • toOptional

      static <T> Supplier<Optional<T>> toOptional(FPUtils.CheckedSupplier<T> supplier)
      Higher-order function to convert partial supplier ()=>T to total supplier ()=>Optional<T>
      Type Parameters:
      T - supplier result type
      Parameters:
      supplier - ()=>T that 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 ()=>T to total supplier ()=>Optional<T>
      Type Parameters:
      T - supplier result type
      Parameters:
      supplier - ()=>T that may throw an exception
      consumer - error consumer
      Returns:
      total supplier ()=>Optional<T>
    • toEither

      static <T, R> Function<T,Either<Exception,R>> toEither(FPUtils.CheckedFunction<T,R> func)
      Higher-order function to convert partial function T=>R to total function T=>Either<R, Exception>
      Type Parameters:
      T - function input parameter type
      R - function result type
      Parameters:
      func - partial function T=>R that may throw an exception
      Returns:
      total function T=>Either<R, Exception>
    • toResultJava8

      static <T, R> Function<T,FPUtils.ResultJava8<R>> toResultJava8(FPUtils.CheckedFunction<T,R> func)
    • toFuture

      static <T, R> Function<T,CompletableFuture<R>> toFuture(FPUtils.CheckedFunction<T,R> func)
    • sneakyThrow

      static <E extends Throwable> void sneakyThrow(Throwable e) throws E
      Throws:
      E extends Throwable
    • toSupplier

      static <T> Supplier<T> toSupplier(FPUtils.CheckedSupplier<T> supplier)
    • measure

      static Duration measure(Runnable runnable)
    • after

      static <T, R> Function<T,R> after(Function<T,R> func, Consumer<R> after)
    • before

      static <T, R> Function<T,R> before(Function<T,R> func, Consumer<T> before)
    • memoize

      static <T, R> Function<T,R> memoize(Function<T,R> func, Map<T,R> cache)
    • memoize

      static <T, R> Function<T,R> memoize(Function<T,R> func)
    • once

      static Runnable once(Runnable runnable, AtomicBoolean runFlag)
    • once

      static Runnable once(Runnable run)
      Runs runnable exactly once.
      Parameters:
      run - runnable to decorate
      Returns:
      runnable that will run only once.
    • memoize

      static <T> Supplier<T> memoize(Supplier<T> supplier, AtomicReference<T> ref)
    • memoize

      static <T> Supplier<T> memoize(Supplier<T> supplier)
    • ctou

      static <T> UnaryOperator<T> ctou(Consumer<T> cons)
      Consumer to UnaryOperator conversion.
      Type Parameters:
      T - type T
      Parameters:
      cons - consumer
      Returns:
      UnaryOperator
    • simpleJitter

      static Supplier<Duration> simpleJitter(Duration duration)
      Simple jitter using system timer.
      Parameters:
      duration - maximum jitter value
      Returns:
      random jitter between 0 and duration
    • exponentialBackoff

      static FPUtils.Backoff exponentialBackoff(long min, long max)
      Backoff strategy that starts with min value, increasing delay twice on each step, with max upper threshold.
      Parameters:
      min - min delay in milliseconds
      max - max delay in milliseconds
      Returns:
      exponential backoff
    • fixedDelay

      static FPUtils.Backoff fixedDelay(Duration delay)
      Fixed delay backoff strategy
      Parameters:
      delay - delay between tries
      Returns:
      fixed backoff
    • retry

      static <V> Optional<V> retry(Callable<V> callable, long numOfRetries, Duration delay)
      Retry with fixed delay.
      Type Parameters:
      V - resulting type
      Parameters:
      callable - callable to retry
      numOfRetries - number of retries
      delay - delay between tries
      Returns:
      Optional result
    • retry

      static <V> Optional<V> retry(Callable<V> callable, long numOfRetries, FPUtils.Backoff backoff)
      Retry with backoff strategy.
      Type Parameters:
      V - resulting type
      Parameters:
      callable - callable to retry
      numOfRetries - number of retries
      backoff - 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

      static boolean sleep(Duration delay)
    • call

      static <T> Optional<T> call(Callable<T> callable)