diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index 5a840f323..a57d75d0e 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -38,7 +38,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection; * Provides utility methods and decorators for {@link Collection} instances. * * @since Commons Collections 1.0 - * @version $Revision: 1.63 $ $Date: 2004/12/11 06:30:38 $ + * @version $Revision: 1.64 $ $Date: 2004/12/19 16:56:30 $ * * @author Rodney Waldhoff * @author Paul Jack @@ -55,7 +55,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection; */ public class CollectionUtils { - /** Constant to avoid repeated object creation */ + /** Constant to avoid repeated object creation */ private static Integer INTEGER_ONE = new Integer(1); /** @@ -1049,22 +1049,23 @@ public class CollectionUtils { return UnmodifiableCollection.decorate(collection); } - /** - * Returns an unmodifiable copy of the collection. - *
+ /** + * Returns an unmodifiable copy of the collection. + *
* This method uses the implementation in the decorators subpackage.
*
- * @param collection the Collection
to copy.
- * @return an unmodifiable Collection
.
- * @throws IllegalArgumentException if collection is null
- */
- public static Collection unmodifiableCollectionCopy(final Collection collection){
- if (collection == null) throw new IllegalArgumentException("null not permitted.");
-
- final Collection copy = new ArrayList(collection.size());
- copy.addAll(collection);
- return UnmodifiableCollection.decorate(copy);
- }
+ * @param collection the Collection
to copy.
+ * @return an unmodifiable Collection
.
+ * @throws IllegalArgumentException if collection is null
+ */
+ public static Collection unmodifiableCollectionCopy(final Collection collection){
+ if (collection == null) throw new IllegalArgumentException("null not permitted.");
+
+ final Collection copy = new ArrayList(collection.size());
+ copy.addAll(collection);
+ return UnmodifiableCollection.decorate(copy);
+ }
+
/**
* Returns a predicated (validating) collection backed by the given collection.
*
@@ -1111,42 +1112,41 @@ public class CollectionUtils {
return TransformedCollection.decorate(collection, transformer);
}
- /**
- * Returns a collection containing all the elements in collection
- * that are also in retain
. The cardinality of an element e
- * in the returned collection is the same as the cardinality of e
- * in collection
unless retain
does not contain e
, in which
- * case the cardinality is zero. This method is useful if you do not wish to modify
- * the collection c
and thus cannot call c.retainAll(retain);
.
- *
- * @param collection the collection whose contents are the target of the #retailAll operation
- * @param retain the collection containing the elements to be retained in the returned collection
- * @return a Collection
containing all the elements of collection
- * that occur at least once in retain
.
- * @throws NullPointerException if either parameter is null
- */
- public static Collection retainAll(final Collection collection, final Collection retain) {
- return ListUtils.retainAll(collection, retain);
- }
+ /**
+ * Returns a collection containing all the elements in collection
+ * that are also in retain
. The cardinality of an element e
+ * in the returned collection is the same as the cardinality of e
+ * in collection
unless retain
does not contain e
, in which
+ * case the cardinality is zero. This method is useful if you do not wish to modify
+ * the collection c
and thus cannot call c.retainAll(retain);
.
+ *
+ * @param collection the collection whose contents are the target of the #retailAll operation
+ * @param retain the collection containing the elements to be retained in the returned collection
+ * @return a Collection
containing all the elements of collection
+ * that occur at least once in retain
.
+ * @throws NullPointerException if either parameter is null
+ */
+ public static Collection retainAll(Collection collection, Collection retain) {
+ return ListUtils.retainAll(collection, retain);
+ }
- /**
- * Removes the elements in remove
from collection
. That is, this
- * method returns a collection containing all the elements in c
- * that are not in remove
. The cardinality of an element e
- * in the returned collection is the same as the cardinality of e
- * in collection
unless remove
contains e
, in which
- * case the cardinality is zero. This method is useful if you do not wish to modify
- * the collection c
and thus cannot call collection.removeAll(remove);
.
- *
- * @param collection the collection from which items are removed (in the returned collection)
- * @param remove the items to be removed from the returned collection
- * @return a Collection
containing all the elements of collection
except
- * any elements that also occur in remove
.
- * @throws NullPointerException if either parameter is null
- */
- public static Collection removeAll(final Collection collection, final Collection remove) {
- return ListUtils.retainAll(collection, remove);
- }
-
+ /**
+ * Removes the elements in remove
from collection
. That is, this
+ * method returns a collection containing all the elements in c
+ * that are not in remove
. The cardinality of an element e
+ * in the returned collection is the same as the cardinality of e
+ * in collection
unless remove
contains e
, in which
+ * case the cardinality is zero. This method is useful if you do not wish to modify
+ * the collection c
and thus cannot call collection.removeAll(remove);
.
+ *
+ * @param collection the collection from which items are removed (in the returned collection)
+ * @param remove the items to be removed from the returned collection
+ * @return a Collection
containing all the elements of collection
except
+ * any elements that also occur in remove
.
+ * @throws NullPointerException if either parameter is null
+ */
+ public static Collection removeAll(Collection collection, Collection remove) {
+ return ListUtils.retainAll(collection, remove);
+ }
}
diff --git a/src/java/org/apache/commons/collections/ListUtils.java b/src/java/org/apache/commons/collections/ListUtils.java
index 8b236aacf..1ed05bdfe 100644
--- a/src/java/org/apache/commons/collections/ListUtils.java
+++ b/src/java/org/apache/commons/collections/ListUtils.java
@@ -33,7 +33,7 @@ import org.apache.commons.collections.list.UnmodifiableList;
* Provides utility methods and decorators for {@link List} instances.
*
* @since Commons Collections 1.0
- * @version $Revision: 1.29 $ $Date: 2004/12/11 06:22:58 $
+ * @version $Revision: 1.30 $ $Date: 2004/12/19 16:56:30 $
*
* @author Federico Barbieri
* @author Peter Donald
@@ -259,21 +259,21 @@ public class ListUtils {
}
- /**
- * Returns an unmodifiable list copy of the collection.
- *
- * This method uses the unmodifiable list implementation in the decorators subpackage.
- * @param collection the Collection
to copy.
- * @return an unmodifiable List
.
- * @throws IllegalArgumentException if collection is null.
- */
- public static List unmodifiableListCopy(final Collection collection) {
- if (collection == null) throw new IllegalArgumentException("null not permitted.");
-
- final List copy = new ArrayList(collection.size());
- copy.addAll(collection);
- return UnmodifiableList.decorate(copy);
- }
+ /**
+ * Returns an unmodifiable list copy of the collection.
+ *
+ * This method uses the unmodifiable list implementation in the decorators subpackage.
+ * @param collection the Collection
to copy.
+ * @return an unmodifiable List
.
+ * @throws IllegalArgumentException if collection is null.
+ */
+ public static List unmodifiableListCopy(final Collection collection) {
+ if (collection == null) throw new IllegalArgumentException("null not permitted.");
+
+ final List copy = new ArrayList(collection.size());
+ copy.addAll(collection);
+ return UnmodifiableList.decorate(copy);
+ }
/**
* Returns a predicated (validating) list backed by the given list.
@@ -369,65 +369,55 @@ public class ListUtils {
}
/**
- * Returns a List containing all the elements in collection
- * that are also in retain
. The cardinality of an element e
- * in the returned list is the same as the cardinality of e
- * in collection
unless retain
does not contain e
, in which
- * case the cardinality is zero. This method is useful if you do not wish to modify
- * the collection c
and thus cannot call collection.retainAll(retain);
.
- *
- * @param collection the collection whose contents are the target of the #retailAll operation
- * @param retain the collection containing the elements to be retained in the returned collection
- * @return a List
containing all the elements of c
- * that occur at least once in retain
.
- * @throws NullPointerException if either parameter is null
- */
- public static List retainAll(final Collection collection, final Collection retain) {
- final List list = new ArrayList(Math.min(collection.size(), retain.size()));
+ * Returns a List containing all the elements in collection
+ * that are also in retain
. The cardinality of an element e
+ * in the returned list is the same as the cardinality of e
+ * in collection
unless retain
does not contain e
, in which
+ * case the cardinality is zero. This method is useful if you do not wish to modify
+ * the collection c
and thus cannot call collection.retainAll(retain);
.
+ *
+ * @param collection the collection whose contents are the target of the #retailAll operation
+ * @param retain the collection containing the elements to be retained in the returned collection
+ * @return a List
containing all the elements of c
+ * that occur at least once in retain
.
+ * @throws NullPointerException if either parameter is null
+ */
+ public static List retainAll(Collection collection, Collection retain) {
+ List list = new ArrayList(Math.min(collection.size(), retain.size()));
- Object item = null;
- for (final Iterator iter = collection.iterator(); iter.hasNext();)
- {
- item = iter.next();
+ for (Iterator iter = collection.iterator(); iter.hasNext();) {
+ Object obj = iter.next();
+ if (retain.contains(obj)) {
+ list.add(obj);
+ }
+ }
+ return list;
+ }
- if (retain.contains(item))
- {
- list.add(item);
- }
- }
+ /**
+ * Removes the elements in remove
from collection
. That is, this
+ * method returns a list containing all the elements in c
+ * that are not in remove
. The cardinality of an element e
+ * in the returned collection is the same as the cardinality of e
+ * in collection
unless remove
contains e
, in which
+ * case the cardinality is zero. This method is useful if you do not wish to modify
+ * collection
and thus cannot call collection.removeAll(remove);
.
+ *
+ * @param collection the collection from which items are removed (in the returned collection)
+ * @param remove the items to be removed from the returned collection
+ * @return a List
containing all the elements of c
except
+ * any elements that also occur in remove
.
+ * @throws NullPointerException if either parameter is null
+ */
+ public static List removeAll(Collection collection, Collection remove) {
+ List list = new ArrayList();
+ for (Iterator iter = collection.iterator(); iter.hasNext();) {
+ Object obj = iter.next();
+ if (remove.contains(obj) == false) {
+ list.add(obj);
+ }
+ }
+ return list;
+ }
- return list;
- }
-
- /**
- * Removes the elements in remove
from collection
. That is, this
- * method returns a list containing all the elements in c
- * that are not in remove
. The cardinality of an element e
- * in the returned collection is the same as the cardinality of e
- * in collection
unless remove
contains e
, in which
- * case the cardinality is zero. This method is useful if you do not wish to modify
- * collection
and thus cannot call collection.removeAll(remove);
.
- *
- * @param collection the collection from which items are removed (in the returned collection)
- * @param remove the items to be removed from the returned collection
- * @return a List
containing all the elements of c
except
- * any elements that also occur in remove
.
- * @throws NullPointerException if either parameter is null
- */
- public static List removeAll(final Collection collection, final Collection remove) {
- final List list = new ArrayList();
-
- Object o = null;
- for (final Iterator iter = collection.iterator(); iter.hasNext();)
- {
- o = iter.next();
- if (remove.contains(o) == false)
- {
- list.add(o);
- }
- }
-
- return list;
- }
-
}
diff --git a/src/java/org/apache/commons/collections/MapUtils.java b/src/java/org/apache/commons/collections/MapUtils.java
index 6e9cec5db..05571692e 100644
--- a/src/java/org/apache/commons/collections/MapUtils.java
+++ b/src/java/org/apache/commons/collections/MapUtils.java
@@ -67,7 +67,7 @@ import org.apache.commons.collections.map.UnmodifiableSortedMap;
*
*
* @since Commons Collections 1.0
- * @version $Revision: 1.50 $ $Date: 2004/12/11 06:26:13 $
+ * @version $Revision: 1.51 $ $Date: 2004/12/19 16:56:30 $
*
* @author James Strachan
* @author Nissim Karpenstein
@@ -1222,19 +1222,19 @@ public class MapUtils {
return UnmodifiableMap.decorate(map);
}
- /**
- * Returns an unmodifiable copy of the map.
+ /**
+ * Returns an unmodifiable copy of the map.
* @param map the map to make an unmodifiable copy of, must not be null
* @return an unmodifiable map backed by the given map
* @throws IllegalArgumentException if the map is null
- */
- public static Map unmodifiableMapCopy(final Map map) {
- if (map == null) throw new IllegalArgumentException("null not permitted.");
-
- final Map copy = new HashMap(map.size(), 1.0f);
- copy.putAll(map);
- return MapUtils.unmodifiableMap(copy);
- }
+ */
+ public static Map unmodifiableMapCopy(Map map) {
+ if (map == null) throw new IllegalArgumentException("null not permitted.");
+
+ Map copy = new HashMap(map.size(), 1.0f);
+ copy.putAll(map);
+ return MapUtils.unmodifiableMap(copy);
+ }
/**
* Returns a predicated (validating) map backed by the given map.
diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java
index aa99ff36c..ddec13e5c 100644
--- a/src/test/org/apache/commons/collections/TestCollectionUtils.java
+++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java
@@ -17,7 +17,6 @@ package org.apache.commons.collections;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
@@ -52,7 +51,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
* @author Steven Melzer
* @author Neil O'Toole
*
- * @version $Revision: 1.42 $ $Date: 2004/12/11 06:30:38 $
+ * @version $Revision: 1.43 $ $Date: 2004/12/19 16:56:31 $
*/
public class TestCollectionUtils extends TestCase {
@@ -1198,30 +1197,26 @@ public class TestCollectionUtils extends TestCase {
}
public void testUnmodifiableCollectionCopy() {
- Collection collection = new ArrayList();
- collection.add("a");
- Collection copy = CollectionUtils.unmodifiableCollectionCopy(collection);
+ Collection collection = new ArrayList();
+ collection.add("a");
+ Collection copy = CollectionUtils.unmodifiableCollectionCopy(collection);
- assertTrue(copy instanceof Unmodifiable);
- assertTrue(CollectionUtils.isEqualCollection(collection, copy));
- collection.clear();
- assertTrue(copy.isEmpty() == false);
+ assertTrue(copy instanceof Unmodifiable);
+ assertTrue(CollectionUtils.isEqualCollection(collection, copy));
+ collection.clear();
+ assertTrue(copy.isEmpty() == false);
- try
- {
+ try {
copy.clear();
fail("should be unmodifiable.");
- }
- catch (UnsupportedOperationException uoe)
- {} // this is what we want
-
- try
- {
+ } catch (UnsupportedOperationException uoe) {
+ } // this is what we want
+
+ try {
copy = CollectionUtils.unmodifiableCollectionCopy(null);
fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException iae) {
}
- catch(IllegalArgumentException iae)
- {}
- }
+ }
}
diff --git a/src/test/org/apache/commons/collections/TestListUtils.java b/src/test/org/apache/commons/collections/TestListUtils.java
index ee1d35109..cded315cb 100644
--- a/src/test/org/apache/commons/collections/TestListUtils.java
+++ b/src/test/org/apache/commons/collections/TestListUtils.java
@@ -27,7 +27,7 @@ import org.apache.commons.collections.list.PredicatedList;
/**
* Tests for ListUtils.
*
- * @version $Revision: 1.20 $ $Date: 2004/12/11 06:24:10 $
+ * @version $Revision: 1.21 $ $Date: 2004/12/19 16:56:31 $
*
* @author Stephen Colebourne
* @author Neil O'Toole
@@ -35,17 +35,17 @@ import org.apache.commons.collections.list.PredicatedList;
*/
public class TestListUtils extends BulkTest {
- private static final String a = "a";
- private static final String b = "b";
- private static final String c = "c";
- private static final String d = "d";
- private static final String e = "e";
- private static final String x = "x";
+ private static final String a = "a";
+ private static final String b = "b";
+ private static final String c = "c";
+ private static final String d = "d";
+ private static final String e = "e";
+ private static final String x = "x";
- private String[] fullArray;
- private List fullList;
-
- public TestListUtils(String name) {
+ private String[] fullArray;
+ private List fullList;
+
+ public TestListUtils(String name) {
super(name);
}
@@ -53,10 +53,10 @@ public class TestListUtils extends BulkTest {
return BulkTest.makeSuite(TestListUtils.class);
}
- public void setUp() {
- fullArray = new String[]{a, b, c, d, e};
- fullList = new ArrayList(Arrays.asList(fullArray));
- }
+ public void setUp() {
+ fullArray = new String[]{a, b, c, d, e};
+ fullList = new ArrayList(Arrays.asList(fullArray));
+ }
public void testNothing() {
@@ -106,12 +106,12 @@ public class TestListUtils extends BulkTest {
assertEquals(6, list.size());
}
- public void testEquals() {
- Collection data = Arrays.asList( new String[] { "a", "b", "c" });
-
- List a = new ArrayList( data );
- List b = new ArrayList( data );
-
+ public void testEquals() {
+ Collection data = Arrays.asList( new String[] { "a", "b", "c" });
+
+ List a = new ArrayList( data );
+ List b = new ArrayList( data );
+
assertEquals(true, a.equals(b));
assertEquals(true, ListUtils.isEqualList(a, b));
a.clear();
@@ -119,14 +119,14 @@ public class TestListUtils extends BulkTest {
assertEquals(false, ListUtils.isEqualList(a, null));
assertEquals(false, ListUtils.isEqualList(null, b));
assertEquals(true, ListUtils.isEqualList(null, null));
- }
-
- public void testHashCode() {
- Collection data = Arrays.asList( new String[] { "a", "b", "c" });
-
- List a = new ArrayList( data );
- List b = new ArrayList( data );
-
+ }
+
+ public void testHashCode() {
+ Collection data = Arrays.asList( new String[] { "a", "b", "c" });
+
+ List a = new ArrayList( data );
+ List b = new ArrayList( data );
+
assertEquals(true, a.hashCode() == b.hashCode());
assertEquals(true, a.hashCode() == ListUtils.hashCodeForList(a));
assertEquals(true, b.hashCode() == ListUtils.hashCodeForList(b));
@@ -134,79 +134,67 @@ public class TestListUtils extends BulkTest {
a.clear();
assertEquals(false, ListUtils.hashCodeForList(a) == ListUtils.hashCodeForList(b));
assertEquals(0, ListUtils.hashCodeForList(null));
- }
-
- public void testUnmodifiableListCopy() {
- List list = new ArrayList();
- list.add("a");
- List copy = ListUtils.unmodifiableListCopy(list);
+ }
+
+ public void testUnmodifiableListCopy() {
+ List list = new ArrayList();
+ list.add("a");
+ List copy = ListUtils.unmodifiableListCopy(list);
- assertTrue(copy instanceof Unmodifiable);
- assertTrue(list.equals(copy));
- list.clear();
- assertTrue(copy.isEmpty() == false);
+ assertTrue(copy instanceof Unmodifiable);
+ assertTrue(list.equals(copy));
+ list.clear();
+ assertTrue(copy.isEmpty() == false);
- try
- {
- copy.clear();
- fail("should be unmodifiable.");
- }
- catch (UnsupportedOperationException uoe)
- {
- // this is what we want
- }
-
- try
- {
- list = ListUtils.unmodifiableListCopy(null);
- fail("expecting IllegalArgumentException");
- }
- catch (IllegalArgumentException iae)
- {
- // this is what we want
- }
- }
-
- public void testRetainAll() {
- List sub = new ArrayList();
- sub.add(a);
- sub.add(b);
- sub.add(x);
+ try {
+ copy.clear();
+ fail("should be unmodifiable.");
+ } catch (UnsupportedOperationException uoe) {
+ // this is what we want
+ }
+
+ try {
+ list = ListUtils.unmodifiableListCopy(null);
+ fail("expecting IllegalArgumentException");
+ } catch (IllegalArgumentException iae) {
+ // this is what we want
+ }
+ }
+
+ public void testRetainAll() {
+ List sub = new ArrayList();
+ sub.add(a);
+ sub.add(b);
+ sub.add(x);
- List retained = ListUtils.retainAll(fullList, sub);
- assertTrue(retained.size() == 2);
- sub.remove(x);
- assertTrue(retained.equals(sub));
- fullList.retainAll(sub);
- assertTrue(retained.equals(fullList));
-
- try
- {
- List list = ListUtils.retainAll(null, null);
- fail("expecting NullPointerException");
- }
- catch(NullPointerException npe)
- {} // this is what we want
- }
+ List retained = ListUtils.retainAll(fullList, sub);
+ assertTrue(retained.size() == 2);
+ sub.remove(x);
+ assertTrue(retained.equals(sub));
+ fullList.retainAll(sub);
+ assertTrue(retained.equals(fullList));
+
+ try {
+ List list = ListUtils.retainAll(null, null);
+ fail("expecting NullPointerException");
+ } catch(NullPointerException npe){} // this is what we want
+ }
- public void testRemoveAll() {
- List sub = new ArrayList();
- sub.add(a);
- sub.add(b);
- sub.add(x);
+ public void testRemoveAll() {
+ List sub = new ArrayList();
+ sub.add(a);
+ sub.add(b);
+ sub.add(x);
- List remainder = ListUtils.removeAll(fullList, sub);
- assertTrue(remainder.size() == 3);
- fullList.removeAll(sub);
- assertTrue(remainder.equals(fullList));
-
- try
- {
- List list = ListUtils.removeAll(null, null);
- fail("expecting NullPointerException");
- }
- catch(NullPointerException npe)
- {} // this is what we want
- }
-
+ List remainder = ListUtils.removeAll(fullList, sub);
+ assertTrue(remainder.size() == 3);
+ fullList.removeAll(sub);
+ assertTrue(remainder.equals(fullList));
+
+ try {
+ List list = ListUtils.removeAll(null, null);
+ fail("expecting NullPointerException");
+ } catch(NullPointerException npe) {} // this is what we want
+ }
+
}
diff --git a/src/test/org/apache/commons/collections/TestMapUtils.java b/src/test/org/apache/commons/collections/TestMapUtils.java
index 2c0ff0db1..78ac94ead 100644
--- a/src/test/org/apache/commons/collections/TestMapUtils.java
+++ b/src/test/org/apache/commons/collections/TestMapUtils.java
@@ -37,7 +37,7 @@ import org.apache.commons.collections.map.TestPredicatedMap;
/**
* Tests for MapUtils.
*
- * @version $Revision: 1.25 $ $Date: 2004/12/11 06:26:13 $
+ * @version $Revision: 1.26 $ $Date: 2004/12/19 16:56:31 $
*
* @author Stephen Colebourne
* @author Arun Mammen Thomas
@@ -771,36 +771,30 @@ public class TestMapUtils extends BulkTest {
assertEquals(EXPECTED_OUT, out.toString());
}
- public void testUnmodifiableMapCopy() {
- Map map = new HashMap();
- map.put("key", "value");
+ public void testUnmodifiableMapCopy() {
+ Map map = new HashMap();
+ map.put("key", "value");
- Map copy = MapUtils.unmodifiableMapCopy(map);
- assertTrue(copy instanceof Unmodifiable);
- assertEquals(map, copy);
- map.clear();
- assertFalse(map.equals(copy));
+ Map copy = MapUtils.unmodifiableMapCopy(map);
+ assertTrue(copy instanceof Unmodifiable);
+ assertEquals(map, copy);
+ map.clear();
+ assertFalse(map.equals(copy));
- try
- {
- copy.clear();
- fail("should be unmodifiable.");
- }
- catch (UnsupportedOperationException uoe)
- {
- // this is what we want
- }
-
- try
- {
- map = MapUtils.unmodifiableMapCopy(null);
- fail("expecting IllegalArgumentException");
- }
- catch (IllegalArgumentException iae)
- {
- // this is what we want
- }
+ try {
+ copy.clear();
+ fail("should be unmodifiable.");
+ } catch (UnsupportedOperationException uoe) {
+ // this is what we want
+ }
+
+ try {
+ map = MapUtils.unmodifiableMapCopy(null);
+ fail("expecting IllegalArgumentException");
+ } catch (IllegalArgumentException iae) {
+ // this is what we want
+ }
+
+ }
- }
-
}