Interface Function<I,O>
-
- Type Parameters:
I- the type of the inputO- the type of the output
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface Function<I,O>
A single argument function.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceFunction.ConditionalSpec<I,O>A spec for adding conditions to a conditional function.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <T> Function<I,T>andThen(Function<? super O,? extends T> after)Joinsthisfunction with the given function.Oapply(I i)The function implementation.default <T> Function<T,O>compose(Function<? super T,? extends I> before)Joins the given function withthisfunction.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(com.google.common.base.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()Convertsthisfunction into the equivalent JDK type.default com.google.common.base.Function<I,O>toGuavaFunction()Convertsthisfunction 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 toidentity().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.
-
-
-
Method Detail
-
apply
O apply(I i) throws Exception
The function implementation.- Parameters:
i- the input to the function- Returns:
- the output of the function
- Throws:
Exception- any
-
andThen
default <T> Function<I,T> andThen(Function<? super O,? extends T> after)
Joinsthisfunction with the given function.import ratpack.func.Function; import static org.junit.jupiter.api.Assertions.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).- Type Parameters:
T- the type of the final output- Parameters:
after- the function to apply to the result ofthisfunction- Returns:
- the result of applying the given function to
thisfunction
-
compose
default <T> Function<T,O> compose(Function<? super T,? extends I> before) throws Exception
Joins the given function withthisfunction.import ratpack.func.Function; import static org.junit.jupiter.api.Assertions.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).- Type Parameters:
T- the type of the new input- Parameters:
before- the function to applythisfunction to the result of- Returns:
- the result of applying
thisfunction to the result of the given function - Throws:
Exception- any thrown bythisorbefore
-
toFunction
default Function<I,O> toFunction()
Convertsthisfunction into the equivalent JDK type.Any exceptions thrown by
thisfunction will be unchecked viaExceptions.uncheck(Throwable)and rethrown.- Returns:
- this function as a JDK style function.
-
toGuavaFunction
default com.google.common.base.Function<I,O> toGuavaFunction()
Convertsthisfunction into the equivalent Guava type.Any exceptions thrown by
thisfunction will be unchecked viaExceptions.uncheck(Throwable)and rethrown.- Returns:
- this function as a Guava style function.
-
from
static <I,O> Function<I,O> from(Function<I,O> function)
Creates a function of this type from a JDK style function.- Type Parameters:
I- the input typeO- the output type- Parameters:
function- a JDK style function- Returns:
- a Ratpack style function wrapping the given JDK function
-
fromGuava
static <I,O> Function<I,O> fromGuava(com.google.common.base.Function<I,O> function)
Creates a function of this type from a Guava style function.- Type Parameters:
I- the input typeO- the output type- Parameters:
function- a Guava style function- Returns:
- a Ratpack style function wrapping the given Guava function
-
identity
static <T> Function<T,T> identity()
Returns an identity function (return value always same as input).- Type Parameters:
T- the type of the input and output objects to the function- Returns:
- a function that always returns its input argument
-
constant
static <T> Function<Object,T> constant(T t)
Returns a function that always returns the given argument.- Type Parameters:
T- the type of returned value- Parameters:
t- the value to always return- Returns:
- a function that returns the given value
-
when
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 toidentity().This is equivalent to
when(predicate, function, identity()).- Type Parameters:
I- the type of argument and return value- Parameters:
predicate- the condition for the argumentfunction- the function to apply if the predicate applies- Returns:
- a function that delegates to the given function if the predicate applies, else returns the argument
- Since:
- 1.5
- See Also:
when(Predicate, Function, Function),conditional(Function, Action)
-
when
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.- Type Parameters:
I- the type of argumentO- the type of return value- Parameters:
predicate- the condition for the argumentonTrue- the function to apply if the predicate appliesonFalse- the function to apply if the predicate DOES NOT apply- Returns:
- a function that delegates to the first function if the predicate applies, else the second argument
- Since:
- 1.5
- See Also:
when(Predicate, Function),conditional(Function, Action)
-
conditional
static <I,O> Function<I,O> conditional(Action<? super Function.ConditionalSpec<I,O>> conditions) throws Exception
Creates a function that delegates based on the specified conditions.If no conditions match, an
IllegalArgumentExceptionwill be thrown. Useconditional(Function, Action)alternatively to specify a different “else” strategy.- Type Parameters:
I- the input typeO- the output type- Parameters:
conditions- the conditions- Returns:
- a conditional function
- Throws:
Exception- any thrown byconditions- Since:
- 1.5
- See Also:
conditional(Function, Action)
-
conditional
static <I,O> Function<I,O> conditional(Function<? super I,? extends O> onElse, Action<? super Function.ConditionalSpec<I,O>> conditions) throws Exception
Creates a function that delegates based on the specified conditions.If no condition applies, the
onElsefunction will be delegated to.- Type Parameters:
I- the input typeO- the output type- Parameters:
onElse- the function to delegate to if no condition matchesconditions- the conditions- Returns:
- a conditional function
- Throws:
Exception- any thrown byconditions- Since:
- 1.5
- See Also:
conditional(Action)
-
-