commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r815080 - /commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
Date Tue, 15 Sep 2009 05:56:10 GMT
Author: bayard
Date: Tue Sep 15 05:56:10 2009
New Revision: 815080

URL: http://svn.apache.org/viewvc?rev=815080&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:

    ------------------------------------------------------------------------
    r471189 | scolebourne | 2006-11-04 05:57:57 -0800 (Sat, 04 Nov 2006) | 1 line
    
    Remove getMap(), getOrderedMap() and getSortedMap() - use decorated()
    ------------------------------------------------------------------------

Modified:
    commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java?rev=815080&r1=815079&r2=815080&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
(original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/ListOrderedMap.java
Tue Sep 15 05:56:10 2009
@@ -32,11 +32,10 @@
 import java.util.NoSuchElementException;
 import java.util.Set;
 
-import org.apache.commons.collections.MapIterator;
 import org.apache.commons.collections.OrderedMap;
 import org.apache.commons.collections.OrderedMapIterator;
 import org.apache.commons.collections.ResettableIterator;
-import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
+import org.apache.commons.collections.iterators.AbstractUntypedIteratorDecorator;
 import org.apache.commons.collections.keyvalue.AbstractMapEntry;
 import org.apache.commons.collections.list.UnmodifiableList;
 
@@ -68,15 +67,15 @@
  * @author Matt Benson
  * @author Dave Meikle
  */
-public class ListOrderedMap
-        extends AbstractMapDecorator
-        implements OrderedMap, Serializable {
+public class ListOrderedMap<K, V>
+        extends AbstractMapDecorator<K, V>
+        implements OrderedMap<K, V>, Serializable {
 
     /** Serialization version */
     private static final long serialVersionUID = 2728177751851003750L;
 
     /** Internal list to hold the sequence of objects */
-    protected final List insertOrder = new ArrayList();
+    protected final List<K> insertOrder = new ArrayList<K>();
 
     /**
      * Factory method to create an ordered map.
@@ -86,8 +85,8 @@
      * @param map  the map to decorate, must not be null
      * @throws IllegalArgumentException if map is null
      */
-    public static OrderedMap decorate(Map map) {
-        return new ListOrderedMap(map);
+    public static <K, V> OrderedMap<K, V> decorate(Map<K, V> map) {
+        return new ListOrderedMap<K, V>(map);
     }
 
     //-----------------------------------------------------------------------
@@ -98,7 +97,7 @@
      * @since Commons Collections 3.1
      */
     public ListOrderedMap() {
-        this(new HashMap());
+        this(new HashMap<K, V>());
     }
 
     /**
@@ -107,9 +106,9 @@
      * @param map  the map to decorate, must not be null
      * @throws IllegalArgumentException if map is null
      */
-    protected ListOrderedMap(Map map) {
+    protected ListOrderedMap(Map<K, V> map) {
         super(map);
-        insertOrder.addAll(getMap().keySet());
+        insertOrder.addAll(decorated().keySet());
     }
 
     //-----------------------------------------------------------------------
@@ -133,6 +132,7 @@
      * @throws ClassNotFoundException
      * @since Commons Collections 3.1
      */
+    @SuppressWarnings("unchecked")
     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
         in.defaultReadObject();
         map = (Map) in.readObject();
@@ -140,12 +140,8 @@
 
     // Implement OrderedMap
     //-----------------------------------------------------------------------
-    public MapIterator mapIterator() {
-        return orderedMapIterator();
-    }
-
-    public OrderedMapIterator orderedMapIterator() {
-        return new ListOrderedMapIterator(this);
+    public OrderedMapIterator<K, V> mapIterator() {
+        return new ListOrderedMapIterator<K, V>(this);
     }
 
     /**
@@ -154,7 +150,7 @@
      * @return the first key currently in this map
      * @throws NoSuchElementException if this map is empty
      */
-    public Object firstKey() {
+    public K firstKey() {
         if (size() == 0) {
             throw new NoSuchElementException("Map is empty");
         }
@@ -167,7 +163,7 @@
      * @return the last key currently in this map
      * @throws NoSuchElementException if this map is empty
      */
-    public Object lastKey() {
+    public K lastKey() {
         if (size() == 0) {
             throw new NoSuchElementException("Map is empty");
         }
@@ -181,7 +177,7 @@
      * @param key  the key to find previous for
      * @return the next key, null if no match or at start
      */
-    public Object nextKey(Object key) {
+    public K nextKey(Object key) {
         int index = insertOrder.indexOf(key);
         if (index >= 0 && index < size() - 1) {
             return insertOrder.get(index + 1);
@@ -196,7 +192,7 @@
      * @param key  the key to find previous for
      * @return the previous key, null if no match or at start
      */
-    public Object previousKey(Object key) {
+    public K previousKey(Object key) {
         int index = insertOrder.indexOf(key);
         if (index > 0) {
             return insertOrder.get(index - 1);
@@ -205,21 +201,20 @@
     }
 
     //-----------------------------------------------------------------------
-    public Object put(Object key, Object value) {
-        if (getMap().containsKey(key)) {
+    public V put(K key, V value) {
+        if (decorated().containsKey(key)) {
             // re-adding doesn't change order
-            return getMap().put(key, value);
+            return decorated().put(key, value);
         } else {
             // first add, so add to both map and list
-            Object result = getMap().put(key, value);
+            V result = decorated().put(key, value);
             insertOrder.add(key);
             return result;
         }
     }
 
-    public void putAll(Map map) {
-        for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
-            Map.Entry entry = (Map.Entry) it.next();
+    public void putAll(Map<? extends K, ? extends V> map) {
+        for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
             put(entry.getKey(), entry.getValue());
         }
     }
@@ -231,22 +226,21 @@
      * @param index the index in the Map to start at.
      * @param map the Map containing the values to be added.
      */
-    public void putAll(int index, Map map) {
-        for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
-            Map.Entry entry = (Map.Entry) it.next();
+    public void putAll(int index, Map<? extends K, ? extends V> map) {
+        for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
             put(index, entry.getKey(), entry.getValue());
             index++;
         }
     }
 
-    public Object remove(Object key) {
-        Object result = getMap().remove(key);
+    public V remove(Object key) {
+        V result = decorated().remove(key);
         insertOrder.remove(key);
         return result;
     }
 
     public void clear() {
-        getMap().clear();
+        decorated().clear();
         insertOrder.clear();
     }
 
@@ -259,8 +253,8 @@
      * @see #keyList()
      * @return the fully modifiable collection view over the keys
      */
-    public Set keySet() {
-        return new KeySetView(this);
+    public Set<K> keySet() {
+        return new KeySetView<K>(this);
     }
 
     /**
@@ -273,7 +267,7 @@
      * @return the unmodifiable list view over the keys
      * @since Commons Collections 3.2
      */
-    public List keyList() {
+    public List<K> keyList() {
         return UnmodifiableList.decorate(insertOrder);
     }
 
@@ -288,8 +282,8 @@
      * @see #valueList()
      * @return the fully modifiable collection view over the values
      */
-    public Collection values() {
-        return new ValuesView(this);
+    public Collection<V> values() {
+        return new ValuesView<V>(this);
     }
 
     /**
@@ -302,8 +296,8 @@
      * @return the partially modifiable list view over the values
      * @since Commons Collections 3.2
      */
-    public List valueList() {
-        return new ValuesView(this);
+    public List<V> valueList() {
+        return new ValuesView<V>(this);
     }
 
     /**
@@ -313,8 +307,8 @@
      *
      * @return the fully modifiable set view over the entries
      */
-    public Set entrySet() {
-        return new EntrySetView(this, this.insertOrder);
+    public Set<Map.Entry<K, V>> entrySet() {
+        return new EntrySetView<K, V>(this, this.insertOrder);
     }
 
     //-----------------------------------------------------------------------
@@ -330,11 +324,9 @@
         StringBuffer buf = new StringBuffer();
         buf.append('{');
         boolean first = true;
-        Iterator it = entrySet().iterator();
-        while (it.hasNext()) {
-            Map.Entry entry = (Map.Entry) it.next();
-            Object key = entry.getKey();
-            Object value = entry.getValue();
+        for (Map.Entry<K, V> entry : entrySet()) {
+            K key = entry.getKey();
+            V value = entry.getValue();
             if (first) {
                 first = false;
             } else {
@@ -356,7 +348,7 @@
      * @return the key at the specified index
      * @throws IndexOutOfBoundsException if the index is invalid
      */
-    public Object get(int index) {
+    public K get(int index) {
         return insertOrder.get(index);
     }
     
@@ -367,7 +359,7 @@
      * @return the key at the specified index
      * @throws IndexOutOfBoundsException if the index is invalid
      */
-    public Object getValue(int index) {
+    public V getValue(int index) {
         return get(insertOrder.get(index));
     }
     
@@ -389,8 +381,8 @@
      * @throws IndexOutOfBoundsException if the index is invalid
      * @since Commons Collections 3.2
      */
-    public Object setValue(int index, Object value) {
-        Object key = insertOrder.get(index);
+    public V setValue(int index, V value) {
+        K key = insertOrder.get(index);
         return put(key, value);
     }
 
@@ -413,10 +405,10 @@
      * @throws IndexOutOfBoundsException if the index is out of range
      * @since Commons Collections 3.2
      */
-    public Object put(int index, Object key, Object value) {
-        Map m = getMap();
+    public V put(int index, K key, V value) {
+        Map<K, V> m = decorated();
         if (m.containsKey(key)) {
-            Object result = m.remove(key);
+            V result = m.remove(key);
             int pos = insertOrder.indexOf(key);
             insertOrder.remove(pos);
             if (pos < index) {
@@ -439,7 +431,7 @@
      * @return the removed value, or <code>null</code> if none existed
      * @throws IndexOutOfBoundsException if the index is invalid
      */
-    public Object remove(int index) {
+    public V remove(int index) {
         return remove(get(index));
     }
 
@@ -460,17 +452,18 @@
      * @see #keySet()
      * @return The ordered list of keys.  
      */
-    public List asList() {
+    public List<K> asList() {
         return keyList();
     }
 
     //-----------------------------------------------------------------------
-    static class ValuesView extends AbstractList {
-        private final ListOrderedMap parent;
+    static class ValuesView<V> extends AbstractList<V> {
+        private final ListOrderedMap<Object, V> parent;
 
-        ValuesView(ListOrderedMap parent) {
+        @SuppressWarnings("unchecked")
+        ValuesView(ListOrderedMap<?, V> parent) {
             super();
-            this.parent = parent;
+            this.parent = (ListOrderedMap<Object, V>) parent;
         }
 
         public int size() {
@@ -485,34 +478,35 @@
             this.parent.clear();
         }
 
-        public Iterator iterator() {
-            return new AbstractIteratorDecorator(parent.entrySet().iterator()) {
-                public Object next() {
-                    return ((Map.Entry) iterator.next()).getValue();
+        public Iterator<V> iterator() {
+            return new AbstractUntypedIteratorDecorator<Map.Entry<Object, V>, V>(parent.entrySet().iterator())
{
+                public V next() {
+                    return getIterator().next().getValue();
                 }
             };
         }
 
-        public Object get(int index) {
+        public V get(int index) {
             return this.parent.getValue(index);
         }
 
-        public Object set(int index, Object value) {
+        public V set(int index, V value) {
             return this.parent.setValue(index, value);
         }
 
-        public Object remove(int index) {
+        public V remove(int index) {
             return this.parent.remove(index);
         }
     }
 
     //-----------------------------------------------------------------------
-    static class KeySetView extends AbstractSet {
-        private final ListOrderedMap parent;
+    static class KeySetView<K> extends AbstractSet<K> {
+        private final ListOrderedMap<K, Object> parent;
 
-        KeySetView(ListOrderedMap parent) {
+        @SuppressWarnings("unchecked")
+        KeySetView(ListOrderedMap<K, ?> parent) {
             super();
-            this.parent = parent;
+            this.parent = (ListOrderedMap<K, Object>) parent;
         }
 
         public int size() {
@@ -527,30 +521,30 @@
             this.parent.clear();
         }
 
-        public Iterator iterator() {
-            return new AbstractIteratorDecorator(parent.entrySet().iterator()) {
-                public Object next() {
-                    return ((Map.Entry) super.next()).getKey();
+        public Iterator<K> iterator() {
+            return new AbstractUntypedIteratorDecorator<Map.Entry<K, Object>, K>(parent.entrySet().iterator())
{
+                public K next() {
+                    return getIterator().next().getKey();
                 }
             };
         }
     }
 
     //-----------------------------------------------------------------------    
-    static class EntrySetView extends AbstractSet {
-        private final ListOrderedMap parent;
-        private final List insertOrder;
-        private Set entrySet;
+    static class EntrySetView<K, V> extends AbstractSet<Map.Entry<K, V>>
{
+        private final ListOrderedMap<K, V> parent;
+        private final List<K> insertOrder;
+        private Set<Map.Entry<K, V>> entrySet;
 
-        public EntrySetView(ListOrderedMap parent, List insertOrder) {
+        public EntrySetView(ListOrderedMap<K, V> parent, List<K> insertOrder)
{
             super();
             this.parent = parent;
             this.insertOrder = insertOrder;
         }
 
-        private Set getEntrySet() {
+        private Set<Map.Entry<K, V>> getEntrySet() {
             if (entrySet == null) {
-                entrySet = parent.getMap().entrySet();
+                entrySet = parent.decorated().entrySet();
             }
             return entrySet;
         }
@@ -566,16 +560,17 @@
             return getEntrySet().contains(obj);
         }
 
-        public boolean containsAll(Collection coll) {
+        public boolean containsAll(Collection<?> coll) {
             return getEntrySet().containsAll(coll);
         }
 
+        @SuppressWarnings("unchecked")
         public boolean remove(Object obj) {
             if (obj instanceof Map.Entry == false) {
                 return false;
             }
             if (getEntrySet().contains(obj)) {
-                Object key = ((Map.Entry) obj).getKey();
+                Object key = ((Map.Entry<K, V>) obj).getKey();
                 parent.remove(key);
                 return true;
             }
@@ -585,14 +580,14 @@
         public void clear() {
             this.parent.clear();
         }
-        
+
         public boolean equals(Object obj) {
             if (obj == this) {
                 return true;
             }
             return getEntrySet().equals(obj);
         }
-        
+
         public int hashCode() {
             return getEntrySet().hashCode();
         }
@@ -600,69 +595,69 @@
         public String toString() {
             return getEntrySet().toString();
         }
-        
-        public Iterator iterator() {
-            return new ListOrderedIterator(parent, insertOrder);
+
+        public Iterator<Map.Entry<K, V>> iterator() {
+            return new ListOrderedIterator<K, V>(parent, insertOrder);
         }
     }
-    
+
     //-----------------------------------------------------------------------
-    static class ListOrderedIterator extends AbstractIteratorDecorator {
-        private final ListOrderedMap parent;
-        private Object last = null;
+    static class ListOrderedIterator<K, V> extends AbstractUntypedIteratorDecorator<K,
Map.Entry<K, V>> {
+        private final ListOrderedMap<K, V> parent;
+        private K last = null;
         
-        ListOrderedIterator(ListOrderedMap parent, List insertOrder) {
+        ListOrderedIterator(ListOrderedMap<K, V> parent, List<K> insertOrder)
{
             super(insertOrder.iterator());
             this.parent = parent;
         }
-        
-        public Object next() {
-            last = super.next();
-            return new ListOrderedMapEntry(parent, last);
+
+        public Map.Entry<K, V> next() {
+            last = getIterator().next();
+            return new ListOrderedMapEntry<K, V>(parent, last);
         }
 
         public void remove() {
             super.remove();
-            parent.getMap().remove(last);
+            parent.decorated().remove(last);
         }
     }
-    
+
     //-----------------------------------------------------------------------
-    static class ListOrderedMapEntry extends AbstractMapEntry {
-        private final ListOrderedMap parent;
-        
-        ListOrderedMapEntry(ListOrderedMap parent, Object key) {
+    static class ListOrderedMapEntry<K, V> extends AbstractMapEntry<K, V> {
+        private final ListOrderedMap<K, V> parent;
+
+        ListOrderedMapEntry(ListOrderedMap<K, V> parent, K key) {
             super(key, null);
             this.parent = parent;
         }
-        
-        public Object getValue() {
+
+        public V getValue() {
             return parent.get(key);
         }
 
-        public Object setValue(Object value) {
-            return parent.getMap().put(key, value);
+        public V setValue(V value) {
+            return parent.decorated().put(key, value);
         }
     }
 
     //-----------------------------------------------------------------------
-    static class ListOrderedMapIterator implements OrderedMapIterator, ResettableIterator
{
-        private final ListOrderedMap parent;
-        private ListIterator iterator;
-        private Object last = null;
+    static class ListOrderedMapIterator<K, V> implements OrderedMapIterator<K, V>,
ResettableIterator<K> {
+        private final ListOrderedMap<K, V> parent;
+        private ListIterator<K> iterator;
+        private K last = null;
         private boolean readable = false;
-        
-        ListOrderedMapIterator(ListOrderedMap parent) {
+
+        ListOrderedMapIterator(ListOrderedMap<K, V> parent) {
             super();
             this.parent = parent;
             this.iterator = parent.insertOrder.listIterator();
         }
-        
+
         public boolean hasNext() {
             return iterator.hasNext();
         }
-        
-        public Object next() {
+
+        public K next() {
             last = iterator.next();
             readable = true;
             return last;
@@ -671,13 +666,13 @@
         public boolean hasPrevious() {
             return iterator.hasPrevious();
         }
-        
-        public Object previous() {
+
+        public K previous() {
             last = iterator.previous();
             readable = true;
             return last;
         }
-        
+
         public void remove() {
             if (readable == false) {
                 throw new IllegalStateException(AbstractHashedMap.REMOVE_INVALID);
@@ -686,41 +681,40 @@
             parent.map.remove(last);
             readable = false;
         }
-        
-        public Object getKey() {
+
+        public K getKey() {
             if (readable == false) {
                 throw new IllegalStateException(AbstractHashedMap.GETKEY_INVALID);
             }
             return last;
         }
 
-        public Object getValue() {
+        public V getValue() {
             if (readable == false) {
                 throw new IllegalStateException(AbstractHashedMap.GETVALUE_INVALID);
             }
             return parent.get(last);
         }
-        
-        public Object setValue(Object value) {
+
+        public V setValue(V value) {
             if (readable == false) {
                 throw new IllegalStateException(AbstractHashedMap.SETVALUE_INVALID);
             }
             return parent.map.put(last, value);
         }
-        
+
         public void reset() {
             iterator = parent.insertOrder.listIterator();
             last = null;
             readable = false;
         }
-        
+
         public String toString() {
             if (readable == true) {
                 return "Iterator[" + getKey() + "=" + getValue() + "]";
-            } else {
-                return "Iterator[]";
             }
+            return "Iterator[]";
         }
     }
-    
+
 }



Mime
View raw message