Skip to content

Commit 65e3b9d

Browse files
committed
✨ add some throwableConsumer methods
1 parent 2f4a6a3 commit 65e3b9d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.dianpoint.summer.lang;
2+
3+
import com.dianpoint.summer.utils.AssertUtils;
4+
5+
import java.util.function.BiConsumer;
6+
7+
@FunctionalInterface
8+
public interface ThrowableConsumer<T> {
9+
10+
void accept(T t) throws Throwable;
11+
12+
13+
/**
14+
* 定义一个默认的异常处理方法,用来固定抛出一个runtimeException
15+
*
16+
* @param t 入参
17+
* @param throwable 待抛出的异常
18+
*/
19+
default void handleException(T t, Throwable throwable) {
20+
throw new RuntimeException(throwable);
21+
}
22+
23+
/**
24+
* 默认执行逻辑,无客制化异常处理逻辑,采用默认的异常处理方法{@link #handleException(Object, Throwable)}
25+
*
26+
* @param t 入参
27+
*/
28+
default void execute(T t) {
29+
30+
}
31+
32+
default void execute(T t, BiConsumer<T, Throwable> exceptionHandler) throws NullPointerException {
33+
AssertUtils.assertNotNull(exceptionHandler, () -> "The 'exceptionHandler' must not be null");
34+
try {
35+
accept(t);
36+
} catch (Throwable failure) {
37+
exceptionHandler.accept(t, failure);
38+
}
39+
}
40+
41+
default void execute(T t, ThrowableConsumer<T> consumer) throws NullPointerException {
42+
consumer.execute(t, this::handleException);
43+
}
44+
45+
default void execute(T t, ThrowableConsumer<T> consumer, BiConsumer<T, Throwable> exceptionHandler)
46+
throws NullPointerException {
47+
AssertUtils.assertNotNull(exceptionHandler, () -> "The 'exceptionHandler' must not be null");
48+
consumer.execute(t,exceptionHandler);
49+
}
50+
}

0 commit comments

Comments
 (0)