T - The type of thing.@FunctionalInterface public interface Action<T>
This type serves the same purpose as the JDK's Consumer, but allows throwing checked exceptions.
It contains methods for bridging to and from the JDK type.
| Modifier and Type | Method and Description |
|---|---|
void |
execute(T t)
Executes the action against the given thing.
|
static <T> Action<T> |
from(Consumer<T> consumer)
Creates an action from a JDK consumer.
|
static <T> Action<T> |
ignoreArg(NoArgAction noArgAction) |
static <T> Action<T> |
join(Action<? super T>... actions)
Returns a new action that executes the given actions in order.
|
static Action<Object> |
noop()
Returns an action that does precisely nothing.
|
static <T> Action<? super T> |
noopIfNull(Action<T> action)
If the given action is
null, returns noop(), otherwise returns the given action. |
static Action<Throwable> |
throwException()
Returns an action that receives a throwable and immediately throws it.
|
static <T> Action<T> |
throwException(Throwable throwable)
Returns an action that immediately throws the given exception.
|
default Consumer<T> |
toConsumer()
Creates a JDK
Consumer from this action. |
static <T> T |
with(T t,
Action<? super T> action)
Executes the action with the given argument, then returns the argument.
|
static Action<Object> noop()
static <T> Action<? super T> noopIfNull(@Nullable Action<T> action)
null, returns noop(), otherwise returns the given action.T - the type of parameter received by the actionaction - an action, maybe null.action param if it is not null, else a noop().@SafeVarargs static <T> Action<T> join(Action<? super T>... actions)
T - the type of object the action acceptsactions - the actions to join into one actionstatic Action<Throwable> throwException()
static <T> Action<T> throwException(Throwable throwable)
The exception is thrown via ExceptionUtils.toException(Throwable)
T - the argument type (anything, as the argument is ignored)throwable - the throwable to immediately throw when the returned action is executedstatic <T> Action<T> ignoreArg(NoArgAction noArgAction)
static <T> T with(T t,
Action<? super T> action)
throws Exception
import ratpack.func.Action;
import java.util.ArrayList;
public class Example {
public static void main(String... args) throws Exception {
assert Action.with(new ArrayList<>(), list -> list.add("foo")).get(0).equals("foo");
}
}
T - the type of the argumentt - the argument to execute the given action withaction - the action to execute with the given argumentt)Exception - any thrown by actionvoid execute(T t) throws Exception
t - the thing to execute the action againstException - if anything goes wrongdefault Consumer<T> toConsumer()
Consumer from this action.
Any exceptions thrown by this action will be unchecked via ExceptionUtils.uncheck(Throwable) and rethrown.