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 e3b107f
Show file tree
Hide file tree
Showing 2 changed files with 48 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.CheckedRunnable;

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(CheckedRunnable... tasks)
{
List<Exception> exceptions = new ArrayList<>();
for (Task task : tasks)
for (CheckedRunnable 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 does not throw any checked exceptions
* @throws NullPointerException if {@code task} is null
*/
public static <V> V wrap(Callable<V> callable)
public static <V> UncheckedCallable<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 ThrowingRunnable}.
*
* @param task the task to execute
* @return a {@code Runnable}
* @throws NullPointerException if {@code task} is null
*/
public static void wrap(Task task)
public static Runnable wrap(CheckedRunnable 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 CheckedRunnable
{
/**
* Runs the task.
Expand All @@ -123,4 +130,18 @@ public interface Task
*/
void run() throws Exception;
}

/**
* A {@link Callable} that does not throw any checked exceptions.
*/
@FunctionalInterface
public interface UncheckedCallable<V>
{
/**
* Runs the task.
*
* @throws WrappedCheckedException if unable to compute a result
*/
V call();
}
}

0 comments on commit e3b107f

Please sign in to comment.