Class CollectionUtil


  • public final class CollectionUtil
    extends java.lang.Object
    A utility class with some useful collection-related functions.
    Version:
    $Id: com/twelvemonkeys/util/CollectionUtil.java#3 $
    See Also:
    Collections, Arrays
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CollectionUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> void addAll​(java.util.Collection<E> pCollection, java.util.Iterator<? extends E> pIterator)
      Adds all elements of the iterator to the collection.
      (package private) static <T extends java.util.Collection<? super E>,​E>
      T
      generify​(java.util.Collection<?> pCollection, java.lang.Class<E> pElementType)  
      (package private) static <T extends java.util.Iterator<? super E>,​E>
      T
      generify​(java.util.Iterator<?> pIterator, java.lang.Class<E> pElementType)  
      (package private) static <T extends java.util.Map<? super K,​? super V>,​K,​V>
      T
      generify​(java.util.Map<?,​?> pMap, java.lang.Class<K> pKeyType, java.lang.Class<V> pValueType)  
      (package private) static <T extends java.util.Collection<? super E>,​E>
      T
      generify2​(java.util.Collection<?> pCollection)  
      static <K,​V>
      java.util.Map<V,​K>
      invert​(java.util.Map<K,​V> pSource)
      Creates an inverted mapping of the key/value pairs in the given map.
      static <K,​V>
      java.util.Map<V,​K>
      invert​(java.util.Map<K,​V> pSource, java.util.Map<V,​K> pResult, DuplicateHandler<K> pHandler)
      Creates an inverted mapping of the key/value pairs in the given map.
      static <E> java.util.ListIterator<E> iterator​(E[] pArray)
      Creates a thin Iterator wrapper around an array.
      static <E> java.util.ListIterator<E> iterator​(E[] pArray, int pStart, int pLength)
      Creates a thin Iterator wrapper around an array.
      static <T> java.util.Iterator<T> iterator​(java.util.Enumeration<T> pEnum)  
      static void main​(java.lang.String[] pArgs)
      Testing only.
      static java.lang.Object mergeArrays​(java.lang.Object pArray1, int pOffset1, int pLength1, java.lang.Object pArray2, int pOffset2, int pLength2)
      Merges two arrays into a new array.
      static java.lang.Object mergeArrays​(java.lang.Object pArray1, java.lang.Object pArray2)
      Merges two arrays into a new array.
      static <T> java.util.Comparator<T> reverseOrder​(java.util.Comparator<T> pOriginal)  
      static java.lang.Object subArray​(java.lang.Object pArray, int pStart)
      Creates an array containing a subset of the original array.
      static java.lang.Object subArray​(java.lang.Object pArray, int pStart, int pLength)
      Creates an array containing a subset of the original array.
      static <T> T[] subArray​(T[] pArray, int pStart)
      Creates an array containing a subset of the original array.
      static <T> T[] subArray​(T[] pArray, int pStart, int pLength)
      Creates an array containing a subset of the original array.
      • Methods inherited from class java.lang.Object

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

      • CollectionUtil

        private CollectionUtil()
    • Method Detail

      • main

        public static void main​(java.lang.String[] pArgs)
        Testing only.
        Parameters:
        pArgs - command line arguents
      • mergeArrays

        public static java.lang.Object mergeArrays​(java.lang.Object pArray1,
                                                   java.lang.Object pArray2)
        Merges two arrays into a new array. Elements from array1 and array2 will be copied into a new array, that has array1.length + array2.length elements.
        Parameters:
        pArray1 - First array
        pArray2 - Second array, must be compatible with (assignable from) the first array
        Returns:
        A new array, containing the values of array1 and array2. The array (wrapped as an object), will have the length of array1 + array2, and can be safely cast to the type of the array1 parameter.
        See Also:
        mergeArrays(Object,int,int,Object,int,int), System.arraycopy(Object,int,Object,int,int)
      • mergeArrays

        public static java.lang.Object mergeArrays​(java.lang.Object pArray1,
                                                   int pOffset1,
                                                   int pLength1,
                                                   java.lang.Object pArray2,
                                                   int pOffset2,
                                                   int pLength2)
        Merges two arrays into a new array. Elements from pArray1 and pArray2 will be copied into a new array, that has pLength1 + pLength2 elements.
        Parameters:
        pArray1 - First array
        pOffset1 - the offset into the first array
        pLength1 - the number of elements to copy from the first array
        pArray2 - Second array, must be compatible with (assignable from) the first array
        pOffset2 - the offset into the second array
        pLength2 - the number of elements to copy from the second array
        Returns:
        A new array, containing the values of pArray1 and pArray2. The array (wrapped as an object), will have the length of pArray1 + pArray2, and can be safely cast to the type of the pArray1 parameter.
        See Also:
        System.arraycopy(Object,int,Object,int,int)
      • subArray

        public static java.lang.Object subArray​(java.lang.Object pArray,
                                                int pStart)
        Creates an array containing a subset of the original array. If the sub array is same length as the original (pStart == 0), the original array will be returned.
        Parameters:
        pArray - the original array
        pStart - the start index of the original array
        Returns:
        a subset of the original array, or the original array itself, if pStart is 0.
        Throws:
        java.lang.IllegalArgumentException - if pArray is null or if pArray is not an array.
        java.lang.ArrayIndexOutOfBoundsException - if pStart < 0
      • subArray

        public static <T> T[] subArray​(T[] pArray,
                                       int pStart)
        Creates an array containing a subset of the original array. If the sub array is same length as the original (pStart == 0), the original array will be returned.
        Type Parameters:
        T - the type of array
        Parameters:
        pArray - the original array
        pStart - the start index of the original array
        Returns:
        a subset of the original array, or the original array itself, if pStart is 0.
        Throws:
        java.lang.IllegalArgumentException - if pArray is null
        java.lang.ArrayIndexOutOfBoundsException - if pStart < 0
      • subArray

        public static java.lang.Object subArray​(java.lang.Object pArray,
                                                int pStart,
                                                int pLength)
        Creates an array containing a subset of the original array. If the pLength parameter is negative, it will be ignored. If there are not pLength elements in the original array after pStart, the pLength parameter will be ignored. If the sub array is same length as the original, the original array will be returned.
        Parameters:
        pArray - the original array
        pStart - the start index of the original array
        pLength - the length of the new array
        Returns:
        a subset of the original array, or the original array itself, if pStart is 0 and pLength is either negative, or greater or equal to pArray.length.
        Throws:
        java.lang.IllegalArgumentException - if pArray is null or if pArray is not an array.
        java.lang.ArrayIndexOutOfBoundsException - if pStart < 0
      • subArray

        public static <T> T[] subArray​(T[] pArray,
                                       int pStart,
                                       int pLength)
        Creates an array containing a subset of the original array. If the pLength parameter is negative, it will be ignored. If there are not pLength elements in the original array after pStart, the pLength parameter will be ignored. If the sub array is same length as the original, the original array will be returned.
        Type Parameters:
        T - the type of array
        Parameters:
        pArray - the original array
        pStart - the start index of the original array
        pLength - the length of the new array
        Returns:
        a subset of the original array, or the original array itself, if pStart is 0 and pLength is either negative, or greater or equal to pArray.length.
        Throws:
        java.lang.IllegalArgumentException - if pArray is null
        java.lang.ArrayIndexOutOfBoundsException - if pStart < 0
      • iterator

        public static <T> java.util.Iterator<T> iterator​(java.util.Enumeration<T> pEnum)
      • addAll

        public static <E> void addAll​(java.util.Collection<E> pCollection,
                                      java.util.Iterator<? extends E> pIterator)
        Adds all elements of the iterator to the collection.
        Parameters:
        pCollection - the collection
        pIterator - the elements to add
        Throws:
        java.lang.UnsupportedOperationException - if add is not supported by the given collection.
        java.lang.ClassCastException - class of the specified element prevents it from being added to this collection.
        java.lang.NullPointerException - if the specified element is null and this collection does not support null elements.
        java.lang.IllegalArgumentException - some aspect of this element prevents it from being added to this collection.
      • iterator

        public static <E> java.util.ListIterator<E> iterator​(E[] pArray)
        Creates a thin Iterator wrapper around an array.
        Parameters:
        pArray - the array to iterate
        Returns:
        a new ListIterator
        Throws:
        java.lang.IllegalArgumentException - if pArray is null, pStart &lt; 0, or pLength &gt; pArray.length - pStart
      • iterator

        public static <E> java.util.ListIterator<E> iterator​(E[] pArray,
                                                             int pStart,
                                                             int pLength)
        Creates a thin Iterator wrapper around an array.
        Parameters:
        pArray - the array to iterate
        pStart - the offset into the array
        pLength - the number of elements to include in the iterator
        Returns:
        a new ListIterator
        Throws:
        java.lang.IllegalArgumentException - if pArray is null, pStart &lt; 0, or pLength &gt; pArray.length - pStart
      • invert

        public static <K,​V> java.util.Map<V,​K> invert​(java.util.Map<K,​V> pSource)
        Creates an inverted mapping of the key/value pairs in the given map.
        Parameters:
        pSource - the source map
        Returns:
        a new Map of same type as pSource
        Throws:
        java.lang.IllegalArgumentException - if pSource == null, or if a new map can't be instantiated, or if source map contains duplicates.
        See Also:
        invert(java.util.Map, java.util.Map, DuplicateHandler)
      • invert

        public static <K,​V> java.util.Map<V,​K> invert​(java.util.Map<K,​V> pSource,
                                                                  java.util.Map<V,​K> pResult,
                                                                  DuplicateHandler<K> pHandler)
        Creates an inverted mapping of the key/value pairs in the given map. Optionally, a duplicate handler may be specified, to resolve duplicate keys in the result map.
        Parameters:
        pSource - the source map
        pResult - the map used to contain the result, may be null, in that case a new Map of same type as pSource is created. The result map should be empty, otherwise duplicate values will need to be resolved.
        pHandler - duplicate handler, may be null if source map don't contain duplicate values
        Returns:
        pResult, or a new Map if pResult == null
        Throws:
        java.lang.IllegalArgumentException - if pSource == null, or if result map is null and a new map can't be instantiated, or if source map contains duplicate values and pHandler == null.
      • reverseOrder

        public static <T> java.util.Comparator<T> reverseOrder​(java.util.Comparator<T> pOriginal)
      • generify

        static <T extends java.util.Iterator<? super E>,​E> T generify​(java.util.Iterator<?> pIterator,
                                                                            java.lang.Class<E> pElementType)
      • generify

        static <T extends java.util.Collection<? super E>,​E> T generify​(java.util.Collection<?> pCollection,
                                                                              java.lang.Class<E> pElementType)
      • generify

        static <T extends java.util.Map<? super K,​? super V>,​K,​V> T generify​(java.util.Map<?,​?> pMap,
                                                                                               java.lang.Class<K> pKeyType,
                                                                                               java.lang.Class<V> pValueType)
      • generify2

        static <T extends java.util.Collection<? super E>,​E> T generify2​(java.util.Collection<?> pCollection)