Skip to content

Commit 6339ffd

Browse files
committed
Issue #64 - Add Transformer/Function support
1 parent 036508c commit 6339ffd

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/main/java/ch/powerunit/extensions/exceptions/CommonsCollections4Helper.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121

2222
import static ch.powerunit.extensions.exceptions.Constants.verifySupplier;
2323
import static ch.powerunit.extensions.exceptions.Constants.verifyPredicate;
24+
import static ch.powerunit.extensions.exceptions.Constants.verifyFunction;
2425

2526
import java.util.function.Function;
2627

2728
import org.apache.commons.collections4.Factory;
2829
import org.apache.commons.collections4.FunctorException;
30+
import org.apache.commons.collections4.Transformer;
2931

3032
/**
3133
* 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
9092
public static <T> Factory<T> asFactory(SupplierWithException<T, ?> supplier) {
9193
return SupplierWithException.unchecked(verifySupplier(supplier), FunctorException::new)::get;
9294
}
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+
}
93119
}

src/test/java/ch/powerunit/extensions/exceptions/CommonsCollections4HelperTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
package ch.powerunit.extensions.exceptions;
2121

22+
import org.apache.commons.collections4.FunctorException;
23+
2224
import ch.powerunit.Test;
2325
import ch.powerunit.TestSuite;
2426

@@ -48,7 +50,7 @@ public void testAsPredicateIllegalArgumentException() {
4850
@Test
4951
public void testAsPredicateOtherException() {
5052
assertWhen(() -> CommonsCollections4Helper.asPredicate(PredicateWithException.failing(Exception::new))
51-
.evaluate("x")).throwException(instanceOf(Exception.class));
53+
.evaluate("x")).throwException(instanceOf(FunctorException.class));
5254
}
5355

5456
// AsFactory
@@ -64,4 +66,30 @@ public void testAsFactoryOtherException() {
6466
.throwException(instanceOf(Exception.class));
6567
}
6668

69+
// AsTransformer
70+
71+
@Test
72+
public void testAsTransformerNoException() {
73+
assertThat(CommonsCollections4Helper.asTransformer(x -> "y").transform("x")).is("y");
74+
}
75+
76+
@Test
77+
public void testAsTransformerClassCastException() {
78+
assertWhen(() -> CommonsCollections4Helper.asTransformer(FunctionWithException.failing(ClassCastException::new))
79+
.transform("x")).throwException(instanceOf(ClassCastException.class));
80+
}
81+
82+
@Test
83+
public void testAsTransformerIllegalArgumentException() {
84+
assertWhen(() -> CommonsCollections4Helper
85+
.asTransformer(FunctionWithException.failing(IllegalArgumentException::new)).transform("x"))
86+
.throwException(instanceOf(IllegalArgumentException.class));
87+
}
88+
89+
@Test
90+
public void testAsTransformerOtherException() {
91+
assertWhen(() -> CommonsCollections4Helper.asTransformer(FunctionWithException.failing(RuntimeException::new))
92+
.transform("x")).throwException(instanceOf(FunctorException.class));
93+
}
94+
6795
}

0 commit comments

Comments
 (0)