Class TypeTestFunction<T,​R>

  • Type Parameters:
    T - the type of the argument
    R - the type of the wrapped return value
    All Implemented Interfaces:
    CondFunction<Object,​R>, Function<Object,​Optional<R>>

    public class TypeTestFunction<T,​R>
    extends Object
    implements CondFunction<Object,​R>
    Interface for conditional functions which do a type test of the argument value. If the argument passes the type test, the value is mapped to a result and the result is wrapped into the Optional. But if the type test is not successful, Optional.empty() is returned.
    Author:
    Herbert Praehofer
    • Field Detail

      • clazz

        private final Class<T> clazz
        The class for the typetest.
      • func

        private final Function<? super T,​? extends R> func
        The function to apply if the test is successful.
    • Constructor Detail

      • TypeTestFunction

        private TypeTestFunction​(Class<T> clazz,
                                 Function<? super T,​? extends R> func)
        Private constructor.
        Parameters:
        clazz - the class for the typetest
        func - the function to apply if the test is successful
    • Method Detail

      • of

        public static <T,​R> TypeTestFunction<T,​R> of​(Class<T> clazz,
                                                                 Function<? super T,​? extends R> func)
        Creates a typetest function from a class and a function.
        Type Parameters:
        T - the type of the argument
        R - the type of the wrapped return value
        Parameters:
        clazz - the class for the typetest
        func - the function
        Returns:
        the typetest function
      • test

        public boolean test​(Object o)
        Tests the object if it is of the specific class.
        Specified by:
        test in interface CondFunction<T,​R>
        Parameters:
        o - the object to test
        Returns:
        true if the object is of the specific class.
      • apply

        public Optional<R> apply​(Object o)
        Tests the object and then applies the function on the object. The result is wrapped into an Optional.
        Specified by:
        apply in interface Function<T,​R>
        Parameters:
        o - the object to test and map
        Returns:
        the result of the function application in an Optional, if the test was successful; Optional.empty() otherwise