Interface ThrowingRunnable<X extends Throwable>
-
- Type Parameters:
X- The type of checked exception that can be thrown.
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface ThrowingRunnable<X extends Throwable>
Represents a task that accepts no input and returns no result. This is a checked-exception throwing equivalent ofRunnable.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <X extends Throwable>
ThrowingRunnable<X>checked(Runnable task)Returns a task that performs thetasktask.static <X extends Throwable>
ThrowingRunnable<X>checked(Runnable task, Class<X> errorType)Returns a task that performs thetasktask to its input.static <X extends Throwable>
ThrowingRunnable<X>of(ThrowingRunnable<X> task)Factory method for turningThrowingRunnable-shaped lambdas intoThrowingRunnables.default RunnableonErrorDiscard()Returns a task that performs this task.default <E extends Throwable>
ThrowingRunnable<E>onErrorHandleChecked(ThrowingConsumer<? super X,? extends E> errorHandler)Returns a task that performs this task.default RunnableonErrorHandleUnchecked(Consumer<? super X> errorHandler)Returns a task that performs this task.default <E extends Throwable>
ThrowingRunnable<E>onErrorRunChecked(ThrowingRunnable<? extends E> fallback)Returns a task that performs this task.default RunnableonErrorRunUnchecked(Runnable fallback)Returns a task that performs this task.default <E extends Throwable>
ThrowingRunnable<E>onErrorThrowAsChecked(Function<? super X,? extends E> errorMapper)Returns a task that performs this task.default <E extends RuntimeException>
RunnableonErrorThrowAsUnchecked(Function<? super X,? extends E> errorMapper)Returns a task that performs this task.voidrun()Performs this task.default Runnableunchecked()Returns a task that performs this task.static Runnableunchecked(ThrowingRunnable<?> task)Returns a task that performs thetasktask.
-
-
-
Method Detail
-
onErrorThrowAsChecked
default <E extends Throwable> ThrowingRunnable<E> onErrorThrowAsChecked(Function<? super X,? extends E> errorMapper)
Returns a task that performs this task. Any checked exception thrown by this task is transformed using the given error mapper, and the returned task throws the transformation result.- Type Parameters:
E- The type of checked exception to transform to.- Parameters:
errorMapper- The function to use to transform any checked exception thrown by this task.- Returns:
- A task that transforms any thrown checked exception.
- Throws:
NullPointerException- IferrorMapperisnull.
-
onErrorThrowAsUnchecked
default <E extends RuntimeException> Runnable onErrorThrowAsUnchecked(Function<? super X,? extends E> errorMapper)
Returns a task that performs this task. Any checked exception thrown by this task is transformed using the given error mapper, and the returned task throws the transformation result.- Type Parameters:
E- The type of unchecked exception to transform to.- Parameters:
errorMapper- The function to use to transform any checked exception thrown by this task.- Returns:
- A task that transforms any thrown checked exception.
- Throws:
NullPointerException- IferrorMapperisnull.
-
onErrorHandleChecked
default <E extends Throwable> ThrowingRunnable<E> onErrorHandleChecked(ThrowingConsumer<? super X,? extends E> errorHandler)
Returns a task that performs this task. Any checked exception thrown by this task is handled by the given error handler.- Type Parameters:
E- The type of checked exception that can be thrown by the given error handler.- Parameters:
errorHandler- The operation to perform on any checked exception thrown by this task.- Returns:
- A task that handles any thrown checked exception.
- Throws:
NullPointerException- IferrorHandlerisnull.
-
onErrorHandleUnchecked
default Runnable onErrorHandleUnchecked(Consumer<? super X> errorHandler)
Returns a task that performs this task. Any checked exception thrown by this task is handled by the given error handler.- Parameters:
errorHandler- The operation to perform on any checked exception thrown by this task.- Returns:
- A task that handles any thrown checked exception.
- Throws:
NullPointerException- IferrorHandlerisnull.
-
onErrorRunChecked
default <E extends Throwable> ThrowingRunnable<E> onErrorRunChecked(ThrowingRunnable<? extends E> fallback)
Returns a task that performs this task. If this task throws any checked exception, it is discarded and the given fallback task is invoked.- Type Parameters:
E- The type of checked exception that can be thrown by the given fallback task.- Parameters:
fallback- The task to invoke if this task throws any checked exception.- Returns:
- A task that invokes the
fallbacktask if this task throws any checked exception. - Throws:
NullPointerException- Iffallbackisnull.
-
onErrorRunUnchecked
default Runnable onErrorRunUnchecked(Runnable fallback)
Returns a task that performs this task. If this task throws any checked exception, it is discarded and the given fallback task is invoked.- Parameters:
fallback- The task to invoke if this task throws any checked exception.- Returns:
- A task that invokes the
fallbacktask if this task throws any checked exception. - Throws:
NullPointerException- Iffallbackisnull.
-
onErrorDiscard
default Runnable onErrorDiscard()
Returns a task that performs this task. Any checked exception thrown by this task is discarded.- Returns:
- A task that discards any thrown checked exception.
-
unchecked
default Runnable unchecked()
Returns a task that performs this task. Any checked exception thrown by this tasl is wrapped in anUncheckedExceptionwithout a stack trace.- Returns:
- A task that wraps any checked exception in an
UncheckedException.
-
of
static <X extends Throwable> ThrowingRunnable<X> of(ThrowingRunnable<X> task)
Factory method for turningThrowingRunnable-shaped lambdas intoThrowingRunnables.- Type Parameters:
X- The type of checked exception that can be thrown.- Parameters:
task- The lambda to return asThrowingRunnable.- Returns:
- The given lambda as a
ThrowingRunnable. - Throws:
NullPointerException- Iftaskisnull.
-
unchecked
static Runnable unchecked(ThrowingRunnable<?> task)
Returns a task that performs thetasktask. Any checked exception thrown by thetasktask is wrapped in anUncheckedException.- Parameters:
task- The task to perform when the returned task is performed.- Returns:
- A task that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- Iftaskisnull.
-
checked
static <X extends Throwable> ThrowingRunnable<X> checked(Runnable task)
Returns a task that performs thetasktask. Any checked exception thrown by thetasktask is relayed to the caller. This method allows existingRunnableinstances to be used whereThrowingRunnableis expected.- Type Parameters:
X- The type of checked exception that can be thrown.- Parameters:
task- The task to perform when the returned task is performed.- Returns:
- A task that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- Iftaskisnull.
-
checked
static <X extends Throwable> ThrowingRunnable<X> checked(Runnable task, Class<X> errorType)
Returns a task that performs thetasktask to its input. AnyUncheckedExceptionthrown by thetakstask is unwrapped if its cause is an instance oferrorType, otherwise it is relayed to the caller.- Type Parameters:
X- The type of checked exception that can be thrown.- Parameters:
task- The task to perform when the returned task is performed.errorType- The type of checked exception that can be thrown.- Returns:
- A task that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- IftaskorerrorTypeisnull.
-
-