File tree Expand file tree Collapse file tree 3 files changed +72
-0
lines changed
summer-java-core/src/main/java/com/dianpoint/summer Expand file tree Collapse file tree 3 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 11package 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>
1014public 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments