Class CollectionUtils


  • public final class CollectionUtils
    extends Object
    A simple utility class to simplify interactions with collections.
    • Method Detail

      • removeBlankEntries

        public static Set<String> removeBlankEntries​(Set<String> originalSet)
        From the original Set of Strings, remove any entries that are blank.
        Parameters:
        originalSet - The Set of Strings to check
        Returns:
        A copy of the original Set less any blank String entries.
      • asList

        public static <T> List<T> asList​(T... values)
        Collects the passed in objects into a List.
        Type Parameters:
        T - The type of the passed in objects.
        Parameters:
        values - An unbounded amount of objects that needs to be collected to a List.
        Returns:
        Either an empty list (if there was no value passed in), or a List that holds all the values passed in.
      • asCaseInsensitiveHashSet

        public static Set asCaseInsensitiveHashSet​(Object... values)
        Collects the passed in objects into a case insensitive hash set. Unfortunately case insensitive hash sets are not generic.
        Parameters:
        values - An unbounded amount of objects that needs to be converted to a case insensitive hash set.
        Returns:
        Either an empty set (if there was no value passed in), or a Set that holds all the values passed in.
      • asSet

        public static <T> Set<T> asSet​(T... values)
        Collects the passed in objects into an unordered Set.
        Type Parameters:
        T - The type of the passed in objects.
        Parameters:
        values - An unbounded amount of objects that needs to be converted to a Set.
        Returns:
        Either an empty set (if there was no value passed in), or a Set that holds all the values passed in.
      • asOrderedSet

        public static <T> LinkedHashSet<T> asOrderedSet​(T... values)
        Collects the passed in objects to a LinkedHashSet.
        Type Parameters:
        T - The type of the passed in objects.
        Parameters:
        values - An unbounded amount of objects that needs to be converted to a Set.
        Returns:
        Either an empty set (if there was no value passed in), or a Set that holds all the values in the exact same order as passed in.
      • invertMap

        public static <K,​V> Map<V,​K> invertMap​(Map<K,​V> map)
        Creates a new Map which contains the entries of the provided map but with keys and values exchanged.
        Type Parameters:
        K - the type of keys in the provided map; the type of the values in the returned map.
        V - the type of values in the provided map; the type of the keys in the returned map.
        Parameters:
        map - the non-null original map.
        Returns:
        a new map with inverted key-value mapping.
      • transformMap

        public static <K,​I,​M,​E extends ExceptionMap<K,​M> transformMap​(Map<K,​I> map,
                                                                                                org.forgerock.util.Function<I,​M,​E> mapper)
                                                                                         throws E extends Exception
        Maps the values of a non-null map from one type to another type using a non-null mapper function.
        Type Parameters:
        K - the type of key
        I - the type of the initial value
        M - the type of the mapped value
        E - the type of exception that the function may throw
        Parameters:
        map - the non-null original map
        mapper - the non-null mapping function
        Returns:
        a new map with the mapped values
        Throws:
        E - should an exception occur during the mapping process
        E extends Exception
      • transformList

        public static <I,​M,​E extends ExceptionList<M> transformList​(Collection<I> values,
                                                                                  org.forgerock.util.Function<I,​M,​E> mapper)
                                                                           throws E extends Exception
        Maps the non-null values from one type to another type using a non-null mapper function.
        Type Parameters:
        I - the type of the initial value
        M - the type of the mapped value
        E - the type of exception that the function may throw
        Parameters:
        values - the non-null original values
        mapper - the non-null mapping function
        Returns:
        a new list with the mapped values
        Throws:
        E - should an exception occur during the mapping process
        E extends Exception
      • transformSet

        public static <I,​M,​E extends ExceptionSet<M> transformSet​(Collection<I> values,
                                                                                org.forgerock.util.Function<I,​M,​E> mapper)
                                                                         throws E extends Exception
        Maps the non-null values from one type to another type using a non-null mapper function.
        Type Parameters:
        I - the type of the initial value
        M - the type of the mapped value
        E - the type of exception that the function may throw
        Parameters:
        values - the non-null original values
        mapper - the non-null mapping function
        Returns:
        a new list with the mapped values
        Throws:
        E - should an exception occur during the mapping process
        E extends Exception
      • getFirstItem

        public static <T> T getFirstItem​(Collection<T> collection)
        Retrieves the first item from a collection.
        Type Parameters:
        T - The type of the collection.
        Parameters:
        collection - The collection.
        Returns:
        The first instance of the collection else null if not present.
      • getFirstItem

        public static <T> T getFirstItem​(Collection<T> collection,
                                         T defaultValue)
        Retrieves the first item from a collections.
        Type Parameters:
        T - the type of the collection
        Parameters:
        collection - the collection
        defaultValue - the default value should not initial value exist
        Returns:
        the first instance of the collection else null if not present
      • genericCompare

        public static <T> boolean genericCompare​(T valA,
                                                 T valB)
        Compares any two arbitrary objects - including collections, for equality. It also handles either of the objects being null without issue.
        Type Parameters:
        T - The type of the objects being compared.
        Parameters:
        valA - The first object to be compared.
        valB - The second object to tbe compared.
        Returns:
        true if the parameter values are the same, false if different.
      • compareCaseInsensitiveMapOfSetOfStrings

        public static boolean compareCaseInsensitiveMapOfSetOfStrings​(Map<String,​Set<String>> valA,
                                                                      Map<String,​Set<String>> valB)
        Compares two map collections containing sets of strings in a case insensitive manner.
        Parameters:
        valA - - the first map of set of strings to be compared.
        valB - - the second map of set of strings to be compared.
        Returns:
        true if the parameter values are the same, false if different.
      • createHashForCaseInsensitiveMapOfSetOfStrings

        public static int createHashForCaseInsensitiveMapOfSetOfStrings​(Map<String,​Set<String>> map)
        Creates a hash code for a map of set of strings in a case insensitive manner. This is needed so that if a case insensitive equality is applied to the map, the hash is generated in a similar and consisent manner.
        Parameters:
        map - - the map of set of strings for which the hash code is to be generated.
        Returns:
        the generated hash code.
      • isNotEmpty

        public static boolean isNotEmpty​(Collection<?> collection)
        Given a collection determines whether it contains some values.
        Parameters:
        collection - collection in question
        Returns:
        whether the collection contains any values
      • isEmpty

        public static boolean isEmpty​(Collection<?> collection)
        Determines whether the collections is null or empty.
        Parameters:
        collection - the collection
        Returns:
        whether the collection is null or empty
      • toAvPairMap

        public static Map<String,​Set<String>> toAvPairMap​(Set<String> names,
                                                                String value)
        Creates a Hash Map, of names mapped to the provided value. If there is no value, an empty map is returned.
        Parameters:
        names - The set of names to be associated with the provided value.
        value - The value to be mapped to each of the provided names.
        Returns:
        The map of name / value pairs, otherwise an Empty Map if no value is provided or an empty hashmap if no names are specified.
      • isEmpty

        public static boolean isEmpty​(Map<?,​?> map)
        Given a map determines whether it contains some values.
        Parameters:
        map - map in question
        Returns:
        whether the map contains any values
      • isNotEmpty

        public static boolean isNotEmpty​(Map<?,​?> map)
        Given a map determines whether it contains some values.
        Parameters:
        map - map in question
        Returns:
        whether the map contains any values
      • anyHaveEntries

        public static boolean anyHaveEntries​(Object... containers)
        See if any of the containers have entries and if so return true. Any argument which is not a Collection or a Map is ignored.
        Parameters:
        containers - a varied list of maps and collections
        Returns:
        true if any one of the maps and/or collections have entries
      • sortMapByValue

        public static <K,​V extends Comparable<? super V>> Map<K,​V> sortMapByValue​(Map<K,​V> map)
        Sort the given Map by comparing it's values.
        Type Parameters:
        K - The key type.
        V - The value type.
        Parameters:
        map - The map to sort.
        Returns:
        The given map sorted and presented in a LinkedHashMap to guarantee iteration order.
      • newList

        public static <T> List<T> newList​(Collection<T> collection)
        Returns an ArrayList of the items in the provided collection, or null if the set is null.
        Type Parameters:
        T - The type of value.
        Parameters:
        collection - The collection.
        Returns:
        The list, or null if the collection was null.