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 |
|---|---|
default <O extends T> |
append(Action<? super O> action)
Returns a new action that executes this action and then the given action.
|
default Block |
curry(T value)
Creates a block that executes this action with the given value when called.
|
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(Block block) |
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. |
default <O extends T> |
prepend(Action<? super O> action)
Returns a new action that executes the given action and then this 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. |
default <O extends T> |
uncheckedWith(O o)
Like
with(Object), but unchecks any exceptions thrown by the action via Exceptions.uncheck(Throwable). |
static <T> T |
uncheckedWith(T t,
Action<? super T> action)
Like
with(Object, Action), but unchecks any exceptions thrown by the action via Exceptions.uncheck(Throwable). |
default <O extends T> |
with(O o)
Executes with the given argument, then returns the argument.
|
static <T> T |
with(T t,
Action<? super T> action)
Executes the action with the given argument, then returns the argument.
|
void execute(T t) throws Exception
t - the thing to execute the action againstException - if anything goes wrongstatic 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 actiondefault <O extends T> Action<O> append(Action<? super O> action)
O - the type of object the action acceptsaction - the action to execute after this actiondefault <O extends T> Action<O> prepend(Action<? super O> action)
O - the type of object the action acceptsaction - the action to execute before this actionstatic Action<Throwable> throwException()
static <T> Action<T> throwException(Throwable throwable)
The exception is thrown via Exceptions.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> T with(T t,
Action<? super T> action)
throws Exception
import ratpack.func.Action;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
public class Example {
public static void main(String... args) throws Exception {
assertEquals("foo", Action.with(new ArrayList<>(), list -> list.add("foo")).get(0));
}
}
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 actiondefault <O extends T> O with(O o) throws Exception
import ratpack.func.Action;
import java.util.List;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
public class Example {
public static void main(String... args) throws Exception {
assertEquals("foo", run(list -> list.add("foo")).get(0));
}
private static List<String> run(Action<? super List<String>> action) throws Exception {
return action.with(new ArrayList<>());
}
}
O - the type of the argumento - the argument to execute the given action witho)Exception - any thrown by execute(Object)static <T> T uncheckedWith(T t,
Action<? super T> action)
with(Object, Action), but unchecks any exceptions thrown by the action via Exceptions.uncheck(Throwable).T - the type of the argumentt - the argument to execute the given action withaction - the action to execute with the given argumentt)default <O extends T> O uncheckedWith(O o)
with(Object), but unchecks any exceptions thrown by the action via Exceptions.uncheck(Throwable).O - the type of the argumento - the argument to execute witho)default Consumer<T> toConsumer()
Consumer from this action.
Any exceptions thrown by this action will be unchecked via Exceptions.uncheck(Throwable) and rethrown.
static <T> Action<T> from(Consumer<T> consumer)
T - the type of object this action acceptsconsumer - the JDK consumer