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 | Interface and Description |
---|---|
static interface |
Action.ConditionalSpec<I>
A spec for adding conditions to a conditional action.
|
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.
|
static Action<Throwable> |
beforeThrow(Action<? super Throwable> action)
Creates an exception-taking action that executes the given action before throwing the exception.
|
static Action<Throwable> |
beforeThrow(Block block)
Creates an exception-taking action that executes the given block before throwing the exception.
|
static <I> Action<I> |
conditional(Action<? super Action.ConditionalSpec<I>> conditions)
Creates an action that delegates based on the specified conditions.
|
static <I> Action<I> |
conditional(Action<? super I> onElse,
Action<? super Action.ConditionalSpec<I>> conditions)
Creates an action that delegates based on the specified conditions.
|
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 <T> Action<T> |
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> |
suppressAndThrow(Throwable toSuppress)
An action that receives a throwable to thrown, suppressing the given value.
|
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) . |
static <I> Action<I> |
when(Predicate<? super I> predicate,
Action<? super I> action)
Creates an action that delegates to the given action if the given predicate applies, else delegates to
noop() . |
static <I> Action<I> |
when(Predicate<? super I> predicate,
Action<? super I> onTrue,
Action<? super I> onFalse)
Creates an action that delegates to the first action if the given predicate applies, else the second action.
|
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 <T> Action<T> 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 Action<Throwable> suppressAndThrow(Throwable toSuppress)
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 action
default <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 consumerdefault Block curry(T value)
value
- the value to execute this action with when the block is executedstatic Action<Throwable> beforeThrow(Action<? super Throwable> action)
This can be used with methods such as Promise.onError(Action)
to simulate the Java finally
construct.
action
- the action to perform before throwing the exceptionstatic Action<Throwable> beforeThrow(Block block)
This can be used with methods such as Promise.onError(Action)
to simulate the Java finally
construct.
block
- the block to execute before throwing the exceptionstatic <I> Action<I> when(Predicate<? super I> predicate, Action<? super I> action)
noop()
.
This is equivalent to when(predicate, action, noop())
.
I
- the type of argumentpredicate
- the condition for the argumentaction
- the action to execute if the predicate applieswhen(Predicate, Action, Action)
,
conditional(Action, Action)
static <I> Action<I> when(Predicate<? super I> predicate, Action<? super I> onTrue, Action<? super I> onFalse)
I
- the type of argumentpredicate
- the condition for the argumentonTrue
- the action to execute if the predicate appliesonFalse
- the action to execute if the predicate DOES NOT applywhen(Predicate, Action)
,
conditional(Action, Action)
static <I> Action<I> conditional(Action<? super Action.ConditionalSpec<I>> conditions) throws Exception
If no conditions match, an IllegalArgumentException
will be thrown.
Use conditional(Action, Action)
alternatively to specify a different “else” strategy.
I
- the input typeconditions
- the conditionsException
- any thrown by conditions
conditional(Action, Action)
static <I> Action<I> conditional(Action<? super I> onElse, Action<? super Action.ConditionalSpec<I>> conditions) throws Exception
If no condition applies, the onElse
action will be delegated to.
I
- the input typeonElse
- the action to delegate to if no condition matchesconditions
- the conditionsException
- any thrown by conditions
conditional(Action)