Add OrderedMap to our SortedMap implementations git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/branches/collections_jdk5_branch@751871 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-collections/commit/b29b93af Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/b29b93af Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/b29b93af Branch: refs/heads/collections_jdk5_branch Commit: b29b93afa7393d411e2ca094fb82b32ed81abba1 Parents: 68e12b9 Author: Matthew Jason Benson <mben...@apache.org> Authored: Mon Mar 9 22:13:34 2009 +0000 Committer: Matthew Jason Benson <mben...@apache.org> Committed: Mon Mar 9 22:13:34 2009 +0000 ---------------------------------------------------------------------- .../commons/collections/IterableSortedMap.java | 33 ++++++++++ .../collections/bidimap/DualTreeBidiMap.java | 8 +++ .../map/AbstractSortedMapDecorator.java | 69 +++++++++++++++++++- 3 files changed, 109 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-collections/blob/b29b93af/src/java/org/apache/commons/collections/IterableSortedMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/IterableSortedMap.java b/src/java/org/apache/commons/collections/IterableSortedMap.java new file mode 100644 index 0000000..b987dba --- /dev/null +++ b/src/java/org/apache/commons/collections/IterableSortedMap.java @@ -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> { +} http://git-wip-us.apache.org/repos/asf/commons-collections/blob/b29b93af/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java b/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java index 5ced759..b8fb0d7 100644 --- a/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java +++ b/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java @@ -269,6 +269,14 @@ public class DualTreeBidiMap<K, V> extends AbstractDualBidiMap<K, V> implements 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); + }; } //----------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-collections/blob/b29b93af/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java b/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java index 7c9a35b..d47411b 100644 --- a/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java +++ b/src/java/org/apache/commons/collections/map/AbstractSortedMapDecorator.java @@ -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 @@ import java.util.SortedMap; * @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 @@ public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecora 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(); + } + } }