Class Algorithms


  • public final class Algorithms
    extends java.lang.Object
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Algorithms.Action<T>  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Set<java.lang.String> annotationMethods  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Algorithms()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <A> UnaryPredicate<A> and​(UnaryPredicate<A> arg1, UnaryPredicate<A> arg2)  
      static java.util.List convertToList​(java.lang.Object arg)
      Converts obj from an Array to a List, if obj is an array.
      static java.lang.String convertToString​(java.lang.Object arg)
      Convert argument to String, either by toString, ot Arrays.toString.
      static <T> T doPrivileged​(Algorithms.Action<T> func)  
      static <T> UnaryPredicate<T> FALSE​(java.lang.Class<T> cls)  
      static <A> void filter​(java.util.List<A> arg, java.util.List<A> result, UnaryPredicate<A> predicate)  
      static <A> java.util.List<A> filter​(java.util.List<A> arg, UnaryPredicate<A> predicate)  
      static <A> A find​(java.util.List<A> arg, UnaryPredicate<A> predicate)  
      static <S,​T>
      java.util.List<T>
      flatten​(java.util.List<S> list, UnaryFunction<S,​java.util.List<T>> map)
      Flatten the results of applying map to list into a list of T.
      static <A,​R>
      R
      fold​(java.util.List<A> list, R initial, BinaryFunction<R,​A,​R> func)  
      static java.util.Map<java.lang.String,​java.lang.Object> getAnnotationValues​(java.lang.annotation.Annotation ann, boolean convertArraysToLists)
      Given an annotation, return a Map that maps each field (given by a method name) to its value in the annotation.
      private static java.util.List<java.lang.reflect.Method> getDeclaredMethods​(java.lang.Class<?> cls)  
      static <T> T getFirst​(java.util.Collection<T> list, java.lang.Runnable handleEmptyList)
      Return the first element of the list, or invoke handleEmptyList if list is empty.
      static <T> java.util.List<T> list​(T... arg)
      Deprecated.
      replaced by Arrays.asList(java.lang.Object...)
      private static <T> java.security.PrivilegedAction<T> makePrivilegedAction​(Algorithms.Action<T> act)  
      static <A,​R>
      void
      map​(java.util.Collection<A> arg, java.util.Collection<R> result, UnaryFunction<A,​R> func)  
      static <A,​R>
      java.util.List<R>
      map​(java.util.List<A> arg, UnaryFunction<A,​R> func)  
      static <K,​A,​R>
      java.util.Map<K,​R>
      map​(java.util.Map<K,​A> arg, UnaryFunction<A,​R> func)  
      static <K,​V>
      java.util.Map<K,​V>
      map​(Pair<K,​V>... pairs)  
      static <A,​R>
      UnaryFunction<A,​R>
      mapToFunction​(java.util.Map<A,​R> map)  
      static <A> UnaryPredicate<A> not​(UnaryPredicate<A> arg1)  
      static <A> UnaryPredicate<A> or​(UnaryPredicate<A> arg1, UnaryPredicate<A> arg2)  
      static <S,​T>
      Pair<S,​T>
      pair​(S first, T second)  
      static <T> UnaryPredicate<T> TRUE​(java.lang.Class<T> cls)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • annotationMethods

        private static java.util.Set<java.lang.String> annotationMethods
    • Constructor Detail

      • Algorithms

        private Algorithms()
    • Method Detail

      • list

        public static <T> java.util.List<T> list​(T... arg)
        Deprecated.
        replaced by Arrays.asList(java.lang.Object...)
        Converts an array of objects into a list
        Type Parameters:
        T - type of objects
        Parameters:
        arg - the array of objects
        Returns:
        list of objects
      • pair

        public static <S,​T> Pair<S,​T> pair​(S first,
                                                       T second)
      • map

        public static <K,​V> java.util.Map<K,​V> map​(Pair<K,​V>... pairs)
      • mapToFunction

        public static <A,​R> UnaryFunction<A,​R> mapToFunction​(java.util.Map<A,​R> map)
      • map

        public static <A,​R> void map​(java.util.Collection<A> arg,
                                           java.util.Collection<R> result,
                                           UnaryFunction<A,​R> func)
      • map

        public static <K,​A,​R> java.util.Map<K,​R> map​(java.util.Map<K,​A> arg,
                                                                       UnaryFunction<A,​R> func)
      • map

        public static <A,​R> java.util.List<R> map​(java.util.List<A> arg,
                                                        UnaryFunction<A,​R> func)
      • FALSE

        public static <T> UnaryPredicate<T> FALSE​(java.lang.Class<T> cls)
      • TRUE

        public static <T> UnaryPredicate<T> TRUE​(java.lang.Class<T> cls)
      • filter

        public static <A> void filter​(java.util.List<A> arg,
                                      java.util.List<A> result,
                                      UnaryPredicate<A> predicate)
      • filter

        public static <A> java.util.List<A> filter​(java.util.List<A> arg,
                                                   UnaryPredicate<A> predicate)
      • find

        public static <A> A find​(java.util.List<A> arg,
                                 UnaryPredicate<A> predicate)
      • fold

        public static <A,​R> R fold​(java.util.List<A> list,
                                         R initial,
                                         BinaryFunction<R,​A,​R> func)
      • flatten

        public static <S,​T> java.util.List<T> flatten​(java.util.List<S> list,
                                                            UnaryFunction<S,​java.util.List<T>> map)
        Flatten the results of applying map to list into a list of T.
        Type Parameters:
        S - Type of elements of list.
        T - Type of elements of result.
        Parameters:
        list - List of elements of type S.
        map - function mapping S to List<T>.
        Returns:
        List<T> containing results of applying map to each element of list.
      • getFirst

        public static <T> T getFirst​(java.util.Collection<T> list,
                                     java.lang.Runnable handleEmptyList)
        Return the first element of the list, or invoke handleEmptyList if list is empty.
        Type Parameters:
        T - The type of the list element.
        Parameters:
        list - The list
        handleEmptyList - A runnable to call when the list is empty. Typically throws an exception.
        Returns:
        The first element of the list, if any.
      • convertToList

        public static java.util.List convertToList​(java.lang.Object arg)
        Converts obj from an Array to a List, if obj is an array. Otherwise just returns a List containing obj.
      • convertToString

        public static java.lang.String convertToString​(java.lang.Object arg)
        Convert argument to String, either by toString, ot Arrays.toString.
        Parameters:
        arg - Object to convert.
      • getDeclaredMethods

        private static java.util.List<java.lang.reflect.Method> getDeclaredMethods​(java.lang.Class<?> cls)
      • getAnnotationValues

        public static java.util.Map<java.lang.String,​java.lang.Object> getAnnotationValues​(java.lang.annotation.Annotation ann,
                                                                                                 boolean convertArraysToLists)
        Given an annotation, return a Map that maps each field (given by a method name) to its value in the annotation. If the value is an annotation, that value is recursively converted into a Map in the same way.
        Parameters:
        ann - The annotation to examine.
        convertArraysToLists - true if annotation values of array type should be converted to an appropriate list. This is often MUCH more useful, but some contexts require arrays.
        Returns:
        A map of annotation fields to their values.
      • makePrivilegedAction

        private static <T> java.security.PrivilegedAction<T> makePrivilegedAction​(Algorithms.Action<T> act)