Skip to content

Commit e3b107f

Browse files
committed
WrappedCheckedException.wrap(Runnable) and wrap(Callable) now returns a wrapped Runnable/Callable instead of executing the task immediately.
1 parent abd5235 commit e3b107f

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

core/src/main/java/com/github/cowwoc/pouch/core/Scopes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
package com.github.cowwoc.pouch.core;
66

7-
import com.github.cowwoc.pouch.core.WrappedCheckedException.Task;
7+
import com.github.cowwoc.pouch.core.WrappedCheckedException.CheckedRunnable;
88

99
import java.util.ArrayList;
1010
import java.util.List;
@@ -27,10 +27,10 @@ private Scopes()
2727
* @param tasks a list of tasks
2828
* @throws WrappedCheckedException if any of the tasks threw a checked exceptions
2929
*/
30-
public static void runAll(Task... tasks)
30+
public static void runAll(CheckedRunnable... tasks)
3131
{
3232
List<Exception> exceptions = new ArrayList<>();
33-
for (Task task : tasks)
33+
for (CheckedRunnable task : tasks)
3434
{
3535
try
3636
{

core/src/main/java/com/github/cowwoc/pouch/core/WrappedCheckedException.java

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,41 +40,48 @@ private WrappedCheckedException(Throwable cause)
4040
}
4141

4242
/**
43-
* Wraps any checked exceptions thrown by a callable.
43+
* Wraps any checked exceptions thrown by a {@code Callable}.
4444
*
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
4949
*/
50-
public static <V> V wrap(Callable<V> callable)
50+
public static <V> UncheckedCallable<V> wrap(Callable<V> task)
5151
{
52-
try
52+
return () ->
5353
{
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+
};
6063
}
6164

6265
/**
63-
* Wraps any checked exceptions thrown by a task.
66+
* Wraps any checked exceptions thrown by a {@code ThrowingRunnable}.
6467
*
6568
* @param task the task to execute
69+
* @return a {@code Runnable}
6670
* @throws NullPointerException if {@code task} is null
6771
*/
68-
public static void wrap(Task task)
72+
public static Runnable wrap(CheckedRunnable task)
6973
{
70-
try
71-
{
72-
task.run();
73-
}
74-
catch (Exception e)
74+
return () ->
7575
{
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+
};
7885
}
7986

8087
/**
@@ -111,10 +118,10 @@ public static RuntimeException wrap(String message, Throwable t)
111118
}
112119

113120
/**
114-
* A {@link Callable} without a return value.
121+
* A {@link Runnable} that throws checked exceptions.
115122
*/
116123
@FunctionalInterface
117-
public interface Task
124+
public interface CheckedRunnable
118125
{
119126
/**
120127
* Runs the task.
@@ -123,4 +130,18 @@ public interface Task
123130
*/
124131
void run() throws Exception;
125132
}
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+
}
126147
}

0 commit comments

Comments
 (0)