Class CollectionUtil


  • public final class CollectionUtil
    extends java.lang.Object
    Generic collection and array-related utility functions for java.util types. See ClassUtil for comparable facilities for short name lookup.
    Version:
    $Revision$
    Author:
    Brian Remedios
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> int addWithoutDuplicates​(java.util.Collection<T> source, java.util.Collection<T> target)
      Add elements from the source to the target as long as they don't already exist there.
      static <T> T[] addWithoutDuplicates​(T[] values, T newValue)
      Deprecated.
      static <T> T[] addWithoutDuplicates​(T[] values, T[] newValues)
      Deprecated.
      static boolean areEqual​(java.lang.Object value, java.lang.Object otherValue)
      Deprecated.
      Objects.deepEquals(Object, Object)
      static <T> boolean areSemanticEquals​(T[] a, T[] b)
      Deprecated.
      Arrays.deepEquals(Object[], Object[])
      static boolean arraysAreEqual​(java.lang.Object value, java.lang.Object otherValue)
      Deprecated.
      Objects.deepEquals(Object, Object)
      static <T> java.util.Set<T> asSet​(T[] items)
      Returns the items as a populated set.
      static java.lang.Class<?> getCollectionTypeFor​(java.lang.String shortName)
      Returns the collection type if we recognize it by its short name.
      static <K,​V>
      java.util.Map<V,​K>
      invertedMapFrom​(java.util.Map<K,​V> source)
      Returns a map based on the source but with the key & values swapped.
      static boolean isCollectionType​(java.lang.Class<?> clazzType, boolean includeInterfaces)
      Return whether we can identify the typeName as a java.util collection class or interface as specified.
      static boolean isCollectionType​(java.lang.String typeName, boolean includeInterfaces)
      Return whether we can identify the typeName as a java.util collection class or interface as specified.
      static boolean isEmpty​(java.lang.Object[] items)
      Returns whether the items array is null or has zero length.
      static boolean isNotEmpty​(java.lang.Object[] items)
      Returns whether the items array is non-null and has at least one entry.
      static <K,​V>
      java.util.Map<K,​V>
      mapFrom​(K[] keys, V[] values)
      Creates and returns a map populated with the keyValuesSets where the value held by the tuples are they key and value in that order.
      static <T> java.util.List<T> toList​(java.util.Iterator<T> it)
      Consumes all the elements of the iterator and returns a list containing them.
      static boolean valuesAreTransitivelyEqual​(java.lang.Object[] thisArray, java.lang.Object[] thatArray)
      Deprecated.
      Arrays.deepEquals(Object[], Object[])
      • Methods inherited from class java.lang.Object

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

      • COLLECTION_INTERFACES_BY_NAMES

        public static final TypeMap COLLECTION_INTERFACES_BY_NAMES
      • COLLECTION_CLASSES_BY_NAMES

        public static final TypeMap COLLECTION_CLASSES_BY_NAMES
    • Method Detail

      • addWithoutDuplicates

        public static <T> int addWithoutDuplicates​(java.util.Collection<T> source,
                                                   java.util.Collection<T> target)
        Add elements from the source to the target as long as they don't already exist there. Return the number of items actually added.
        Parameters:
        source -
        target -
        Returns:
        int
      • getCollectionTypeFor

        public static java.lang.Class<?> getCollectionTypeFor​(java.lang.String shortName)
        Returns the collection type if we recognize it by its short name.
        Parameters:
        shortName - String
        Returns:
        Class
      • isCollectionType

        public static boolean isCollectionType​(java.lang.String typeName,
                                               boolean includeInterfaces)
        Return whether we can identify the typeName as a java.util collection class or interface as specified.
        Parameters:
        typeName - String
        includeInterfaces - boolean
        Returns:
        boolean
      • isCollectionType

        public static boolean isCollectionType​(java.lang.Class<?> clazzType,
                                               boolean includeInterfaces)
        Return whether we can identify the typeName as a java.util collection class or interface as specified.
        Parameters:
        clazzType - Class
        includeInterfaces - boolean
        Returns:
        boolean
      • asSet

        public static <T> java.util.Set<T> asSet​(T[] items)
        Returns the items as a populated set.
        Parameters:
        items - Object[]
        Returns:
        Set
      • mapFrom

        public static <K,​V> java.util.Map<K,​V> mapFrom​(K[] keys,
                                                                   V[] values)
        Creates and returns a map populated with the keyValuesSets where the value held by the tuples are they key and value in that order.
        Parameters:
        keys - K[]
        values - V[]
        Returns:
        Map
      • invertedMapFrom

        public static <K,​V> java.util.Map<V,​K> invertedMapFrom​(java.util.Map<K,​V> source)
        Returns a map based on the source but with the key & values swapped.
        Parameters:
        source - Map
        Returns:
        Map
      • toList

        public static <T> java.util.List<T> toList​(java.util.Iterator<T> it)
        Consumes all the elements of the iterator and returns a list containing them. The iterator is then unusable
        Parameters:
        it - An iterator
        Returns:
        a list containing the elements remaining on the iterator
      • arraysAreEqual

        @Deprecated
        public static boolean arraysAreEqual​(java.lang.Object value,
                                             java.lang.Object otherValue)
        Deprecated.
        Objects.deepEquals(Object, Object)
        Returns true if the objects are array instances and each of their elements compares via equals as well.
        Parameters:
        value - Object
        otherValue - Object
        Returns:
        boolean
      • valuesAreTransitivelyEqual

        @Deprecated
        public static boolean valuesAreTransitivelyEqual​(java.lang.Object[] thisArray,
                                                         java.lang.Object[] thatArray)
        Deprecated.
        Arrays.deepEquals(Object[], Object[])
        Returns whether the arrays are equal by examining each of their elements, even if they are arrays themselves.
        Parameters:
        thisArray - Object[]
        thatArray - Object[]
        Returns:
        boolean
      • areEqual

        @Deprecated
        public static boolean areEqual​(java.lang.Object value,
                                       java.lang.Object otherValue)
        Deprecated.
        Objects.deepEquals(Object, Object)
        A comprehensive isEqual method that handles nulls and arrays safely.
        Parameters:
        value - Object
        otherValue - Object
        Returns:
        boolean
      • isEmpty

        public static boolean isEmpty​(java.lang.Object[] items)
        Returns whether the items array is null or has zero length.
        Parameters:
        items -
        Returns:
        boolean
      • isNotEmpty

        public static boolean isNotEmpty​(java.lang.Object[] items)
        Returns whether the items array is non-null and has at least one entry.
        Parameters:
        items -
        Returns:
        boolean
      • areSemanticEquals

        @Deprecated
        public static <T> boolean areSemanticEquals​(T[] a,
                                                    T[] b)
        Deprecated.
        Arrays.deepEquals(Object[], Object[])
        Returns true if both arrays are if both are null or have zero-length, otherwise return the false if their respective elements are not equal by position.
        Type Parameters:
        T -
        Parameters:
        a -
        b -
        Returns:
        boolean
      • addWithoutDuplicates

        @Deprecated
        public static <T> T[] addWithoutDuplicates​(T[] values,
                                                   T newValue)
        Deprecated.
        If the newValue is already held within the values array then the values array is returned, otherwise a new array is created appending the newValue to the end.
        Type Parameters:
        T -
        Parameters:
        values -
        newValue -
        Returns:
        an array containing the union of values and newValue
      • addWithoutDuplicates

        @Deprecated
        public static <T> T[] addWithoutDuplicates​(T[] values,
                                                   T[] newValues)
        Deprecated.
        Returns an array of values as a union set of the two input arrays.
        Type Parameters:
        T -
        Parameters:
        values -
        newValues -
        Returns:
        the union of the two arrays