diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/util/CollectionUtil.java b/common/common-lang/src/main/java/com/twelvemonkeys/util/CollectionUtil.java
index 0b15be9f..63e205ba 100755
--- a/common/common-lang/src/main/java/com/twelvemonkeys/util/CollectionUtil.java
+++ b/common/common-lang/src/main/java/com/twelvemonkeys/util/CollectionUtil.java
@@ -28,17 +28,21 @@
package com.twelvemonkeys.util;
+import com.twelvemonkeys.lang.Validate;
+
import java.lang.reflect.Array;
import java.util.*;
+import static com.twelvemonkeys.lang.Validate.isTrue;
+import static com.twelvemonkeys.lang.Validate.notNull;
+
/**
* A utility class with some useful collection-related functions.
*
* @author Harald Kuhr
* @author Eirik Torske
* @author last modified by $Author: haku $
- * @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/CollectionUtil.java#3 $
- * @todo move makeList and makeSet to StringUtil?
+ * @version $Id: com/twelvemonkeys/util/CollectionUtil.java#3 $
* @see Collections
* @see Arrays
*/
@@ -51,8 +55,6 @@ public final class CollectionUtil {
*/
@SuppressWarnings({"UnusedDeclaration", "UnusedAssignment", "unchecked"})
public static void main(String[] pArgs) {
- test();
-
int howMany = 1000;
if (pArgs.length > 0) {
@@ -257,7 +259,7 @@ public final class CollectionUtil {
* If the sub array is same length as the original
* ({@code pStart == 0}), the original array will be returned.
*
- * @param pArray the origianl array
+ * @param pArray the original array
* @param pStart the start index of the original array
* @return a subset of the original array, or the original array itself,
* if {@code pStart} is 0.
@@ -270,16 +272,33 @@ public final class CollectionUtil {
return subArray(pArray, pStart, -1);
}
+ /**
+ * Creates an array containing a subset of the original array.
+ * If the sub array is same length as the original
+ * ({@code pStart == 0}), the original array will be returned.
+ *
+ * @param pArray the original array
+ * @param pStart the start index of the original array
+ * @return a subset of the original array, or the original array itself,
+ * if {@code pStart} is 0.
+ *
+ * @throws IllegalArgumentException if {@code pArray} is {@code null}
+ * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0
+ */
+ public static T[] subArray(T[] pArray, int pStart) {
+ return subArray(pArray, pStart, -1);
+ }
+
/**
* Creates an array containing a subset of the original array.
* If the {@code pLength} parameter is negative, it will be ignored.
* If there are not {@code pLength} elements in the original array
- * after {@code pStart}, the {@code pLength} paramter will be
+ * after {@code pStart}, the {@code pLength} parameter will be
* ignored.
* If the sub array is same length as the original, the original array will
* be returned.
*
- * @param pArray the origianl array
+ * @param pArray the original array
* @param pStart the start index of the original array
* @param pLength the length of the new array
* @return a subset of the original array, or the original array itself,
@@ -292,9 +311,7 @@ public final class CollectionUtil {
*/
@SuppressWarnings({"SuspiciousSystemArraycopy"})
public static Object subArray(Object pArray, int pStart, int pLength) {
- if (pArray == null) {
- throw new IllegalArgumentException("array == null");
- }
+ Validate.notNull(pArray, "array");
// Get component type
Class type;
@@ -321,7 +338,7 @@ public final class CollectionUtil {
Object result;
if (newLength < originalLength) {
- // Create subarray & copy into
+ // Create sub array & copy into
result = Array.newInstance(type, newLength);
System.arraycopy(pArray, pStart, result, 0, newLength);
}
@@ -335,7 +352,33 @@ public final class CollectionUtil {
return result;
}
+ /**
+ * Creates an array containing a subset of the original array.
+ * If the {@code pLength} parameter is negative, it will be ignored.
+ * If there are not {@code pLength} elements in the original array
+ * after {@code pStart}, the {@code pLength} parameter will be
+ * ignored.
+ * If the sub array is same length as the original, the original array will
+ * be returned.
+ *
+ * @param pArray the original array
+ * @param pStart the start index of the original array
+ * @param pLength the length of the new array
+ * @return a subset of the original array, or the original array itself,
+ * if {@code pStart} is 0 and {@code pLength} is either
+ * negative, or greater or equal to {@code pArray.length}.
+ *
+ * @throws IllegalArgumentException if {@code pArray} is {@code null}
+ * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0
+ */
+ @SuppressWarnings("unchecked")
+ public static T[] subArray(T[] pArray, int pStart, int pLength) {
+ return (T[]) subArray((Object) pArray, pStart, pLength);
+ }
+
public static Iterator iterator(final Enumeration pEnum) {
+ notNull(pEnum, "enumeration");
+
return new Iterator() {
public boolean hasNext() {
return pEnum.hasMoreElements();
@@ -361,8 +404,8 @@ public final class CollectionUtil {
* the given collection.
* @throws ClassCastException class of the specified element prevents it
* from being added to this collection.
- * @throws NullPointerException if the specified element is null and this
- * collection does not support null elements.
+ * @throws NullPointerException if the specified element is {@code null} and this
+ * collection does not support {@code null} elements.
* @throws IllegalArgumentException some aspect of this element prevents
* it from being added to this collection.
*/
@@ -372,7 +415,7 @@ public final class CollectionUtil {
}
}
- // Is there a usecase where Arrays.asList(pArray).iterator() can't ne used?
+ // Is there a use case where Arrays.asList(pArray).iterator() can't ne used?
/**
* Creates a thin {@link Iterator} wrapper around an array.
*
@@ -383,7 +426,7 @@ public final class CollectionUtil {
* {@code pLength > pArray.length - pStart}
*/
public static ListIterator iterator(final E[] pArray) {
- return iterator(pArray, 0, pArray.length);
+ return iterator(pArray, 0, notNull(pArray).length);
}
/**
@@ -408,7 +451,7 @@ public final class CollectionUtil {
* @return a new {@code Map} of same type as {@code pSource}
* @throws IllegalArgumentException if {@code pSource == null},
* or if a new map can't be instantiated,
- * or if source map contains duplaicates.
+ * or if source map contains duplicates.
*
* @see #invert(java.util.Map, java.util.Map, DuplicateHandler)
*/
@@ -424,7 +467,7 @@ public final class CollectionUtil {
* @param pResult the map used to contain the result, may be {@code null},
* in that case a new {@code Map} of same type as {@code pSource} is created.
* The result map should be empty, otherwise duplicate values will need to be resolved.
- * @param pHandler duplicate handler, may be {@code null} if source map don't contain dupliate values
+ * @param pHandler duplicate handler, may be {@code null} if source map don't contain duplicate values
* @return {@code pResult}, or a new {@code Map} if {@code pResult == null}
* @throws IllegalArgumentException if {@code pSource == null},
* or if result map is {@code null} and a new map can't be instantiated,
@@ -476,20 +519,20 @@ public final class CollectionUtil {
return result;
}
- public static Comparator reverseOrder(Comparator pOriginal) {
+ public static Comparator reverseOrder(final Comparator pOriginal) {
return new ReverseComparator(pOriginal);
}
private static class ReverseComparator implements Comparator {
- private Comparator mComparator;
+ private final Comparator comparator;
- public ReverseComparator(Comparator pComparator) {
- mComparator = pComparator;
+ public ReverseComparator(final Comparator pComparator) {
+ comparator = notNull(pComparator);
}
public int compare(T pLeft, T pRight) {
- int result = mComparator.compare(pLeft, pRight);
+ int result = comparator.compare(pLeft, pRight);
// We can't simply return -result, as -Integer.MIN_VALUE == Integer.MIN_VALUE.
return -(result | (result >>> 1));
@@ -516,73 +559,21 @@ public final class CollectionUtil {
return (T) pCollection;
}
- @SuppressWarnings({"UnusedDeclaration"})
- static void test() {
- List list = Collections.singletonList("foo");
- @SuppressWarnings({"unchecked"})
- Set set = new HashSet(list);
-
- List strs0 = CollectionUtil.generify(list, String.class);
- List