Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 29730 invoked from network); 15 Sep 2009 05:57:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Sep 2009 05:57:07 -0000 Received: (qmail 31750 invoked by uid 500); 15 Sep 2009 05:57:06 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 31663 invoked by uid 500); 15 Sep 2009 05:57:06 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 31561 invoked by uid 99); 15 Sep 2009 05:57:06 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Sep 2009 05:57:06 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Sep 2009 05:57:02 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id EA68523889B5; Tue, 15 Sep 2009 05:56:40 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r815097 - /commons/proper/collections/trunk/src/java/org/apache/commons/collections/set/ListOrderedSet.java Date: Tue, 15 Sep 2009 05:56:40 -0000 To: commits@commons.apache.org From: bayard@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090915055640.EA68523889B5@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bayard Date: Tue Sep 15 05:56:40 2009 New Revision: 815097 URL: http://svn.apache.org/viewvc?rev=815097&view=rev Log: Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956. Also see the following revisions: ------------------------------------------------------------------------ r751869 | mbenson | 2009-03-09 15:10:00 -0700 (Mon, 09 Mar 2009) | 1 line return type narrowing ------------------------------------------------------------------------ Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/set/ListOrderedSet.java Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/set/ListOrderedSet.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/set/ListOrderedSet.java?rev=815097&r1=815096&r2=815097&view=diff ============================================================================== --- commons/proper/collections/trunk/src/java/org/apache/commons/collections/set/ListOrderedSet.java (original) +++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/set/ListOrderedSet.java Tue Sep 15 05:56:40 2009 @@ -21,8 +21,10 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.Set; +import org.apache.commons.collections.OrderedIterator; import org.apache.commons.collections.iterators.AbstractIteratorDecorator; import org.apache.commons.collections.list.UnmodifiableList; @@ -36,7 +38,7 @@ *

* The ListOrderedSet also has various useful direct methods. These include many * from List, such as get(int), remove(int) - * and indexOf(int). An unmodifiable List view of + * and indexOf(int). An unmodifiable List view of * the set can be obtained via asList(). *

* This class cannot implement the List interface directly as @@ -50,26 +52,26 @@ * @author Stephen Colebourne * @author Henning P. Schmiedehausen */ -public class ListOrderedSet extends AbstractSerializableSetDecorator implements Set { +public class ListOrderedSet extends AbstractSerializableSetDecorator implements Set { /** Serialization version */ private static final long serialVersionUID = -228664372470420141L; /** Internal list to hold the sequence of objects */ - protected final List setOrder; + protected final List setOrder; /** * Factory method to create an ordered set specifying the list and set to use. *

* The list and set must both be empty. - * + * * @param set the set to decorate, must be empty and not null * @param list the list to decorate, must be empty and not null * @throws IllegalArgumentException if set or list is null * @throws IllegalArgumentException if either the set or list is not empty * @since Commons Collections 3.1 */ - public static ListOrderedSet decorate(Set set, List list) { + public static ListOrderedSet decorate(Set set, List list) { if (set == null) { throw new IllegalArgumentException("Set must not be null"); } @@ -79,19 +81,19 @@ if (set.size() > 0 || list.size() > 0) { throw new IllegalArgumentException("Set and List must be empty"); } - return new ListOrderedSet(set, list); + return new ListOrderedSet(set, list); } /** * Factory method to create an ordered set. *

* An ArrayList is used to retain order. - * + * * @param set the set to decorate, must not be null * @throws IllegalArgumentException if set is null */ - public static ListOrderedSet decorate(Set set) { - return new ListOrderedSet(set); + public static ListOrderedSet decorate(Set set) { + return new ListOrderedSet(set); } /** @@ -101,53 +103,53 @@ *

* NOTE: If the list contains duplicates, the duplicates are removed, * altering the specified list. - * + * * @param list the list to decorate, must not be null * @throws IllegalArgumentException if list is null */ - public static ListOrderedSet decorate(List list) { + public static ListOrderedSet decorate(List list) { if (list == null) { throw new IllegalArgumentException("List must not be null"); } - Set set = new HashSet(list); + Set set = new HashSet(list); list.retainAll(set); - - return new ListOrderedSet(set, list); + + return new ListOrderedSet(set, list); } //----------------------------------------------------------------------- /** * Constructs a new empty ListOrderedSet using * a HashSet and an ArrayList internally. - * + * * @since Commons Collections 3.1 */ public ListOrderedSet() { - super(new HashSet()); - setOrder = new ArrayList(); + super(new HashSet()); + setOrder = new ArrayList(); } /** * Constructor that wraps (not copies). - * + * * @param set the set to decorate, must not be null * @throws IllegalArgumentException if set is null */ - protected ListOrderedSet(Set set) { + protected ListOrderedSet(Set set) { super(set); - setOrder = new ArrayList(set); + setOrder = new ArrayList(set); } /** * Constructor that wraps (not copies) the Set and specifies the list to use. *

* The set and list must both be correctly initialised to the same elements. - * + * * @param set the set to decorate, must not be null * @param list the list to decorate, must not be null * @throws IllegalArgumentException if set or list is null */ - protected ListOrderedSet(Set set, List list) { + protected ListOrderedSet(Set set, List list) { super(set); if (list == null) { throw new IllegalArgumentException("List must not be null"); @@ -158,10 +160,10 @@ //----------------------------------------------------------------------- /** * Gets an unmodifiable view of the order of the Set. - * + * * @return an unmodifiable list view */ - public List asList() { + public List asList() { return UnmodifiableList.decorate(setOrder); } @@ -171,27 +173,22 @@ setOrder.clear(); } - public Iterator iterator() { - return new OrderedSetIterator(setOrder.iterator(), collection); + public OrderedIterator iterator() { + return new OrderedSetIterator(setOrder.listIterator(), collection); } - public boolean add(Object object) { - if (collection.contains(object)) { - // re-adding doesn't change order - return collection.add(object); - } else { - // first add, so add to both set and list - boolean result = collection.add(object); + public boolean add(E object) { + if (collection.add(object)) { setOrder.add(object); - return result; + return true; } + return false; } - public boolean addAll(Collection coll) { + public boolean addAll(Collection coll) { boolean result = false; - for (Iterator it = coll.iterator(); it.hasNext();) { - Object object = it.next(); - result = result | add(object); + for (E e : coll) { + result |= add(e); } return result; } @@ -202,25 +199,24 @@ return result; } - public boolean removeAll(Collection coll) { + public boolean removeAll(Collection coll) { boolean result = false; - for (Iterator it = coll.iterator(); it.hasNext();) { - Object object = it.next(); - result = result | remove(object); + for (Iterator it = coll.iterator(); it.hasNext();) { + result |= remove(it.next()); } return result; } - public boolean retainAll(Collection coll) { + public boolean retainAll(Collection coll) { boolean result = collection.retainAll(coll); if (result == false) { return false; - } else if (collection.size() == 0) { + } + if (collection.size() == 0) { setOrder.clear(); } else { - for (Iterator it = setOrder.iterator(); it.hasNext();) { - Object object = it.next(); - if (collection.contains(object) == false) { + for (Iterator it = setOrder.iterator(); it.hasNext();) { + if (!collection.contains(it.next())) { it.remove(); } } @@ -232,12 +228,12 @@ return setOrder.toArray(); } - public Object[] toArray(Object a[]) { + public T[] toArray(T a[]) { return setOrder.toArray(a); } //----------------------------------------------------------------------- - public Object get(int index) { + public E get(int index) { return setOrder.get(index); } @@ -245,23 +241,22 @@ return setOrder.indexOf(object); } - public void add(int index, Object object) { - if (contains(object) == false) { + public void add(int index, E object) { + if (!contains(object)) { collection.add(object); setOrder.add(index, object); } } - public boolean addAll(int index, Collection coll) { + public boolean addAll(int index, Collection coll) { boolean changed = false; - for (Iterator it = coll.iterator(); it.hasNext();) { - Object object = it.next(); - if (contains(object) == false) { - collection.add(object); - setOrder.add(index, object); - index++; - changed = true; + for (E e : coll) { + if (contains(e)) { + continue; } + collection.add(e); + setOrder.add(index++, e); + changed = true; } return changed; } @@ -273,9 +268,9 @@ } /** - * Uses the underlying List's toString so that order is achieved. - * This means that the decorated Set's toString is not used, so - * any custom toStrings will be ignored. + * Uses the underlying List's toString so that order is achieved. + * This means that the decorated Set's toString is not used, so + * any custom toStrings will be ignored. */ // Fortunately List.toString and Set.toString look the same public String toString() { @@ -286,19 +281,20 @@ /** * Internal iterator handle remove. */ - static class OrderedSetIterator extends AbstractIteratorDecorator { - + static class OrderedSetIterator extends AbstractIteratorDecorator implements OrderedIterator { + /** Object we iterate on */ - protected final Collection set; + protected final Collection set; + /** Last object retrieved */ - protected Object last; + protected E last; - private OrderedSetIterator(Iterator iterator, Collection set) { + private OrderedSetIterator(ListIterator iterator, Collection set) { super(iterator); this.set = set; } - public Object next() { + public E next() { last = iterator.next(); return last; } @@ -308,6 +304,15 @@ iterator.remove(); last = null; } + + public boolean hasPrevious() { + return ((ListIterator) iterator).hasPrevious(); + } + + public E previous() { + last = ((ListIterator) iterator).previous(); + return last; + } } }