diff --git a/src/java/org/apache/commons/collections/ListUtils.java b/src/java/org/apache/commons/collections/ListUtils.java index d368e85e5..8b236aacf 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.28 $ $Date: 2004/04/01 20:12:00 $ + * @version $Revision: 1.29 $ $Date: 2004/12/11 06:22:58 $ * * @author Federico Barbieri * @author Peter Donald @@ -258,6 +258,23 @@ public class ListUtils { return UnmodifiableList.decorate(list); } + + /** + * 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.
*
@@ -351,4 +368,66 @@ public class ListUtils {
return FixedSizeList.decorate(list);
}
+ /**
+ * 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()));
+
+ Object item = null;
+ for (final Iterator iter = collection.iterator(); iter.hasNext();)
+ {
+ item = iter.next();
+
+ if (retain.contains(item))
+ {
+ list.add(item);
+ }
+ }
+
+ 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;
+ }
+
}