Author: julien(a)jboss.com
Date: 2007-11-12 04:48:33 -0500 (Mon, 12 Nov 2007)
New Revision: 8874
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java
Log:
generified Tools collection related utility, fixed a few bugs and added test cases
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java 2007-11-12
08:42:53 UTC (rev 8873)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/Tools.java 2007-11-12
09:48:33 UTC (rev 8874)
@@ -138,26 +138,34 @@
}
};
- public static Enumeration toEnumeration(final Iterator iterator)
+ public static <E> Enumeration<E> toEnumeration(final Iterator<E>
iterator)
{
- return new Enumeration()
+ if (iterator == null)
{
+ throw new IllegalArgumentException();
+ }
+ return new Enumeration<E>()
+ {
public boolean hasMoreElements()
{
return iterator.hasNext();
}
- public Object nextElement()
+ public E nextElement()
{
return iterator.next();
}
};
}
- public static Enumeration toEnumeration(final Object[] objects)
+ public static <E> Enumeration<E> toEnumeration(final E[] objects)
{
- return new Enumeration()
+ if (objects == null)
{
+ throw new IllegalArgumentException();
+ }
+ return new Enumeration<E>()
+ {
int index = 0;
public boolean hasMoreElements()
@@ -165,7 +173,7 @@
return index < objects.length;
}
- public Object nextElement()
+ public E nextElement()
{
if (index < objects.length)
{
@@ -179,18 +187,18 @@
};
}
- public static Enumeration toEnumeration(final Object o)
+ public static <E> Enumeration<E> toEnumeration(final E o)
{
- return new Enumeration()
+ return new Enumeration<E>()
{
- boolean hasMore = false;
+ boolean hasMore = true;
public boolean hasMoreElements()
{
return hasMore;
}
- public Object nextElement()
+ public E nextElement()
{
if (hasMore)
{
@@ -205,9 +213,13 @@
};
}
- public static Set toSet(Enumeration e)
+ public static <E> Set<E> toSet(Enumeration<E> e)
{
- HashSet set = new HashSet();
+ if (e == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ HashSet<E> set = new HashSet<E>();
while (e.hasMoreElements())
{
set.add(e.nextElement());
@@ -215,59 +227,56 @@
return set;
}
- public static Object[] toArray(Iterator i)
+ public static <E> Set<E> toSet(E[] objects)
{
- return toList(i).toArray();
- }
-
- public static List toList(Enumeration e)
- {
- List list = new ArrayList();
- while (e.hasMoreElements())
+ if (objects == null)
{
- list.add(e.nextElement());
+ throw new IllegalArgumentException();
}
- return list;
- }
-
- public static Set toSet(Object[] objects)
- {
- HashSet set = new HashSet();
- for (int i = 0; i < objects.length; i++)
+ HashSet<E> set = new HashSet<E>();
+ for (E object : objects)
{
- set.add(objects[i]);
+ set.add(object);
}
return set;
}
/**
* Transforms an iterator into an unordered Set
- *
+ *
* @param iterator The iterator to transform
* @return A HashSet
*/
- public static Set toSet(Iterator iterator)
+ public static <E> Set<E> toSet(Iterator<E> iterator)
{
+ if (iterator == null)
+ {
+ throw new IllegalArgumentException();
+ }
return toSet(iterator, false);
}
/**
* Transforms an iterator into a Set
- *
+ *
* @param iterator The iterator to transform
- * @param ordered true if the set must respect the ordering of the iterator
+ * @param preserveOrder true if the set must respect the ordering of the iterator
* @return a LinkedHashSet if ordered is true, a HashSet otherwise
*/
- public static Set toSet(Iterator iterator, boolean ordered)
+ public static <E> Set<E> toSet(Iterator<E> iterator, boolean
preserveOrder)
{
- Set set = null;
- if (ordered)
+ if (iterator == null)
{
- set = new LinkedHashSet();
+ throw new IllegalArgumentException();
}
+ Set<E> set;
+ if (preserveOrder)
+ {
+ set = new LinkedHashSet<E>();
+ }
else
{
- set = new HashSet();
+ set = new HashSet<E>();
}
while (iterator.hasNext())
{
@@ -276,20 +285,27 @@
return set;
}
- public static List toList(Object[] objects)
+ public static <E> List<E> toList(Enumeration<E> e)
{
- List list = new ArrayList(objects.length);
- for (int i = 0; i < objects.length; i++)
+ if (e == null)
{
- Object object = objects[i];
- list.add(object);
+ throw new IllegalArgumentException();
}
+ List<E> list = new ArrayList<E>();
+ while (e.hasMoreElements())
+ {
+ list.add(e.nextElement());
+ }
return list;
}
- public static List toList(Iterator iterator)
+ public static <E> List<E> toList(Iterator<E> iterator)
{
- List list = new ArrayList();
+ if (iterator == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ List<E> list = new ArrayList<E>();
while (iterator.hasNext())
{
list.add(iterator.next());
@@ -297,15 +313,42 @@
return list;
}
+ public static <E> List<E> toList(E[] objects)
+ {
+ if (objects == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ List<E> list = new ArrayList<E>(objects.length);
+ for (E object : objects)
+ {
+ list.add(object);
+ }
+ return list;
+ }
+
/**
+ * Consider remove this method as it cannot be generified.
+ *
+ * @param i
+ * @return
+ */
+ @Deprecated
+ public static Object[] toArray(Iterator i)
+ {
+ // This method cannot be generified.
+ return toList(i).toArray();
+ }
+
+ /**
* Returns a singleton iterator.
*
* @param o the singleton object
* @return the iterator
*/
- public static Iterator iterator(final Object o)
+ public static <E> Iterator<E> iterator(final E o)
{
- return new Iterator()
+ return new Iterator<E>()
{
/** The status of the iterator. */
boolean done = false;
@@ -315,7 +358,7 @@
return !done;
}
- public Object next()
+ public E next()
{
if (done)
{
@@ -339,7 +382,7 @@
* @return the iterator
* @throws IllegalArgumentException if the object array is null or the specified range
is not valid
*/
- public static Iterator iterator(final Object[] objects) throws
IllegalArgumentException
+ public static <E> Iterator<E> iterator(final E[] objects) throws
IllegalArgumentException
{
if (objects == null)
{
@@ -349,15 +392,17 @@
}
/**
- * Returns an iterator over the array elements within the specified range.
+ * Returns an iterator over the array elements within the specified range. The range
is considered as valid
+ * if the from argument is greater or equals than zero, the to argument is lesser or
equals than array size
+ * and the from argument is lesser or equals to the to argument.
*
* @param objects the array containing the objects to iterate on
* @param from the inclusive start index
* @param to the exclusive stop index
* @return the iterator
- * @throws IllegalArgumentException if the object array is null or the specified range
is not valid
+ * @throws IllegalArgumentException if the object array is null or the specified range
is not valid or if the range is not valid
*/
- public static Iterator iterator(final Object[] objects, final int from, final int to)
throws IllegalArgumentException
+ public static <E> Iterator<E> iterator(final E[] objects, final int from,
final int to) throws IllegalArgumentException
{
if (objects == null)
{
@@ -367,7 +412,7 @@
{
throw new IllegalArgumentException("Invalid range [" + from +
"," + to + "] for array of length " + objects.length);
}
- return new Iterator()
+ return new Iterator<E>()
{
/** . */
int index = from;
@@ -377,9 +422,9 @@
return index < to;
}
- public Object next()
+ public E next()
{
- if (index >= objects.length)
+ if (index >= to)
{
throw new NoSuchElementException("Index is greater than the array
length");
}
@@ -597,13 +642,7 @@
public static String getShortNameOf(Class clazz)
{
- String name = clazz.getName();
- int index = name.lastIndexOf('.');
- if (index != -1)
- {
- name = name.substring(index + 1);
- }
- return name;
+ return clazz.getSimpleName();
}
public static String getPackageOf(Class clazz)
@@ -746,7 +785,7 @@
* @throws IllegalArgumentException if the array is null
* @throws ClassCastException if the appended object class prevents it from being
added to the array
*/
- public static Object[] appendTo(Object[] array, Object o) throws
IllegalArgumentException, ClassCastException
+ public static <E> E[] appendTo(E[] array, E o) throws IllegalArgumentException,
ClassCastException
{
if (array == null)
{
@@ -757,7 +796,7 @@
{
throw new ClassCastException("Object with class " +
o.getClass().getName() + " cannot be casted to class " +
componentType.getComponentType().getName());
}
- Object[] copy = (Object[])Array.newInstance(componentType, array.length + 1);
+ E[] copy = (E[])Array.newInstance(componentType, array.length + 1);
copy[array.length] = o;
return copy;
}
@@ -838,6 +877,12 @@
/**
* Determines if value is contained in array.
*
+ * todo: correct this method contract in order to make it more reusable, it looks like
for now like a method
+ * which has a contract convenient only for some kind of callers.
+ *
+ * 1/ null value should be accepted (or the method should be called
isContainedInButNotForNullValue ?)
+ * 2/ null array should not be accepted (or the method should be called
isContainedInExceptIfThePassedArrayIsNull ?)
+ *
* @param value
* @param array
* @return
@@ -850,11 +895,12 @@
return false;
}
+ //
if (array != null)
{
- for (int i = 0; i < array.length; i++)
+ for (Object anArray : array)
{
- if (value.equals(array[i]))
+ if (value.equals(anArray))
{
return true;
}
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java 2007-11-12
08:42:53 UTC (rev 8873)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/ToolsTestCase.java 2007-11-12
09:48:33 UTC (rev 8874)
@@ -26,6 +26,15 @@
import junit.framework.TestCase;
import org.jboss.portal.common.util.Tools;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.Vector;
+
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision: 6384 $
@@ -44,4 +53,337 @@
assertFalse(Tools.isContainedIn(null, null));
assertFalse(Tools.isContainedIn("bat", array));
}
+
+ public void testIteratorToEnumerationThrowsIAE()
+ {
+ try
+ {
+ Tools.toEnumeration((Iterator)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIteratorToEnumeration1()
+ {
+ assertEnumeration(Tools.toEnumeration(new ArrayList<String>().iterator()));
+ }
+
+ public void testIteratorToEnumeration2()
+ {
+ List<String> tmp = new ArrayList<String>();
+ tmp.add("a");
+ assertEnumeration(Tools.toEnumeration(tmp.iterator()), "a");
+ }
+
+ public void testIteratorToEnumeration3()
+ {
+ List<String> tmp = new ArrayList<String>();
+ tmp.add("a");
+ tmp.add("b");
+ assertEnumeration(Tools.toEnumeration(tmp.iterator()), "a",
"b");
+ }
+
+ public void testArrayToEnumerationThrowsIAE()
+ {
+ try
+ {
+ Tools.toEnumeration((Object[])null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayToEnumeration1()
+ {
+ assertEnumeration(Tools.toEnumeration(new String[]{}));
+ }
+
+ public void testArrayToEnumeration2()
+ {
+ assertEnumeration(Tools.toEnumeration(new String[]{"a"}),
"a");
+ }
+
+ public void testArrayToEnumeration3()
+ {
+ assertEnumeration(Tools.toEnumeration(new String[]{"a","b"}),
"a", "b");
+ }
+
+ public void testElementToEnumeration()
+ {
+ assertEnumeration(Tools.toEnumeration("a"), "a");
+ }
+
+ public void testEnumerationToSetThrowsIAE()
+ {
+ try
+ {
+ Tools.toSet((Enumeration)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testEnumerationToSet()
+ {
+ Vector<String> v = new Vector<String>();
+ v.add("a");
+ v.add("b");
+ v.add("a");
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, Tools.toSet(v.elements()));
+ }
+
+ public void testArrayToSetThrowsIAE()
+ {
+ try
+ {
+ Tools.toSet((Object[])null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayToSet()
+ {
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, Tools.toSet(new
String[]{"a","b","a"}));
+ }
+
+ public void testIteratorToSetThrowsIAE()
+ {
+ try
+ {
+ Tools.toSet((Iterator)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIteratorToSet()
+ {
+ List<String> list = new ArrayList<String>();
+ list.add("a");
+ list.add("b");
+ list.add("a");
+ Set<String> expected = new HashSet<String>();
+ expected.add("a");
+ expected.add("b");
+ assertEquals(expected, Tools.toSet(list.iterator()));
+ }
+
+ public void testEnumerationToListThrowsIAE()
+ {
+ try
+ {
+ Tools.toList((Enumeration)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testEnumerationToList()
+ {
+ Vector<String> v = new Vector<String>();
+ v.add("a");
+ v.add("b");
+ v.add("a");
+ List<String> expected = new ArrayList<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, Tools.toList(v.elements()));
+ }
+
+ public void testArrayToListThrowsIAE()
+ {
+ try
+ {
+ Tools.toList((Object[])null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayToList()
+ {
+ List<String> expected = new ArrayList<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, Tools.toList(new
String[]{"a","b","a"}));
+ }
+
+ public void testIteratorToListThrowsIAE()
+ {
+ try
+ {
+ Tools.toList((Iterator)null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testIteratorToList()
+ {
+ List<String> list = new ArrayList<String>();
+ list.add("a");
+ list.add("b");
+ list.add("a");
+ List<String> expected = new ArrayList<String>();
+ expected.add("a");
+ expected.add("b");
+ expected.add("a");
+ assertEquals(expected, Tools.toList(list.iterator()));
+ }
+
+
+ public void testArrayIteratorThrowsIAE()
+ {
+ try
+ {
+ Tools.iterator(null);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayIterator1()
+ {
+ assertIterator(Tools.iterator(new String[]{}));
+ }
+
+ public void testArrayIterator2()
+ {
+ assertIterator(Tools.iterator(new String[]{"a"}), "a");
+ }
+
+ public void testArrayIterator3()
+ {
+ assertIterator(Tools.iterator(new String[]{"a","b"}),
"a", "b");
+ }
+
+ public void testElementIterator()
+ {
+ assertIterator(Tools.iterator("a"), "a");
+ }
+
+ public void testArrayRangeIteratorThrowsIAE()
+ {
+ try
+ {
+ Tools.iterator(null, 0, 0);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ public void testArrayRangeIterator1()
+ {
+ assertIteratorMethodThrowIAE(new String[]{}, -1, 0);
+ assertIteratorMethodThrowIAE(new String[]{}, 0, 1);
+ assertIteratorMethodThrowIAE(new String[]{}, 1, 0);
+ assertIterator(Tools.iterator(new String[]{}, 0, 0));
+ }
+
+ public void testArrayRangeIterator2()
+ {
+ assertIteratorMethodThrowIAE(new String[]{"a"}, -1, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a"}, 1, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a"}, 1, 2);
+ assertIterator(Tools.iterator(new String[]{"a"}, 0, 0));
+ assertIterator(Tools.iterator(new String[]{"a"}, 1, 1));
+ assertIterator(Tools.iterator(new String[]{"a"}, 0, 1), "a");
+ }
+
+ public void testArrayRangeIterator3()
+ {
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, -1, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, 1, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, 2, 0);
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, 2, 1);
+ assertIteratorMethodThrowIAE(new String[]{"a", "b"}, 2, 3);
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 0, 0));
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 1, 1));
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 2, 2));
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 0, 1),
"a");
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 1, 2),
"b");
+ assertIterator(Tools.iterator(new String[]{"a","b"}, 0, 2),
"a", "b");
+ }
+
+ private <E> void assertEnumeration(Enumeration<E> elements, E...
expectedElements)
+ {
+ assertNotNull(elements);
+ for (E expectedElement : expectedElements)
+ {
+ assertTrue(elements.hasMoreElements());
+ E element = elements.nextElement();
+ assertEquals(expectedElement, element);
+ }
+ assertFalse(elements.hasMoreElements());
+ try
+ {
+ elements.nextElement();
+ fail();
+ }
+ catch (NoSuchElementException expected)
+ {
+ }
+ }
+
+ private void assertIteratorMethodThrowIAE(Object[] array, int from, int to)
+ {
+ try
+ {
+ Tools.iterator(array, from, to);
+ fail();
+ }
+ catch (IllegalArgumentException ignore)
+ {
+ }
+ }
+
+ private <E> void assertIterator(Iterator<E> elements, E...
expectedElements)
+ {
+ assertNotNull(elements);
+ for (E expectedElement : expectedElements)
+ {
+ assertTrue(elements.hasNext());
+ E element = elements.next();
+ assertEquals(expectedElement, element);
+ }
+ assertFalse(elements.hasNext());
+ try
+ {
+ elements.next();
+ fail();
+ }
+ catch (NoSuchElementException expected)
+ {
+ }
+ }
}