Class XFunctionalUtils


  • public final class XFunctionalUtils
    extends Object
    Utilities related to java.util.function
    Since:
    8.0.4
    Author:
    XIMA MEDIA GmbH
    • Method Detail

      • andPredicate

        @SafeVarargs
        public static <T> Predicate<T> andPredicate​(Predicate<? super T>... predicates)
        Combines all predicates with a logical AND. Creates a combined predicate that checks whether all the given predicates are true. Returns true if no predicates are given. Has a better name for static imports, and uses the native Predicate interface. Predicates that are null are ignored.
        Type Parameters:
        T - Type of the items to test.
        Parameters:
        predicates - The predicates to combine.
        Returns:
        A predicate that checks whether all the given predicates are true.
      • andPredicate

        public static <T> Predicate<T> andPredicate​(Iterable<? extends Predicate<? super T>> predicates)
        Combines all predicates with a logical AND. Creates a combined predicate that checks whether all the given predicates are true. Returns true if no predicates are given. Has a better name for static imports, and uses the native Predicate interface. Predicates that are null are ignored.
        Type Parameters:
        T - Type of the items to test.
        Parameters:
        predicates - The predicates to combine.
        Returns:
        A predicate that checks whether all the given predicates are true.
      • catchingBiFunction

        public static <I1,​I2,​O> BiFunction<I1,​I2,​O> catchingBiFunction​(IThrowingBiFunction<I1,​I2,​O> fn)
        Wraps a throwing bi-function into a bi-function that catches checked exceptions and rethrows them as runtime exceptions.
        Type Parameters:
        I1 - First input type.
        I2 - Second input type.
        O - Output type.
        Parameters:
        fn - Throwing function to wrap.
        Returns:
        Function that catches checked exceptions and rethrows them as runtime exceptions.
      • catchingFunction

        public static <I,​O> Function<I,​O> catchingFunction​(IThrowingFunction<I,​O> fn)
        Wraps a throwing function into a function that catches checked exceptions and rethrows them as runtime exceptions.
        Type Parameters:
        I - Input type.
        O - Output type.
        Parameters:
        fn - Throwing function to wrap.
        Returns:
        Function that catches checked exceptions and rethrows them as runtime exceptions.
      • catchingSupplier

        public static <O> Supplier<O> catchingSupplier​(IThrowingSupplier<O> fn)
        Wraps a throwing supplier into a supplier that catches checked exceptions and rethrows them as runtime exceptions.
        Type Parameters:
        O - Output type.
        Parameters:
        fn - Throwing supplier to wrap.
        Returns:
        Supplier that catches checked exceptions and rethrows them as runtime exceptions.
      • composeFunction

        public static <A,​B,​C> Function<A,​C> composeFunction​(Function<? super A,​? extends B> f,
                                                                              Function<? super B,​? extends C> g)
        Composes two functions, applying the first function and then the second function to the input. Equivalent to f.andThen(g) or g(f(x)). This order was chosen intentionally to correspond better with the common Java coding style of chaining method calls.
          composeFunction(toRootLowerCase, Locale::getDisplayName)
        
          // The above composition is equivalent to the following lambda expression.
          // Note the order of the functions is the same.
          locale -> toRootLowerCase(locale.getDisplayName())
         
        Type Parameters:
        A - The input type of the first function.
        B - The output type of the first function and the input type of the second function.
        C - The output type of the second function.
        Parameters:
        f - The first function to apply. When null, a constant function supplying null is used.
        g - The second function to apply. When null, a constant function supplying null is used.
        Returns:
        A function that applies the first function and then the second function to the input.
      • constantBiFunction

        public static <A,​B,​C> BiFunction<A,​B,​C> constantBiFunction​(C value)
        Returns a bi-function that always returns the same value.
        Type Parameters:
        A - Type of the first input.
        B - Type of the second input.
        C - Type of the output.
        Parameters:
        value - The value to return.
        Returns:
        A bi-function that always returns the same value.
      • constantFunction

        public static <A,​B> Function<A,​B> constantFunction​(B value)
        Returns a function that always returns the same value.
        Type Parameters:
        A - Type of the input.
        B - Type of the output.
        Parameters:
        value - The value to return.
        Returns:
        A function that always returns the same value.
      • constantIntFunction

        public static <T> IntFunction<T> constantIntFunction​(T value)
        Returns an int function that always returns the same value.
        Type Parameters:
        T - Type of the value.
        Parameters:
        value - The value to return.
        Returns:
        An int function that always returns the same value.
      • constantPredicate

        public static <T> Predicate<? super T> constantPredicate​(boolean value)
        Returns a predicate that always returns the same value.
        Type Parameters:
        T - Type of the input.
        Parameters:
        value - The value to return.
        Returns:
        A predicate that always returns the same value.
      • constantSupplier

        public static <T> Supplier<T> constantSupplier​(T value)
        Returns a supplier that always returns the same value.
        Type Parameters:
        T - Type of the value.
        Parameters:
        value - The value to return.
        Returns:
        A supplier that always returns the same value.
      • curryBiFunction

        public static <T1,​T2,​R> Supplier<R> curryBiFunction​(BiFunction<? super T1,​? super T2,​? extends R> fn,
                                                                        T1 arg1,
                                                                        T2 arg2)
        Curries both arguments of a bi-variate function with the given value.
        Type Parameters:
        T1 - The type of the first argument.
        T2 - The type of the second argument.
        R - The return type of the function.
        Parameters:
        fn - The function to curry.
        arg1 - The first argument to curry.
        arg2 - The second argument to curry.
        Returns:
        A supplier that invokes the given function with the given arguments and returns that result.
      • curryBiFunction

        public static <T1,​T2,​R> Function<T2,​R> curryBiFunction​(BiFunction<? super T1,​? super T2,​? extends R> fn,
                                                                                 T1 arg1)
        Curries the first argument of a bi-variate function with the given value.
        Type Parameters:
        T1 - The type of the first argument.
        T2 - The type of the second argument.
        R - The return type of the function.
        Parameters:
        fn - The function to curry.
        arg1 - The argument to curry.
        Returns:
        A function that takes a value of the second type and returns the result of invoking the given function with that the given first argument and that value.
      • curryBiFunctionLast

        public static <T1,​T2,​R> Function<T1,​R> curryBiFunctionLast​(BiFunction<? super T1,​? super T2,​? extends R> fn,
                                                                                     T2 arg2)
        Curries the second argument of a bi-variate function with the given value.
        Type Parameters:
        T1 - The type of the first argument.
        T2 - The type of the second argument.
        R - The return type of the function.
        Parameters:
        fn - The function to curry.
        arg2 - The argument to curry.
        Returns:
        A function that takes a value of the first type and returns the result of invoking the given function with that value and the given second argument.
      • endingWith

        public static Predicate<String> endingWith​(String suffix)
        Creates a predicate that checks whether the input ends with the given suffix. null strings are always treated as empty strings.
        Parameters:
        suffix - The suffix to check for.
        Returns:
        A predicate that checks whether the input ends with the given suffix.
      • endingWithAny

        public static Predicate<String> endingWithAny​(Iterable<String> suffixes)
        Creates a predicate that checks whether the input ends with any of the given suffixes. null strings are always treated as empty strings.
        Parameters:
        suffixes - The suffixes to check for.
        Returns:
        A predicate that checks whether the input ends with any of the given suffixes.
      • equalTo

        public static <T> Predicate<T> equalTo​(T value)
        Creates a predicate that checks whether the input is equal to the given value.
        Type Parameters:
        T - The input type.
        Parameters:
        value - The key to compare to.
        Returns:
        A predicate that checks whether a property of the input equals the given key.
      • equalTo

        public static <T,​K> Predicate<T> equalTo​(K key,
                                                       Function<? super T,​? extends K> keyExtractor)
        Creates a predicate that extracts the key from the input using the given key extractor and compares it to the given key.
        Type Parameters:
        T - The input type.
        K - The key type.
        Parameters:
        key - The key to compare to.
        keyExtractor - The function to extract the key from the input.
        Returns:
        A predicate that checks whether a property of the input equals the given key.
      • equalToIgnoreCase

        public static <T extends CharSequencePredicate<T> equalToIgnoreCase​(String value)
        Creates a predicate that checks whether the input is equal to the given value, ignoring case.
        Type Parameters:
        T - The input type.
        Parameters:
        value - The key to compare to.
        Returns:
        A predicate that checks whether a property of the input equals the given key, ignoring case.
      • orPredicate

        @SafeVarargs
        public static <T> Predicate<T> orPredicate​(Predicate<? super T>... predicates)
        Combines all predicates with a logical OR. Creates a combined predicate that checks whether any of the given predicates are true. Returns false if no predicates are given. Has a better name for static imports, and uses the native Predicate interface. Predicates that are null are ignored.
        Type Parameters:
        T - Type of the items to test.
        Parameters:
        predicates - The predicates to combine.
        Returns:
        A predicate that checks whether any of the given predicates are true.
      • orPredicate

        public static <T> Predicate<T> orPredicate​(Iterable<? extends Predicate<? super T>> predicates)
        Combines all predicates with a logical OR. Creates a combined predicate that checks whether any of the given predicates are true. Returns false if no predicates are given. Has a better name for static imports, and uses the native Predicate interface. Predicates that are null are ignored.
        Type Parameters:
        T - Type of the items to test.
        Parameters:
        predicates - The predicates to combine.
        Returns:
        A predicate that checks whether any of the given predicates are true.
      • startingWith

        public static Predicate<String> startingWith​(String prefix)
        Creates a predicate that checks whether the input starts with the given prefix.
        Parameters:
        prefix - The prefix to check for.
        Returns:
        A predicate that checks whether the input starts with the given prefix.
      • startingWithAny

        public static Predicate<String> startingWithAny​(Iterable<String> prefixes)
        Creates a predicate that checks whether the input start with any of the given suffixes. null strings are always treated as empty strings.
        Parameters:
        prefixes - The prefixes to check for.
        Returns:
        A predicate that checks whether the input ends with any of the given suffixes.
      • takeFirst

        public static <R> BinaryOperator<R> takeFirst()
        Gets a binary operator that returns its first argument, useful e.g. for map merge functions.
        Type Parameters:
        R - Type of the items.
        Returns:
        A binary operator returning its first argument and discarding its second argument.
      • takeMax

        public static <R> BinaryOperator<R> takeMax​(Comparator<R> comparator)
        Gets a binary operator that returns the largest of both arguments, useful e.g. for map merge functions. When both arguments are equal, the second argument is returned.
        Type Parameters:
        R - Type of the arguments.
        Parameters:
        comparator - Comparator for comparing two arguments.
        Returns:
        A binary operator returning the largest of both given arguments.
      • takeMin

        public static <R> BinaryOperator<R> takeMin​(Comparator<R> comparator)
        Gets a binary operator that returns the smallest of both arguments, useful e.g. for map merge functions. When both arguments are equal, the first argument is returned.
        Type Parameters:
        R - Type of the arguments.
        Parameters:
        comparator - Comparator for comparing two arguments.
        Returns:
        A binary operator returning the largest of both given arguments.
      • takeSecond

        public static <R> BinaryOperator<R> takeSecond()
        A binary operator that returns its second argument, useful e.g. for map merge functions.
        Type Parameters:
        R - Type of the items.
        Returns:
        A binary operator returning its second argument and discarding its first argument.