Class LambdaExceptionUtil
Wraps consumers and functions into interfaces that can throw Checked Exceptions
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interfacestatic interface -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,E extends Exception>
Comparator<T>ignoringComparator(LambdaExceptionUtil.ComparatorWithExceptions<T, E> comparator) Wrap a standardComparator, swallowing allException.static <T,E extends Exception>
Comparator<T>rethrowComparator(LambdaExceptionUtil.ComparatorWithExceptions<T, E> comparator) Wrap a standardComparatorin aLambdaExceptionUtil.ComparatorWithExceptions.rethrowConsumer(LambdaExceptionUtil.ConsumerWithExceptions<T, E> consumer) Wrap a standardConsumerin aLambdaExceptionUtil.ConsumerWithExceptions.rethrowFunction(LambdaExceptionUtil.FunctionWithExceptions<T, R, E> function) Wrap a standardFunctionin aLambdaExceptionUtil.FunctionWithExceptions.
-
Constructor Details
-
LambdaExceptionUtil
public LambdaExceptionUtil()
-
-
Method Details
-
rethrowConsumer
public static <T,E extends Exception> Consumer<T> rethrowConsumer(LambdaExceptionUtil.ConsumerWithExceptions<T, E> consumer) Wrap a standardConsumerin aLambdaExceptionUtil.ConsumerWithExceptions.This allows a consumer to throw Checked Exception that will be propagated to the context of the statement calling it
Example:
Stream.of("java.lang.String", "java.bad.Class").forEach(rethrowConsumer(name -> System.out.println(Class.forName(name))));throws checkedClassNotFoundException -
rethrowFunction
public static <T,R, Function<T,E extends Exception> R> rethrowFunction(LambdaExceptionUtil.FunctionWithExceptions<T, R, E> function) Wrap a standardFunctionin aLambdaExceptionUtil.FunctionWithExceptions.This allows a lambda function to throw Checked Exception that will be propagated to the context of the statement calling it
Example:
Stream.of("java.lang.String", "java.bad.Class").map(rethrowFunction(Class::forName)));throws checkedClassNotFoundException -
rethrowComparator
public static <T,E extends Exception> Comparator<T> rethrowComparator(LambdaExceptionUtil.ComparatorWithExceptions<T, E> comparator) Wrap a standardComparatorin aLambdaExceptionUtil.ComparatorWithExceptions.This allows a comparator to propagate a Checked Exception
Example:
List.of("java.lang.String", "java.lang.Integer", "java.bad.Class").sorted((c1, c2) -> Class.forName(c1).getFields().length - Class.forName(c2).getFields().lengththrows checkedClassNotFoundException -
ignoringComparator
public static <T,E extends Exception> Comparator<T> ignoringComparator(LambdaExceptionUtil.ComparatorWithExceptions<T, E> comparator) Wrap a standardComparator, swallowing allException.WARNING! WhenComparator.compare(T, T)throws an exception the elements are treated as equal. This should only be used for ignoring checked exceptions that can never be throws.
-