commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
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 GMT
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 @@
  * <p>
  * The ListOrderedSet also has various useful direct methods. These include many
  * from <code>List</code>, such as <code>get(int)</code>, <code>remove(int)</code>
- * and <code>indexOf(int)</code>. An unmodifiable <code>List</code>
view of 
+ * and <code>indexOf(int)</code>. An unmodifiable <code>List</code>
view of
  * the set can be obtained via <code>asList()</code>.
  * <p>
  * This class cannot implement the <code>List</code> interface directly as
@@ -50,26 +52,26 @@
  * @author Stephen Colebourne
  * @author Henning P. Schmiedehausen
  */
-public class ListOrderedSet extends AbstractSerializableSetDecorator implements Set {
+public class ListOrderedSet<E> extends AbstractSerializableSetDecorator<E> implements
Set<E> {
 
     /** Serialization version */
     private static final long serialVersionUID = -228664372470420141L;
 
     /** Internal list to hold the sequence of objects */
-    protected final List setOrder;
+    protected final List<E> setOrder;
 
     /**
      * Factory method to create an ordered set specifying the list and set to use.
      * <p>
      * 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 <E> ListOrderedSet<E> decorate(Set<E> set, List<E>
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<E>(set, list);
     }
 
     /**
      * Factory method to create an ordered set.
      * <p>
      * An <code>ArrayList</code> 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 <E> ListOrderedSet<E> decorate(Set<E> set) {
+        return new ListOrderedSet<E>(set);
     }
 
     /**
@@ -101,53 +103,53 @@
      * <p>
      * 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 <E> ListOrderedSet<E> decorate(List<E> list) {
         if (list == null) {
             throw new IllegalArgumentException("List must not be null");
         }
-        Set set = new HashSet(list);
+        Set<E> set = new HashSet<E>(list);
         list.retainAll(set);
-        
-        return new ListOrderedSet(set, list);
+
+        return new ListOrderedSet<E>(set, list);
     }
 
     //-----------------------------------------------------------------------
     /**
      * Constructs a new empty <code>ListOrderedSet</code> using
      * a <code>HashSet</code> and an <code>ArrayList</code> internally.
-     * 
+     *
      * @since Commons Collections 3.1
      */
     public ListOrderedSet() {
-        super(new HashSet());
-        setOrder = new ArrayList();
+        super(new HashSet<E>());
+        setOrder = new ArrayList<E>();
     }
 
     /**
      * 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<E> set) {
         super(set);
-        setOrder = new ArrayList(set);
+        setOrder = new ArrayList<E>(set);
     }
 
     /**
      * Constructor that wraps (not copies) the Set and specifies the list to use.
      * <p>
      * 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<E> set, List<E> 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<E> asList() {
         return UnmodifiableList.decorate(setOrder);
     }
 
@@ -171,27 +173,22 @@
         setOrder.clear();
     }
 
-    public Iterator iterator() {
-        return new OrderedSetIterator(setOrder.iterator(), collection);
+    public OrderedIterator<E> iterator() {
+        return new OrderedSetIterator<E>(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<? extends E> 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<E> 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> 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<? extends E> 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<E> extends AbstractIteratorDecorator<E> implements
OrderedIterator<E> {
+
         /** Object we iterate on */
-        protected final Collection set;
+        protected final Collection<E> set;
+
         /** Last object retrieved */
-        protected Object last;
+        protected E last;
 
-        private OrderedSetIterator(Iterator iterator, Collection set) {
+        private OrderedSetIterator(ListIterator<E> iterator, Collection<E> 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<E>) iterator).hasPrevious();
+        }
+
+        public E previous() {
+            last = ((ListIterator<E>) iterator).previous();
+            return last;
+        }
     }
 
 }



Mime
View raw message