Skip to content

Commit

Permalink
WrappedCheckedException.wrap(Runnable) and wrap(Callable) now returns…
Browse files Browse the repository at this point in the history
… a wrapped Runnable/Callable instead of executing the task immediately.
  • Loading branch information
cowwoc committed Sep 29, 2024
1 parent abd5235 commit 7f07e0e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/com/github/cowwoc/pouch/core/Scopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package com.github.cowwoc.pouch.core;

import com.github.cowwoc.pouch.core.WrappedCheckedException.Task;
import com.github.cowwoc.pouch.core.WrappedCheckedException.ThrowingRunnable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -27,10 +27,10 @@ private Scopes()
* @param tasks a list of tasks
* @throws WrappedCheckedException if any of the tasks threw a checked exceptions
*/
public static void runAll(Task... tasks)
public static void runAll(ThrowingRunnable... tasks)
{
List<Exception> exceptions = new ArrayList<>();
for (Task task : tasks)
for (ThrowingRunnable task : tasks)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,48 @@ private WrappedCheckedException(Throwable cause)
}

/**
* Wraps any checked exceptions thrown by a callable.
* Wraps any checked exceptions thrown by a {@code Callable}.
*
* @param callable the task to execute
* @param <V> the type of value returned by {@code callable}
* @return the value returned by {@code callable}
* @throws NullPointerException if {@code callable} is null
* @param task the task to execute
* @param <V> the type of value returned by {@code task}
* @return a {@code Callable} that wraps any checked exceptions thrown by the {@code task}
* @throws NullPointerException if {@code task} is null
*/
public static <V> V wrap(Callable<V> callable)
public static <V> Callable<V> wrap(Callable<V> task)
{
try
return () ->
{
return callable.call();
}
catch (Exception e)
{
throw WrappedCheckedException.wrap(e);
}
try
{
return task.call();
}
catch (Exception e)
{
throw WrappedCheckedException.wrap(e);
}
};
}

/**
* Wraps any checked exceptions thrown by a task.
* Wraps any checked exceptions thrown by a {@code Runnable}.
*
* @param task the task to execute
* @return a {@code Runnable} that wraps any checked exceptions thrown by the {@code task}
* @throws NullPointerException if {@code task} is null
*/
public static void wrap(Task task)
public static Runnable wrap(ThrowingRunnable task)
{
try
{
task.run();
}
catch (Exception e)
return () ->
{
throw WrappedCheckedException.wrap(e);
}
try
{
task.run();
}
catch (Exception e)
{
throw WrappedCheckedException.wrap(e);
}
};
}

/**
Expand Down Expand Up @@ -111,10 +118,10 @@ public static RuntimeException wrap(String message, Throwable t)
}

/**
* A {@link Callable} without a return value.
* A {@link Runnable} that throws checked exceptions.
*/
@FunctionalInterface
public interface Task
public interface ThrowingRunnable
{
/**
* Runs the task.
Expand Down

0 comments on commit 7f07e0e

Please sign in to comment.