neilotoole 2004/12/10 22:22:58 Modified: collections/src/java/org/apache/commons/collections ListUtils.java Log: Added new methods: #retainAll(Collection, Collection) #removeAll(Collection, Collection) #unmodifiableListCopy(Collection) Revision Changes Path 1.29 +80 -1 jakarta-commons/collections/src/java/org/apache/commons/collections/ListUtils.java Index: ListUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ListUtils.java,v retrieving revision 1.28 retrieving revision 1.29 diff -u -r1.28 -r1.29 --- ListUtils.java 1 Apr 2004 20:12:00 -0000 1.28 +++ ListUtils.java 11 Dec 2004 06:22:58 -0000 1.29 @@ -258,6 +258,23 @@ return UnmodifiableList.decorate(list); } + + /** + * Returns an unmodifiable list copy of the collection. + * <p> + * This method uses the unmodifiable list implementation in the decorators subpackage. + * @param collection the <code>Collection</code> to copy. + * @return an unmodifiable <code>List</code>. + * @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. * <p> @@ -351,4 +368,66 @@ return FixedSizeList.decorate(list); } + /** + * Returns a List containing all the elements in <code>collection</code> + * that are also in <code>retain</code>. The cardinality of an element <code>e</code> + * in the returned list is the same as the cardinality of <code>e</code> + * in <code>collection</code> unless <code>retain</code> does not contain <code>e</code>, in which + * case the cardinality is zero. This method is useful if you do not wish to modify + * the collection <code>c</code> and thus cannot call <code>collection.retainAll(retain);</code>. + * + * @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 <code>List</code> containing all the elements of <code>c</code> + * that occur at least once in <code>retain</code>. + * @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 <code>remove</code> from <code>collection</code>. That is, this + * method returns a list containing all the elements in <code>c</code> + * that are not in <code>remove</code>. The cardinality of an element <code>e</code> + * in the returned collection is the same as the cardinality of <code>e</code> + * in <code>collection</code> unless <code>remove</code> contains <code>e</code>, in which + * case the cardinality is zero. This method is useful if you do not wish to modify + * <code>collection</code> and thus cannot call <code>collection.removeAll(remove);</code>. + * + * @param collection the collection from which items are removed (in the returned collection) + * @param remove the items to be removed from the returned <code>collection</code> + * @return a <code>List</code> containing all the elements of <code>c</code> except + * any elements that also occur in <code>remove</code>. + * @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; + } + }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]