Class LambdaUtils

java.lang.Object
no.mnemonic.commons.utilities.lambda.LambdaUtils

public class LambdaUtils extends Object
  • Method Details

    • waitFor

      public static boolean waitFor(BooleanSupplier booleanSupplier, long waitTime, TimeUnit timeUnit) throws InterruptedException
      Simple helper to wait for a predicate to return true. Will test predicate every 100ms until true or timeout.
      Parameters:
      booleanSupplier - the predicate
      waitTime - how long to wait before giving up
      timeUnit - time unit of waitTime
      Returns:
      true if predicate returns true within timeout, false if timed out without getting true value
      Throws:
      InterruptedException - if interrupted during sleep.
    • tryTo

      public static boolean tryTo(ExceptionalTask callable)
      Call provided task, ignore any exception thrown. Convenience method to call a method/lambda without having to wrap with try/catch
      Parameters:
      callable - task to call
      Returns:
      true if task was successful, false if exception was caught
    • tryTo

      public static boolean tryTo(ExceptionalTask callable, Consumer<Throwable> onException)
      Call provided task, ignore any exception thrown, instead passing it to a provided exception handler. Convenience method to call a method/lambda without having to wrap with try/catch LambdaUtils.tryTo(()->doSomethingThatMightThrowAnException(), error->LOGGER.warn("Error doing something", error));
      Parameters:
      callable - task to call
      onException - consumer to provide any exception caught
      Returns:
      true if task was successful, false if exception was caught
    • forEachTry

      public static <T> void forEachTry(Collection<T> values, ExceptionalConsumer<T,? extends Exception> consumer)
      Try to perform operation on multiple values, ignoring any exception thrown. Convenience method to call forEach on a collection of values without having to use try/catch in lambda
      Type Parameters:
      T - value type
      Parameters:
      values - values
      consumer - consumer to handle value, which may throw exception
    • forEachTry

      public static <T, E extends Exception> void forEachTry(Collection<T> values, ExceptionalConsumer<T,E> consumer, Consumer<Throwable> onException)
      Try to perform operation on multiple values, ignoring any exception thrown, instead pass any error to exception consumer. Convenience method to call forEach on a collection of values without having to use try/catch in lambda
      Type Parameters:
      T - value type
      Parameters:
      values - values
      consumer - consumer to handle value, which may throw exception
      onException - exception handler to pass any exception to. Might be called once for every invocation of consumer.
    • tryResult

      public static <T> T tryResult(Callable<T> supplier, T defaultValue)
      Invoke callable to fetch result and return it. If exception is thrown, return default value instead. Use this method to avoid try/catch blocks where the expected exception should result in a default value.
      Type Parameters:
      T - value type
      Parameters:
      supplier - callable to fetch result from
      defaultValue - value to return if callable fails
      Returns:
      the value from the callable, or defaultValue on exception
    • tryResult

      public static <T> T tryResult(Callable<T> supplier, Supplier<T> defaultValueSupplier, Consumer<Throwable> onException)
      Invoke callable to fetch result and return it. If exception is thrown, return value from defaultValueSupplier instead. Notify onException of any exception caught. Use this method to avoid try/catch blocks where the expected exception should result in a default value.
      Type Parameters:
      T - value type
      Parameters:
      supplier - callable to fetch result from
      defaultValueSupplier - supplier to fetch default value from
      onException - exception consumer to notify on exception
      Returns:
      the value from the callable, or defaultValue on exception
      See Also:
    • tryStream

      public static <T, E extends Exception> TryStream<T,E> tryStream(Stream<T> stream)
      Wrap stream into a TryStream. A TryStream allows using map/filter lambdas which throws checked exceptions. Any exception thrown will be caught and rethrown by this method, this is just a convenience method to avoid having to use try/catch inside your lambdas.
      Type Parameters:
      T - stream type
      E - checked exception
      Parameters:
      stream - any stream
      Returns:
      a TryStream wrapping the stream
      See Also:
    • tryStream

      public static <T, E extends Exception> TryStream<T,E> tryStream(Collection<T> collection)