Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 27346 invoked from network); 15 Sep 2009 05:56:36 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Sep 2009 05:56:36 -0000 Received: (qmail 25010 invoked by uid 500); 15 Sep 2009 05:56:36 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 24936 invoked by uid 500); 15 Sep 2009 05:56:36 -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 24927 invoked by uid 99); 15 Sep 2009 05:56:36 -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:56:36 +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:56:31 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 0379F23889C4; Tue, 15 Sep 2009 05:56:11 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: bayard@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090915055611.0379F23889C4@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 + extends AbstractMapDecorator + implements OrderedMap, 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 insertOrder = new ArrayList(); /** * 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 OrderedMap decorate(Map map) { + return new ListOrderedMap(map); } //----------------------------------------------------------------------- @@ -98,7 +97,7 @@ * @since Commons Collections 3.1 */ public ListOrderedMap() { - this(new HashMap()); + this(new HashMap()); } /** @@ -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 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 mapIterator() { + return new ListOrderedMapIterator(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 map) { + for (Map.Entry 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 map) { + for (Map.Entry 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 keySet() { + return new KeySetView(this); } /** @@ -273,7 +267,7 @@ * @return the unmodifiable list view over the keys * @since Commons Collections 3.2 */ - public List keyList() { + public List 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 values() { + return new ValuesView(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 valueList() { + return new ValuesView(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> entrySet() { + return new EntrySetView(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 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 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 null 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 asList() { return keyList(); } //----------------------------------------------------------------------- - static class ValuesView extends AbstractList { - private final ListOrderedMap parent; + static class ValuesView extends AbstractList { + private final ListOrderedMap parent; - ValuesView(ListOrderedMap parent) { + @SuppressWarnings("unchecked") + ValuesView(ListOrderedMap parent) { super(); - this.parent = parent; + this.parent = (ListOrderedMap) 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 iterator() { + return new AbstractUntypedIteratorDecorator, 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 extends AbstractSet { + private final ListOrderedMap parent; - KeySetView(ListOrderedMap parent) { + @SuppressWarnings("unchecked") + KeySetView(ListOrderedMap parent) { super(); - this.parent = parent; + this.parent = (ListOrderedMap) 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 iterator() { + return new AbstractUntypedIteratorDecorator, 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 extends AbstractSet> { + private final ListOrderedMap parent; + private final List insertOrder; + private Set> entrySet; - public EntrySetView(ListOrderedMap parent, List insertOrder) { + public EntrySetView(ListOrderedMap parent, List insertOrder) { super(); this.parent = parent; this.insertOrder = insertOrder; } - private Set getEntrySet() { + private Set> 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) 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> iterator() { + return new ListOrderedIterator(parent, insertOrder); } } - + //----------------------------------------------------------------------- - static class ListOrderedIterator extends AbstractIteratorDecorator { - private final ListOrderedMap parent; - private Object last = null; + static class ListOrderedIterator extends AbstractUntypedIteratorDecorator> { + private final ListOrderedMap parent; + private K last = null; - ListOrderedIterator(ListOrderedMap parent, List insertOrder) { + ListOrderedIterator(ListOrderedMap parent, List insertOrder) { super(insertOrder.iterator()); this.parent = parent; } - - public Object next() { - last = super.next(); - return new ListOrderedMapEntry(parent, last); + + public Map.Entry next() { + last = getIterator().next(); + return new ListOrderedMapEntry(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 extends AbstractMapEntry { + private final ListOrderedMap parent; + + ListOrderedMapEntry(ListOrderedMap 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 implements OrderedMapIterator, ResettableIterator { + private final ListOrderedMap parent; + private ListIterator iterator; + private K last = null; private boolean readable = false; - - ListOrderedMapIterator(ListOrderedMap parent) { + + ListOrderedMapIterator(ListOrderedMap 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[]"; } } - + }