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 <T> Function<Object,T> |
constant(T t) |
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. |
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 function