I
- the type of the inputO
- the type of the output@FunctionalInterface public interface Function<I,O>
This type serves the same purpose as the JDK's Function
, but allows throwing checked exceptions.
It contains methods for bridging to and from the JDK type.
Modifier and Type | Interface and Description |
---|---|
static interface |
Function.ConditionalSpec<I,O>
A spec for adding conditions to a conditional function.
|
Modifier and Type | Method and Description |
---|---|
default <T> Function<I,T> |
andThen(Function<? super O,? extends T> after)
Joins
this function with the given function. |
O |
apply(I i)
The function implementation.
|
default <T> Function<T,O> |
compose(Function<? super T,? extends I> before)
Joins the given function with
this function. |
static <I,O> Function<I,O> |
conditional(Action<? super Function.ConditionalSpec<I,O>> conditions)
Creates a function that delegates based on the specified conditions.
|
static <I,O> Function<I,O> |
conditional(Function<? super I,? extends O> onElse,
Action<? super Function.ConditionalSpec<I,O>> conditions)
Creates a function that delegates based on the specified conditions.
|
static <T> Function<Object,T> |
constant(T t)
Returns a function that always returns the given argument.
|
static <I,O> Function<I,O> |
from(Function<I,O> function)
Creates a function of this type from a JDK style function.
|
static <I,O> Function<I,O> |
fromGuava(Function<I,O> function)
Creates a function of this type from a Guava style function.
|
static <T> Function<T,T> |
identity()
Returns an identity function (return value always same as input).
|
default Function<I,O> |
toFunction()
Converts
this function into the equivalent JDK type. |
default Function<I,O> |
toGuavaFunction()
Converts
this function into the equivalent Guava type. |
static <I> Function<I,I> |
when(Predicate<? super I> predicate,
Function<? super I,? extends I> function)
Creates a function that delegates to the given function if the given predicate applies, else delegates to
identity() . |
static <I,O> Function<I,O> |
when(Predicate<? super I> predicate,
Function<? super I,? extends O> onTrue,
Function<? super I,? extends O> onFalse)
Creates a function that delegates to the first function if the given predicate applies, else the second function.
|
O apply(I i) throws Exception
i
- the input to the functionException
- anydefault <T> Function<I,T> andThen(Function<? super O,? extends T> after) throws Exception
this
function with the given function.
import ratpack.func.Function;
import static org.junit.Assert.assertEquals;
public class Example {
public static void main(String[] args) throws Exception {
Function<String, String> function = in -> in + "-bar";
assertEquals("FOO-BAR", function.andThen(String::toUpperCase).apply("foo"));
}
}
Analogous to Function.andThen(java.util.function.Function)
.
T
- the type of the final outputafter
- the function to apply to the result of this
functionthis
functionException
- any thrown by this
or after
default <T> Function<T,O> compose(Function<? super T,? extends I> before) throws Exception
this
function.
import ratpack.func.Function;
import static org.junit.Assert.assertEquals;
public class Example {
public static void main(String... args) throws Exception {
Function<String, String> function = String::toUpperCase;
assertEquals("FOO-BAR", function.compose(in -> in + "-BAR").apply("foo"));
}
}
Analogous to Function.compose(java.util.function.Function)
.
T
- the type of the new inputbefore
- the function to apply this
function to the result ofthis
function to the result of the given functionException
- any thrown by this
or before
default Function<I,O> toFunction()
this
function into the equivalent JDK type.
Any exceptions thrown by this
function will be unchecked via Exceptions.uncheck(Throwable)
and rethrown.
default Function<I,O> toGuavaFunction()
this
function into the equivalent Guava type.
Any exceptions thrown by this
function will be unchecked via Exceptions.uncheck(Throwable)
and rethrown.
static <I,O> Function<I,O> from(Function<I,O> function)
I
- the input typeO
- the output typefunction
- a JDK style functionstatic <I,O> Function<I,O> fromGuava(Function<I,O> function)
I
- the input typeO
- the output typefunction
- a Guava style functionstatic <T> Function<T,T> identity()
T
- the type of the input and output objects to the functionstatic <T> Function<Object,T> constant(T t)
T
- the type of returned valuet
- the value to always returnstatic <I> Function<I,I> when(Predicate<? super I> predicate, Function<? super I,? extends I> function)
identity()
.
This is equivalent to when(predicate, function, identity())
.
I
- the type of argument and return valuepredicate
- the condition for the argumentfunction
- the function to apply if the predicate applieswhen(Predicate, Function, Function)
,
conditional(Function, Action)
static <I,O> Function<I,O> when(Predicate<? super I> predicate, Function<? super I,? extends O> onTrue, Function<? super I,? extends O> onFalse)
I
- the type of argumentO
- the type of return valuepredicate
- the condition for the argumentonTrue
- the function to apply if the predicate appliesonFalse
- the function to apply if the predicate DOES NOT applywhen(Predicate, Function)
,
conditional(Function, Action)
static <I,O> Function<I,O> conditional(Action<? super Function.ConditionalSpec<I,O>> conditions) throws Exception
If no conditions match, an IllegalArgumentException
will be thrown.
Use conditional(Function, Action)
alternatively to specify a different “else” strategy.
I
- the input typeO
- the output typeconditions
- the conditionsException
- any thrown by conditions
conditional(Function, Action)
static <I,O> Function<I,O> conditional(Function<? super I,? extends O> onElse, Action<? super Function.ConditionalSpec<I,O>> conditions) throws Exception
If no condition applies, the onElse
function will be delegated to.
I
- the input typeO
- the output typeonElse
- the function to delegate to if no condition matchesconditions
- the conditionsException
- any thrown by conditions
conditional(Action)