Author: mbenson Date: Mon Mar 9 22:13:34 2009 New Revision: 751871 URL: http://svn.apache.org/viewvc?rev=751871&view=rev Log: Add OrderedMap to our SortedMap implementations
Added: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IterableSortedMap.java (with props) Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java Added: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IterableSortedMap.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IterableSortedMap.java?rev=751871&view=auto ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IterableSortedMap.java (added) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IterableSortedMap.java Mon Mar 9 22:13:34 2009 @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections; + +import java.util.SortedMap; + +/** + * {...@link SortedMap} + {...@link OrderedMap}. + * + * @param <K> the type of the keys in the map + * @param <V> the type of the values in the map + * @since Commons Collections 5 + * @TODO fix version + * @version $Revision$ $Date$ + * + * @author Matt Benson + */ +public interface IterableSortedMap<K, V> extends SortedMap<K, V>, OrderedMap<K, V> { +} Propchange: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IterableSortedMap.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/IterableSortedMap.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java?rev=751871&r1=751870&r2=751871&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java Mon Mar 9 22:13:34 2009 @@ -269,6 +269,14 @@ protected DualTreeBidiMap<K, V> decorated() { return (DualTreeBidiMap<K, V>) super.decorated(); } + + public K previousKey(K key) { + return decorated().previousKey(key); + }; + + public K nextKey(K key) { + return decorated().nextKey(key); + }; } //----------------------------------------------------------------------- Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java?rev=751871&r1=751870&r2=751871&view=diff ============================================================================== --- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java (original) +++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java Mon Mar 9 22:13:34 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. @@ -39,7 +47,7 @@ * @author Stephen Colebourne */ public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecorator<K, V> implements - SortedMap<K, V> { + IterableSortedMap<K, V> { /** * Constructor only used in deserialization, do not use otherwise. @@ -93,4 +101,63 @@ return decorated().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(); + } + } }