Package com.twelvemonkeys.util
Class CollectionUtil
- java.lang.Object
-
- com.twelvemonkeys.util.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
CollectionUtil.ArrayIterator<E>
private static class
CollectionUtil.ReverseComparator<T>
-
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>
Tgenerify(java.util.Collection<?> pCollection, java.lang.Class<E> pElementType)
(package private) static <T extends java.util.Iterator<? super E>,E>
Tgenerify(java.util.Iterator<?> pIterator, java.lang.Class<E> pElementType)
(package private) static <T extends java.util.Map<? super K,? super V>,K,V>
Tgenerify(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>
Tgenerify2(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 thinIterator
wrapper around an array.static <E> java.util.ListIterator<E>
iterator(E[] pArray, int pStart, int pLength)
Creates a thinIterator
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.
-
-
-
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 arraypArray2
- 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 arraypOffset1
- the offset into the first arraypLength1
- the number of elements to copy from the first arraypArray2
- Second array, must be compatible with (assignable from) the first arraypOffset2
- the offset into the second arraypLength2
- 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 arraypStart
- 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
- ifpArray
isnull
or ifpArray
is not an array.java.lang.ArrayIndexOutOfBoundsException
- ifpStart
< 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 arraypStart
- 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
- ifpArray
isnull
java.lang.ArrayIndexOutOfBoundsException
- ifpStart
< 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 thepLength
parameter is negative, it will be ignored. If there are notpLength
elements in the original array afterpStart
, thepLength
parameter will be ignored. If the sub array is same length as the original, the original array will be returned.- Parameters:
pArray
- the original arraypStart
- the start index of the original arraypLength
- the length of the new array- Returns:
- a subset of the original array, or the original array itself,
if
pStart
is 0 andpLength
is either negative, or greater or equal topArray.length
. - Throws:
java.lang.IllegalArgumentException
- ifpArray
isnull
or ifpArray
is not an array.java.lang.ArrayIndexOutOfBoundsException
- ifpStart
< 0
-
subArray
public static <T> T[] subArray(T[] pArray, int pStart, int pLength)
Creates an array containing a subset of the original array. If thepLength
parameter is negative, it will be ignored. If there are notpLength
elements in the original array afterpStart
, thepLength
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 arraypStart
- the start index of the original arraypLength
- the length of the new array- Returns:
- a subset of the original array, or the original array itself,
if
pStart
is 0 andpLength
is either negative, or greater or equal topArray.length
. - Throws:
java.lang.IllegalArgumentException
- ifpArray
isnull
java.lang.ArrayIndexOutOfBoundsException
- ifpStart
< 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 collectionpIterator
- the elements to add- Throws:
java.lang.UnsupportedOperationException
- ifadd
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 isnull
and this collection does not supportnull
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 thinIterator
wrapper around an array.- Parameters:
pArray
- the array to iterate- Returns:
- a new
ListIterator
- Throws:
java.lang.IllegalArgumentException
- ifpArray
isnull
,pStart < 0
, orpLength > pArray.length - pStart
-
iterator
public static <E> java.util.ListIterator<E> iterator(E[] pArray, int pStart, int pLength)
Creates a thinIterator
wrapper around an array.- Parameters:
pArray
- the array to iteratepStart
- the offset into the arraypLength
- the number of elements to include in the iterator- Returns:
- a new
ListIterator
- Throws:
java.lang.IllegalArgumentException
- ifpArray
isnull
,pStart < 0
, orpLength > 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 aspSource
- Throws:
java.lang.IllegalArgumentException
- ifpSource == 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 mappResult
- the map used to contain the result, may benull
, in that case a newMap
of same type aspSource
is created. The result map should be empty, otherwise duplicate values will need to be resolved.pHandler
- duplicate handler, may benull
if source map don't contain duplicate values- Returns:
pResult
, or a newMap
ifpResult == null
- Throws:
java.lang.IllegalArgumentException
- ifpSource == null
, or if result map isnull
and a new map can't be instantiated, or if source map contains duplicate values andpHandler == 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)
-
-