Package ratpack.func

Interface Function<I,​O>

  • Type Parameters:
    I - the type of the input
    O - 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 interface  Function.ConditionalSpec<I,​O>
      A spec for adding conditions to a conditional 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)
        Joins this function 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 of this function
        Returns:
        the result of applying the given function to this function
      • compose

        default <T> Function<T,​O> compose​(Function<? super T,​? extends I> before)
                                         throws Exception
        Joins the given function with this 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 = 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 apply this function to the result of
        Returns:
        the result of applying this function to the result of the given function
        Throws:
        Exception - any thrown by this or before
      • toFunction

        default Function<I,​O> toFunction()
        Converts this function into the equivalent JDK type.

        Any exceptions thrown by this function will be unchecked via Exceptions.uncheck(Throwable) and rethrown.

        Returns:
        this function as a JDK style function.
      • toGuavaFunction

        default com.google.common.base.Function<I,​O> toGuavaFunction()
        Converts this function into the equivalent Guava type.

        Any exceptions thrown by this function will be unchecked via Exceptions.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 type
        O - 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 type
        O - 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,​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 argument
        O - the type of return value
        Parameters:
        predicate - the condition for the argument
        onTrue - the function to apply if the predicate applies
        onFalse - 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​(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 onElse function will be delegated to.

        Type Parameters:
        I - the input type
        O - the output type
        Parameters:
        onElse - the function to delegate to if no condition matches
        conditions - the conditions
        Returns:
        a conditional function
        Throws:
        Exception - any thrown by conditions
        Since:
        1.5
        See Also:
        conditional(Action)