Class Functions



  • public abstract class Functions
    extends Object
    Utilities relating to functions.
    • Field Detail

      • NULLS_FIRST_COMPARATOR

        private static final Comparator NULLS_FIRST_COMPARATOR
      • NULLS_LAST_COMPARATOR

        private static final Comparator NULLS_LAST_COMPARATOR
      • NULLS_LAST_REVERSE_COMPARATOR

        private static final Comparator NULLS_LAST_REVERSE_COMPARATOR
      • NULLS_FIRST_REVERSE_COMPARATOR

        private static final Comparator NULLS_FIRST_REVERSE_COMPARATOR
      • CONSTANT_NULL_FUNCTION1

        private static final Function1 CONSTANT_NULL_FUNCTION1
      • TO_STRING_FUNCTION1

        private static final Function1 TO_STRING_FUNCTION1
    • Constructor Detail

      • Functions

        private Functions​()
    • Method Detail

      • map

        private static <K,V> Map<K,V> map​(K k,
                                          V v,
                                          Object... rest)
      • inverse

        private static <K,V> Map<V,K> inverse​(Map<K,V> map)
      • constant

        public static <T,R> Function1<T,R> constant​(R r)
        Returns a 1-parameter function that always returns the same value.
      • constantNull

        public static <T,R> Function1<T,R> constantNull​()
        Returns a 1-parameter function that always returns null.
      • truePredicate1

        public static <T> Predicate1<T> truePredicate1​()
        A predicate with one parameter that always returns true.
        Type Parameters:
        T - First parameter type
        Returns:
        Predicate that always returns true
      • falsePredicate1

        public static <T> Predicate1<T> falsePredicate1​()
        A predicate with one parameter that always returns true.
        Type Parameters:
        T - First parameter type
        Returns:
        Predicate that always returns true
      • truePredicate2

        public static <T1,T2> Predicate2<T1,T2> truePredicate2​()
        A predicate with two parameters that always returns true.
        Type Parameters:
        T1 - First parameter type
        T2 - Second parameter type
        Returns:
        Predicate that always returns true
      • falsePredicate2

        public static <T1,T2> Predicate2<T1,T2> falsePredicate2​()
        A predicate with two parameters that always returns false.
        Type Parameters:
        T1 - First parameter type
        T2 - Second parameter type
        Returns:
        Predicate that always returns false
      • identitySelector

        public static <TSource> Function1<TSource,TSource> identitySelector​()
      • toStringSelector

        public static <TSource> Function1<TSource,String> toStringSelector​()
        Returns a selector that calls the Object.toString() method on each element.
      • ofTypePredicate

        public static <T,T2> Predicate1<T> ofTypePredicate​(Class<T2> clazz)
        Creates a predicate that returns whether an object is an instance of a particular type or is null.
        Type Parameters:
        T - Type of objects to test
        T2 - Desired type
        Parameters:
        clazz - Desired type
        Returns:
        Predicate that tests for desired type
      • toPredicate

        public static <T1,T2> Predicate2<T1,T2> toPredicate​(Function2<T1,T2,Boolean> function)
        Converts a 2-parameter function to a predicate.
      • toPredicate

        private static <T> Predicate1<T> toPredicate​(Function1<T,Boolean> function)
        Converts a 1-parameter function to a predicate.
      • functionClass

        public static Class<? extends Function> functionClass​(Type aClass)
        Returns the appropriate interface for a lambda function with 1 argument and the given return type.

        For example:

        functionClass(Integer.TYPE) returns IntegerFunction1.class functionClass(String.class) returns Function1.class
        Parameters:
        aClass - Return type
        Returns:
        Function class
      • adapt

        @Deprecated
        public static <T1,R> List<R> adapt​(List<T1> list,
                                           Function1<T1,R> f)
        Deprecated. Use Lists.transform(java.util.List<F>, com.google.common.base.Function<? super F, ? extends T>)
        Creates a view of a list that applies a function to each element.
      • adapt

        @Deprecated
        public static <T,R> List<R> adapt​(T[] ts,
                                          Function1<T,R> f)
        Deprecated. Use Lists.transform(java.util.List<F>, com.google.common.base.Function<? super F, ? extends T>) and Arrays.asList(Object[])
        Creates a view of an array that applies a function to each element.
      • apply

        public static <T1,R> List<R> apply​(List<T1> list,
                                           Function1<T1,R> f)
        Creates a copy of a list, applying a function to each element.
      • filter

        public static <E> List<E> filter​(List<E> list,
                                         Predicate1<E> predicate)
        Returns a list that contains only elements of list that match predicate. Avoids allocating a list if all elements match or no elements match.
      • exists

        public static <E> boolean exists​(List<? extends E> list,
                                         Predicate1<E> predicate)
        Returns whether there is an element in list for which predicate is true.
      • all

        public static <E> boolean all​(List<? extends E> list,
                                      Predicate1<E> predicate)
        Returns whether predicate is true for all elements of list.
      • generate

        public static <E> List<E> generate​(int size,
                                           Function1<Integer,E> fn)
        Returns a list generated by applying a function to each index between 0 and size - 1.
      • ignore0

        public static <R> Function0<R> ignore0​()
        Returns a function of arity 0 that does nothing.
        Type Parameters:
        R - Return type
        Returns:
        Function that does nothing.
      • ignore1

        public static <R,T0> Function1<R,T0> ignore1​()
        Returns a function of arity 1 that does nothing.
        Type Parameters:
        R - Return type
        T0 - Type of parameter 0
        Returns:
        Function that does nothing.
      • ignore2

        public static <R,T0,T1> Function2<R,T0,T1> ignore2​()
        Returns a function of arity 2 that does nothing.
        Type Parameters:
        R - Return type
        T0 - Type of parameter 0
        T1 - Type of parameter 1
        Returns:
        Function that does nothing.
      • nullsComparator

        public static <T extends Comparable<T>> Comparator<T> nullsComparator​(boolean nullsFirst,
                                                                              boolean reverse)
        Returns a Comparator that handles null values.
        Parameters:
        nullsFirst - Whether nulls come before all other values
        reverse - Whether to reverse the usual order of Comparables