Skip to content

Commit d559e35

Browse files
committed
✨ add throwable functions
1 parent 386ac51 commit d559e35

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

summer-java-core/src/main/java/com/dianpoint/summer/lang/ThrowableSupplier.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.dianpoint.summer.lang;
22

3+
import java.util.function.Function;
4+
5+
import static com.dianpoint.summer.utils.AssertUtils.assertNotNull;
6+
37
/**
48
* @author: congccoder
59
* @email: congccoder@gmail.com | <a href="https://github.com/ccoderJava">github-homepage</a>
@@ -10,4 +14,34 @@
1014
public interface ThrowableSupplier<T> {
1115

1216
T get() throws Throwable;
17+
18+
default T execute() {
19+
return execute(this::handleException);
20+
}
21+
22+
default T handleException(Throwable failure) {
23+
throw new RuntimeException(failure);
24+
}
25+
26+
default T execute(Function<Throwable, T> exceptionHandler) {
27+
assertNotNull(exceptionHandler, () -> "The 'exceptionHandler' must not be null");
28+
T result;
29+
try {
30+
result = get();
31+
} catch (Throwable e) {
32+
result = exceptionHandler.apply(e);
33+
}
34+
return result;
35+
}
36+
37+
static <T> T execute(ThrowableSupplier<T> supplier) throws NullPointerException {
38+
return execute(supplier, supplier::handleException);
39+
}
40+
41+
42+
static <T> T execute(ThrowableSupplier<T> supplier, Function<Throwable, T> exceptionHandler) throws NullPointerException {
43+
assertNotNull(supplier, "The supplier must not be null");
44+
return supplier.execute(exceptionHandler);
45+
}
46+
1347
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.dianpoint.summer.utils;
2+
3+
import java.util.function.Supplier;
4+
5+
/**
6+
* @author: congccoder
7+
* @email: congccoder@gmail.com | <a href="https://github.com/ccoderJava">github-homepage</a>
8+
* @date: 2025/6/5 20:46
9+
*/
10+
11+
public class AssertUtils implements Utils {
12+
13+
public static void assertNotNull(Object object, String message) {
14+
if (object == null) {
15+
throw new IllegalArgumentException(message);
16+
}
17+
}
18+
19+
public static void assertNotNull(Object object, Supplier<String> messageSupplier) {
20+
if (object == null) {
21+
throw new IllegalArgumentException(nullSafeGet(messageSupplier));
22+
}
23+
}
24+
25+
private static String nullSafeGet(Supplier<String> messageSupplier) {
26+
return messageSupplier == null ? null : messageSupplier.get();
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.dianpoint.summer.utils;
2+
3+
/**
4+
* @author: congccoder
5+
* @email: congccoder@gmail.com | <a href="https://github.com/ccoderJava">github-homepage</a>
6+
* @date: 2025/6/5 20:46
7+
*/
8+
9+
public interface Utils {
10+
}

0 commit comments

Comments
 (0)