You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/09/15 07:55:57 UTC

svn commit: r815073 - /commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java

Author: bayard
Date: Tue Sep 15 05:55:57 2009
New Revision: 815073

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

    ------------------------------------------------------------------------
    r751871 | mbenson | 2009-03-09 15:13:34 -0700 (Mon, 09 Mar 2009) | 1 line
    
    Add OrderedMap to our SortedMap implementations
    ------------------------------------------------------------------------
    r471189 | scolebourne | 2006-11-04 05:57:57 -0800 (Sat, 04 Nov 2006) | 1 line
    
    Remove getMap(), getOrderedMap() and getSortedMap() - use decorated()
    ------------------------------------------------------------------------
    r471180 | scolebourne | 2006-11-04 05:27:44 -0800 (Sat, 04 Nov 2006) | 1 line
    
    Abstract*Decorator - Generify and use covariant return types
    ------------------------------------------------------------------------

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

Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java?rev=815073&r1=815072&r2=815073&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java Tue Sep 15 05:55:57 2009
@@ -17,8 +17,16 @@
 package org.apache.commons.collections.map;
 
 import java.util.Comparator;
+import java.util.Iterator;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.Set;
 import java.util.SortedMap;
 
+import org.apache.commons.collections.IterableSortedMap;
+import org.apache.commons.collections.OrderedMapIterator;
+import org.apache.commons.collections.iterators.ListIteratorWrapper;
+
 /** 
  * Provides a base decorator that enables additional functionality to be added
  * to a Map via decoration.
@@ -31,13 +39,15 @@
  * it would provide a loophole around the validation.
  * But, you might want that loophole, so this class is kept simple.
  *
+ * @param <K> the type of the keys in the map
+ * @param <V> the type of the values in the map
  * @since Commons Collections 3.0
  * @version $Revision$ $Date$
  *
  * @author Stephen Colebourne
  */
-public abstract class AbstractSortedMapDecorator
-        extends AbstractMapDecorator implements SortedMap {
+public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecorator<K, V> implements
+        IterableSortedMap<K, V> {
 
     /**
      * Constructor only used in deserialization, do not use otherwise.
@@ -53,7 +63,7 @@
      * @param map  the map to decorate, must not be null
      * @throws IllegalArgumentException if the collection is null
      */
-    public AbstractSortedMapDecorator(SortedMap map) {
+    public AbstractSortedMapDecorator(SortedMap<K, V> map) {
         super(map);
     }
 
@@ -62,33 +72,92 @@
      * 
      * @return the decorated map
      */
-    protected SortedMap getSortedMap() {
-        return (SortedMap) map;
+    protected SortedMap<K, V> decorated() {
+        return (SortedMap<K, V>) super.decorated();
     }
 
     //-----------------------------------------------------------------------
-    public Comparator comparator() {
-        return getSortedMap().comparator();
+    public Comparator<? super K> comparator() {
+        return decorated().comparator();
+    }
+
+    public K firstKey() {
+        return decorated().firstKey();
     }
 
-    public Object firstKey() {
-        return getSortedMap().firstKey();
+    public K lastKey() {
+        return decorated().lastKey();
     }
 
-    public SortedMap headMap(Object toKey) {
-        return getSortedMap().headMap(toKey);
+    public SortedMap<K, V> subMap(K fromKey, K toKey) {
+        return decorated().subMap(fromKey, toKey);
     }
 
-    public Object lastKey() {
-        return getSortedMap().lastKey();
+    public SortedMap<K, V> headMap(K toKey) {
+        return decorated().headMap(toKey);
     }
 
-    public SortedMap subMap(Object fromKey, Object toKey) {
-        return getSortedMap().subMap(fromKey, toKey);
+    public SortedMap<K, V> tailMap(K fromKey) {
+        return decorated().tailMap(fromKey);
     }
 
-    public SortedMap tailMap(Object fromKey) {
-        return getSortedMap().tailMap(fromKey);
+    public K previousKey(K key) {
+        SortedMap<K, V> headMap = headMap(key);
+        return headMap.isEmpty() ? null : headMap.lastKey();
+    };
+
+    public K nextKey(K key) {
+        Iterator<K> it = tailMap(key).keySet().iterator();
+        it.next();
+        return it.hasNext() ? it.next() : null;
+    };
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public OrderedMapIterator<K, V> mapIterator() {
+        return new SortedMapIterator<K, V>(entrySet());
     }
 
+    /**
+     * OrderedMapIterator implementation.
+     *
+     * @param <K>
+     * @param <V>
+     */
+    protected static class SortedMapIterator<K, V> extends EntrySetToMapIteratorAdapter<K, V>
+            implements OrderedMapIterator<K, V> {
+
+        /**
+         * Create a new AbstractSortedMapDecorator.SortedMapIterator.
+         */
+        protected SortedMapIterator(Set<Map.Entry<K, V>> entrySet) {
+            super(entrySet);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public synchronized void reset() {
+            super.reset();
+            iterator = new ListIteratorWrapper<Map.Entry<K, V>>(iterator);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public boolean hasPrevious() {
+            return ((ListIterator<Map.Entry<K, V>>) iterator).hasPrevious();
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public K previous() {
+            entry = ((ListIterator<Map.Entry<K, V>>) iterator).previous();
+            return getKey();
+        }
+    }
 }