Skip to content

Commit 0f48f27

Browse files
committed
Cleaned up ThrowingCallable again. Callable do trhow already.
1 parent 8b03300 commit 0f48f27

File tree

6 files changed

+61
-76
lines changed

6 files changed

+61
-76
lines changed

mihxil-functional/src/main/java/org/meeuw/functional/Functions.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,29 @@ public R call() throws E {
178178
* @return a new callable, which will use the given function and argument for its implementation
179179
* @since 0.14
180180
*/
181-
public static <A1, R, E extends Exception> Callable<R> withArg1(ThrowingFunction<A1, R, E> function) {
182-
return new CallableWrapper<ThrowingFunction<A1, R, E>, R>(function, null) {
181+
public static <A1, R, E extends Exception> Callable<R> withNull(ThrowingFunction<A1, R, E> function) {
182+
return withArg1(function, null);
183+
}
184+
185+
186+
/**
187+
* Giving a {@link ThrowingFunction function}, morphs it into a {@link Callable} that ignores its argument and just calls the function with {@code null}.
188+
*
189+
* @param <R> the return value of the function, and of the resulting {@link Callable}
190+
* @param <E> the exception type that the function (and the callable) can throw
191+
* @param callable the function on which to base the new {@code Callable} on
192+
* @return a new function, which will use the given call its implementation. The argument of the function will be ignored, all calls just call the {@code Callable}.
193+
* @since 0.14
194+
*/
195+
public static <R, E extends Exception > ThrowingFunction<Void, R, E> ignoreArg1(Callable<R> callable) {
196+
return new ThrowingMonoWrapper<Callable<R>, Void, R, E>(callable, null, "function") {
183197
@Override
184-
public R call() throws E {
185-
return wrapped.applyWithException(null);
198+
public R applyWithException(Void o) throws E {
199+
try {
200+
return callable.call();
201+
} catch (Exception e) {
202+
return Sneaky.sneakyThrow(e);
203+
}
186204
}
187205
};
188206
}
@@ -522,6 +540,20 @@ public MonoWrapper(W wrapped, Object value, String why) {
522540
}
523541
}
524542

543+
544+
545+
protected static abstract class ThrowingMonoWrapper<W, X, R, E extends Exception> extends ValueWrapper<W> implements ThrowingFunction<X, R, E> {
546+
public ThrowingMonoWrapper(W wrapped, Object value, String why) {
547+
super(wrapped, value, why);
548+
}
549+
}
550+
551+
552+
/**
553+
* A wrapper that is {@link Callable}
554+
* @param <W>
555+
* @param <X>
556+
*/
525557
protected static abstract class CallableWrapper<W, X> extends Wrapper<W> implements Callable<X> {
526558
public CallableWrapper(W wrapped, String why) {
527559
super(wrapped, why);

mihxil-functional/src/main/java/org/meeuw/functional/ThrowAnyCallable.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

mihxil-functional/src/main/java/org/meeuw/functional/ThrowingCallable.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

mihxil-functional/src/main/java/org/meeuw/functional/ThrowingFunction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/**
99
* An extension of {@link Function} that can throw exceptions too.
1010
* @since 1.13
11+
* @param <E> the type of exception that can be thrown
1112
*/
1213
@FunctionalInterface
1314
public interface ThrowingFunction<A, R, E extends Exception> extends Function<A, R> {

mihxil-functional/src/test/java/org/meeuw/functional/FunctionsTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.meeuw.functional;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.Callable;
6+
import java.util.concurrent.atomic.AtomicInteger;
37
import java.util.function.*;
48

59
import org.junit.jupiter.api.Test;
@@ -12,6 +16,7 @@
1216
/**
1317
* @author Michiel Meeuwissen
1418
*/
19+
@SuppressWarnings({"ConstantValue", "EqualsBetweenInconvertibleTypes"})
1520
class FunctionsTest {
1621

1722
@Test
@@ -80,17 +85,32 @@ void quadriAlways() {
8085

8186

8287

88+
89+
8390
@Test
8491
void monoWithArg1() {
8592
assertThat(withArg1(mono, 1.0f).get()).isEqualTo(("1.0"));
8693
}
8794

8895
@Test
89-
void tmonoWithArg1() throws Exception {
90-
assertThat(withArg1(tmono, 1.0f).call()).isEqualTo(("1.0"));
96+
void tmonoWithNull() throws Exception {
97+
List<String> list = new ArrayList<>();
98+
AtomicInteger counter = new AtomicInteger();
99+
Callable<Integer> callable = () -> {
100+
int result = counter.incrementAndGet();
101+
if (result % 3 == 0) {
102+
throw new IllegalArgumentException();
103+
}
104+
return result;
105+
};
106+
Callable<Integer> wrapped = withNull(ignoreArg1(callable));
107+
108+
assertThat(wrapped.call()).isEqualTo(1);
109+
assertThat(wrapped.call()).isEqualTo(2);
110+
assertThatThrownBy(wrapped::call).isInstanceOf(IllegalArgumentException.class);
111+
91112

92-
assertThatThrownBy(() ->
93-
withArg1(tmono, 0.0f).call()).isInstanceOf(IllegalArgumentException.class);
113+
assertThat(wrapped.toString()).endsWith("(function)(with arg1 null)");
94114

95115
}
96116

mihxil-functional/src/test/java/org/meeuw/functional/ThrowingCallableTest.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)