|
22 | 22 | import static ch.powerunit.extensions.exceptions.Constants.verifySupplier; |
23 | 23 | import static ch.powerunit.extensions.exceptions.Constants.verifyPredicate; |
24 | 24 | import static ch.powerunit.extensions.exceptions.Constants.verifyFunction; |
| 25 | +import static ch.powerunit.extensions.exceptions.Constants.verifyConsumer; |
25 | 26 |
|
26 | 27 | import java.util.function.Function; |
27 | 28 |
|
| 29 | +import org.apache.commons.collections4.Closure; |
28 | 30 | import org.apache.commons.collections4.Factory; |
29 | 31 | import org.apache.commons.collections4.FunctorException; |
30 | 32 | import org.apache.commons.collections4.Transformer; |
@@ -116,4 +118,26 @@ public static <T> Factory<T> asFactory(SupplierWithException<T, ?> supplier) { |
116 | 118 | public static <I, O> Transformer<I, O> asTransformer(FunctionWithException<I, O, ?> function) { |
117 | 119 | return FunctionWithException.unchecked(verifyFunction(function), DEFAULT_EXCEPTION_MAPPER)::apply; |
118 | 120 | } |
| 121 | + |
| 122 | + /** |
| 123 | + * Transforms a {@link ConsumerWithException} to the one from |
| 124 | + * commons-collections. |
| 125 | + * |
| 126 | + * @param consumer |
| 127 | + * the {@link ConsumerWithException} to be transformed to the one |
| 128 | + * from commons-collections. |
| 129 | + * @param <T> |
| 130 | + * the type of the input argument for the consumer |
| 131 | + * @return the {@link Closure closure} from commons-collections. The |
| 132 | + * ClassCastException and IllegalArgumentException are not wrapped and |
| 133 | + * the other exception are wrapped in a FunctorException. |
| 134 | + * @throws NoClassDefFoundError |
| 135 | + * In case the commons-collections4 library is not available. |
| 136 | + * @throws NullPointerException |
| 137 | + * if consumer is null. |
| 138 | + * @see org.apache.commons.collections4.Predicate |
| 139 | + */ |
| 140 | + public static <T> Closure<T> asClosure(ConsumerWithException<T, ?> consumer) { |
| 141 | + return ConsumerWithException.unchecked(verifyConsumer(consumer), DEFAULT_EXCEPTION_MAPPER)::accept; |
| 142 | + } |
119 | 143 | } |
0 commit comments