Skip to content

Commit e4c6e7a

Browse files
committed
Issue #64 - Add Closure/Consumer support
1 parent 6339ffd commit e4c6e7a

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import static ch.powerunit.extensions.exceptions.Constants.verifySupplier;
2323
import static ch.powerunit.extensions.exceptions.Constants.verifyPredicate;
2424
import static ch.powerunit.extensions.exceptions.Constants.verifyFunction;
25+
import static ch.powerunit.extensions.exceptions.Constants.verifyConsumer;
2526

2627
import java.util.function.Function;
2728

29+
import org.apache.commons.collections4.Closure;
2830
import org.apache.commons.collections4.Factory;
2931
import org.apache.commons.collections4.FunctorException;
3032
import org.apache.commons.collections4.Transformer;
@@ -116,4 +118,26 @@ public static <T> Factory<T> asFactory(SupplierWithException<T, ?> supplier) {
116118
public static <I, O> Transformer<I, O> asTransformer(FunctionWithException<I, O, ?> function) {
117119
return FunctionWithException.unchecked(verifyFunction(function), DEFAULT_EXCEPTION_MAPPER)::apply;
118120
}
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+
}
119143
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,31 @@ public void testAsTransformerOtherException() {
9292
.transform("x")).throwException(instanceOf(FunctorException.class));
9393
}
9494

95+
// AsClosure
96+
97+
@Test
98+
public void testAsClosureNoException() {
99+
CommonsCollections4Helper.asClosure(x -> {
100+
}).execute("x");
101+
}
102+
103+
@Test
104+
public void testAsClosureClassCastException() {
105+
assertWhen((x) -> CommonsCollections4Helper.asClosure(ConsumerWithException.failing(ClassCastException::new))
106+
.execute("x")).throwException(instanceOf(ClassCastException.class));
107+
}
108+
109+
@Test
110+
public void testAsClosureIllegalArgumentException() {
111+
assertWhen((x) -> CommonsCollections4Helper
112+
.asClosure(ConsumerWithException.failing(IllegalArgumentException::new)).execute("x"))
113+
.throwException(instanceOf(IllegalArgumentException.class));
114+
}
115+
116+
@Test
117+
public void testAsClosureOtherException() {
118+
assertWhen((x) -> CommonsCollections4Helper.asClosure(ConsumerWithException.failing(RuntimeException::new))
119+
.execute("x")).throwException(instanceOf(FunctorException.class));
120+
}
121+
95122
}

0 commit comments

Comments
 (0)