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 | 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> |
from(Function<I,O> function)
Creates a function of this type from a JDK style function.
|
default Function<I,O> |
toFunction()
Converts
this function into the equivalent JDK type. |
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;
public class Example {
public static void main(String[] args) throws Exception {
Function<String, String> function = in -> in + "-bar";
assert function.andThen(String::toUpperCase).apply("foo").equals("FOO-BAR");
}
}
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;
public class Example {
public static void main(String... args) throws Exception {
Function<String, String> function = String::toUpperCase;
assert function.compose(in -> in + "-BAR").apply("foo").equals("FOO-BAR");
}
}
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 ExceptionUtils.uncheck(Throwable)
and rethrown.