Class CheckedAdapters

java.lang.Object
com.dudko.tools.safejavastreams.adapter.CheckedAdapters

public class CheckedAdapters extends Object
Utility class for working with lambdas and stream functions that throw checked exceptions.

This enables using Stream API and functional interfaces in a safer and cleaner way without wrapping try/catch manually in every lambda.

  • Constructor Details

    • CheckedAdapters

      public CheckedAdapters()
  • Method Details

    • wrapFunction

      public static <T, R> Function<T,R> wrapFunction(ThrowingFunction<T,R> function)
      Wraps a ThrowingFunction and rethrows as unchecked RuntimeException.
      Type Parameters:
      T - input type
      R - result type
      Parameters:
      function - lambda with checked exception
      Returns:
      safe Function
    • wrapBiFunction

      public static <T, U, R> BiFunction<T,U,R> wrapBiFunction(ThrowingBiFunction<T,U,R> function)
      Wraps a ThrowingBiFunction and rethrows as unchecked RuntimeException.
      Type Parameters:
      T - first input
      U - second input
      R - result type
      Parameters:
      function - lambda with checked exception
      Returns:
      safe BiFunction
    • wrapConsumer

      public static <T> Consumer<T> wrapConsumer(ThrowingConsumer<T> consumer)
      Wraps a ThrowingConsumer into a regular Consumer.
      Type Parameters:
      T - consumed type
      Parameters:
      consumer - lambda with checked exception
      Returns:
      safe Consumer
    • wrapBiConsumer

      public static <T, U> BiConsumer<T,U> wrapBiConsumer(ThrowingBiConsumer<T,U> consumer)
      Wraps a ThrowingBiConsumer into a regular BiConsumer.
      Type Parameters:
      T - first input
      U - second input
      Parameters:
      consumer - lambda with checked exception
      Returns:
      safe BiConsumer
    • wrapSupplier

      public static <T> Supplier<T> wrapSupplier(ThrowingSupplier<T> supplier)
      Wraps a ThrowingSupplier into a regular Supplier.
      Type Parameters:
      T - result type
      Parameters:
      supplier - lambda with checked exception
      Returns:
      safe Supplier
    • wrapRunnable

      public static Runnable wrapRunnable(ThrowingRunnable runnable)
      Wraps a ThrowingRunnable into a regular Runnable.
      Parameters:
      runnable - lambda with checked exception
      Returns:
      safe Runnable
    • safeFunctionOptional

      public static <T, R> Function<T,Optional<R>> safeFunctionOptional(ThrowingFunction<T,R> function)
      Converts a ThrowingFunction into one that returns Optional. Returns Optional.empty() in case of exception.
      Type Parameters:
      T - input type
      R - result type
      Parameters:
      function - function that may throw
      Returns:
      Optional-producing safe function
    • safeFunctionEither

      public static <T, R> Function<T,Either<Exception,R>> safeFunctionEither(ThrowingFunction<T,R> function)
      Converts a ThrowingFunction into one that returns Either<Exception, R>.
      Type Parameters:
      T - input type
      R - result type
      Parameters:
      function - function that may throw
      Returns:
      Either-producing function
    • propagate

      private static RuntimeException propagate(Exception e)
      Propagates a checked exception as unchecked RuntimeException.
      Parameters:
      e - exception to propagate
      Returns:
      RuntimeException