|
21 | 21 |
|
22 | 22 | import static ch.powerunit.extensions.exceptions.Constants.verifySupplier; |
23 | 23 | import static ch.powerunit.extensions.exceptions.Constants.verifyPredicate; |
| 24 | +import static ch.powerunit.extensions.exceptions.Constants.verifyFunction; |
24 | 25 |
|
25 | 26 | import java.util.function.Function; |
26 | 27 |
|
27 | 28 | import org.apache.commons.collections4.Factory; |
28 | 29 | import org.apache.commons.collections4.FunctorException; |
| 30 | +import org.apache.commons.collections4.Transformer; |
29 | 31 |
|
30 | 32 | /** |
31 | 33 | * This class provides several helper methods for the functional interface of |
@@ -90,4 +92,28 @@ public static <T> org.apache.commons.collections4.Predicate<T> asPredicate(Predi |
90 | 92 | public static <T> Factory<T> asFactory(SupplierWithException<T, ?> supplier) { |
91 | 93 | return SupplierWithException.unchecked(verifySupplier(supplier), FunctorException::new)::get; |
92 | 94 | } |
| 95 | + |
| 96 | + /** |
| 97 | + * Transforms a {@link FunctionWithException} to the one from |
| 98 | + * commons-collections. |
| 99 | + * |
| 100 | + * @param function |
| 101 | + * the {@link FunctionWithException} to be transformed to the one |
| 102 | + * from commons-collections. |
| 103 | + * @param <I> |
| 104 | + * the input argument type of the function |
| 105 | + * @param <O> |
| 106 | + * the result type of the function |
| 107 | + * @return the {@link Transformer transformer} from commons-collections. The |
| 108 | + * ClassCastException and IllegalArgumentException are not wrapped and |
| 109 | + * the other exception are wrapped in a FunctorException. |
| 110 | + * @throws NoClassDefFoundError |
| 111 | + * In case the commons-collections4 library is not available. |
| 112 | + * @throws NullPointerException |
| 113 | + * if function is null. |
| 114 | + * @see org.apache.commons.collections4.Transformer |
| 115 | + */ |
| 116 | + public static <I, O> Transformer<I, O> asTransformer(FunctionWithException<I, O, ?> function) { |
| 117 | + return FunctionWithException.unchecked(verifyFunction(function), DEFAULT_EXCEPTION_MAPPER)::apply; |
| 118 | + } |
93 | 119 | } |
0 commit comments