Skip to content

Commit 036508c

Browse files
committed
Issue #64 - Add Factory/Supplier support
1 parent ede320a commit 036508c

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

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

22+
import static ch.powerunit.extensions.exceptions.Constants.verifySupplier;
2223
import static ch.powerunit.extensions.exceptions.Constants.verifyPredicate;
2324

2425
import java.util.function.Function;
25-
import java.util.function.Predicate;
2626

27+
import org.apache.commons.collections4.Factory;
2728
import org.apache.commons.collections4.FunctorException;
2829

2930
/**
@@ -68,4 +69,25 @@ private CommonsCollections4Helper() {
6869
public static <T> org.apache.commons.collections4.Predicate<T> asPredicate(PredicateWithException<T, ?> predicate) {
6970
return PredicateWithException.unchecked(verifyPredicate(predicate), DEFAULT_EXCEPTION_MAPPER)::test;
7071
}
72+
73+
/**
74+
* Transforms a {@link SupplierWithException} to the one from
75+
* commons-collections.
76+
*
77+
* @param supplier
78+
* the {@link SupplierWithException} to be transformed to the one
79+
* from commons-collections.
80+
* @param <T>
81+
* the type of the result of the supplier
82+
* @return the {@link Factory factory} from commons-collections. The exception
83+
* are wrapped in a FunctorException.
84+
* @throws NoClassDefFoundError
85+
* In case the commons-collections4 library is not available.
86+
* @throws NullPointerException
87+
* if supplier is null.
88+
* @see org.apache.commons.collections4.Factory
89+
*/
90+
public static <T> Factory<T> asFactory(SupplierWithException<T, ?> supplier) {
91+
return SupplierWithException.unchecked(verifySupplier(supplier), FunctorException::new)::get;
92+
}
7193
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
@SuppressWarnings("squid:S2187") // Sonar doesn't under that it is really a test
2626
public class CommonsCollections4HelperTest implements TestSuite {
2727

28+
// AsPredicate
29+
2830
@Test
2931
public void testAsPredicateNoException() {
3032
assertThat(CommonsCollections4Helper.asPredicate(x -> true).evaluate("x")).is(true);
@@ -49,4 +51,17 @@ public void testAsPredicateOtherException() {
4951
.evaluate("x")).throwException(instanceOf(Exception.class));
5052
}
5153

54+
// AsFactory
55+
56+
@Test
57+
public void testAsFactoryNoException() {
58+
assertThat(CommonsCollections4Helper.asFactory(() -> "").create()).is("");
59+
}
60+
61+
@Test
62+
public void testAsFactoryOtherException() {
63+
assertWhen(() -> CommonsCollections4Helper.asFactory(SupplierWithException.failing(Exception::new)).create())
64+
.throwException(instanceOf(Exception.class));
65+
}
66+
5267
}

0 commit comments

Comments
 (0)