Package com.twelvemonkeys.util
Class CollectionUtil
java.lang.Object
com.twelvemonkeys.util.CollectionUtil
A utility class with some useful collection-related functions.
- Version:
- $Id: com/twelvemonkeys/util/CollectionUtil.java#3 $
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
private static class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <E> void
addAll
(Collection<E> pCollection, Iterator<? extends E> pIterator) Adds all elements of the iterator to the collection.(package private) static <T extends Collection<? super E>,
E>
Tgenerify
(Collection<?> pCollection, Class<E> pElementType) (package private) static <T extends Iterator<? super E>,
E>
T(package private) static <T extends Map<? super K,
? super V>, K, V>
T(package private) static <T extends Collection<? super E>,
E>
Tgenerify2
(Collection<?> pCollection) static <K,
V> Map <V, K> Creates an inverted mapping of the key/value pairs in the given map.static <K,
V> Map <V, K> invert
(Map<K, V> pSource, Map<V, K> pResult, DuplicateHandler<K> pHandler) Creates an inverted mapping of the key/value pairs in the given map.static <E> ListIterator
<E> iterator
(E[] pArray) Creates a thinIterator
wrapper around an array.static <E> ListIterator
<E> iterator
(E[] pArray, int pStart, int pLength) Creates a thinIterator
wrapper around an array.static <T> Iterator
<T> iterator
(Enumeration<T> pEnum) static void
Testing only.static Object
mergeArrays
(Object pArray1, int pOffset1, int pLength1, Object pArray2, int pOffset2, int pLength2) Merges two arrays into a new array.static Object
mergeArrays
(Object pArray1, Object pArray2) Merges two arrays into a new array.static <T> Comparator
<T> reverseOrder
(Comparator<T> pOriginal) static Object
Creates an array containing a subset of the original array.static Object
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.
-
Constructor Details
-
CollectionUtil
private CollectionUtil()
-
-
Method Details
-
main
Testing only.- Parameters:
pArgs
- command line arguents
-
mergeArrays
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
public static Object mergeArrays(Object pArray1, int pOffset1, int pLength1, 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:
-
subArray
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:
IllegalArgumentException
- ifpArray
isnull
or ifpArray
is not an array.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:
IllegalArgumentException
- ifpArray
isnull
ArrayIndexOutOfBoundsException
- ifpStart
< 0
-
subArray
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:
IllegalArgumentException
- ifpArray
isnull
or ifpArray
is not an array.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:
IllegalArgumentException
- ifpArray
isnull
ArrayIndexOutOfBoundsException
- ifpStart
< 0
-
iterator
-
addAll
Adds all elements of the iterator to the collection.- Parameters:
pCollection
- the collectionpIterator
- the elements to add- Throws:
UnsupportedOperationException
- ifadd
is not supported by the given collection.ClassCastException
- class of the specified element prevents it from being added to this collection.NullPointerException
- if the specified element isnull
and this collection does not supportnull
elements.IllegalArgumentException
- some aspect of this element prevents it from being added to this collection.
-
iterator
Creates a thinIterator
wrapper around an array.- Parameters:
pArray
- the array to iterate- Returns:
- a new
ListIterator
- Throws:
IllegalArgumentException
- ifpArray
isnull
,pStart < 0
, orpLength > pArray.length - pStart
-
iterator
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:
IllegalArgumentException
- ifpArray
isnull
,pStart < 0
, orpLength > pArray.length - pStart
-
invert
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:
IllegalArgumentException
- ifpSource == null
, or if a new map can't be instantiated, or if source map contains duplicates.- See Also:
-
invert
public static <K,V> Map<V,K> invert(Map<K, V> pSource, 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:
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
-
generify
-
generify
static <T extends Collection<? super E>,E> T generify(Collection<?> pCollection, Class<E> pElementType) -
generify
-
generify2
-