@@ -40,41 +40,48 @@ private WrappedCheckedException(Throwable cause)
40
40
}
41
41
42
42
/**
43
- * Wraps any checked exceptions thrown by a callable .
43
+ * Wraps any checked exceptions thrown by a {@code Callable} .
44
44
*
45
- * @param callable the task to execute
46
- * @param <V> the type of value returned by {@code callable }
47
- * @return the value returned by {@code callable}
48
- * @throws NullPointerException if {@code callable } is null
45
+ * @param task the task to execute
46
+ * @param <V> the type of value returned by {@code task }
47
+ * @return a {@code Callable} that does not throw any checked exceptions
48
+ * @throws NullPointerException if {@code task } is null
49
49
*/
50
- public static <V > V wrap (Callable <V > callable )
50
+ public static <V > UncheckedCallable < V > wrap (Callable <V > task )
51
51
{
52
- try
52
+ return () ->
53
53
{
54
- return callable .call ();
55
- }
56
- catch (Exception e )
57
- {
58
- throw WrappedCheckedException .wrap (e );
59
- }
54
+ try
55
+ {
56
+ return task .call ();
57
+ }
58
+ catch (Exception e )
59
+ {
60
+ throw WrappedCheckedException .wrap (e );
61
+ }
62
+ };
60
63
}
61
64
62
65
/**
63
- * Wraps any checked exceptions thrown by a task .
66
+ * Wraps any checked exceptions thrown by a {@code ThrowingRunnable} .
64
67
*
65
68
* @param task the task to execute
69
+ * @return a {@code Runnable}
66
70
* @throws NullPointerException if {@code task} is null
67
71
*/
68
- public static void wrap (Task task )
72
+ public static Runnable wrap (CheckedRunnable task )
69
73
{
70
- try
71
- {
72
- task .run ();
73
- }
74
- catch (Exception e )
74
+ return () ->
75
75
{
76
- throw WrappedCheckedException .wrap (e );
77
- }
76
+ try
77
+ {
78
+ task .run ();
79
+ }
80
+ catch (Exception e )
81
+ {
82
+ throw WrappedCheckedException .wrap (e );
83
+ }
84
+ };
78
85
}
79
86
80
87
/**
@@ -111,10 +118,10 @@ public static RuntimeException wrap(String message, Throwable t)
111
118
}
112
119
113
120
/**
114
- * A {@link Callable} without a return value .
121
+ * A {@link Runnable} that throws checked exceptions .
115
122
*/
116
123
@ FunctionalInterface
117
- public interface Task
124
+ public interface CheckedRunnable
118
125
{
119
126
/**
120
127
* Runs the task.
@@ -123,4 +130,18 @@ public interface Task
123
130
*/
124
131
void run () throws Exception ;
125
132
}
133
+
134
+ /**
135
+ * A {@link Callable} that does not throw any checked exceptions.
136
+ */
137
+ @ FunctionalInterface
138
+ public interface UncheckedCallable <V >
139
+ {
140
+ /**
141
+ * Runs the task.
142
+ *
143
+ * @throws WrappedCheckedException if unable to compute a result
144
+ */
145
+ V call ();
146
+ }
126
147
}
0 commit comments