001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.map;
018
019import java.util.Comparator;
020import java.util.Iterator;
021import java.util.ListIterator;
022import java.util.Map;
023import java.util.Set;
024import java.util.SortedMap;
025
026import org.apache.commons.collections4.IterableSortedMap;
027import org.apache.commons.collections4.OrderedMapIterator;
028import org.apache.commons.collections4.iterators.ListIteratorWrapper;
029
030/**
031 * Provides a base decorator that enables additional functionality to be added
032 * to a Map via decoration.
033 * <p>
034 * Methods are forwarded directly to the decorated map.
035 * <p>
036 * This implementation does not perform any special processing with the map views.
037 * Instead it simply returns the set/collection from the wrapped map. This may be
038 * undesirable, for example if you are trying to write a validating implementation
039 * it would provide a loophole around the validation.
040 * But, you might want that loophole, so this class is kept simple.
041 *
042 * @param <K> the type of the keys in the map
043 * @param <V> the type of the values in the map
044 * @since 3.0
045 * @version $Id: AbstractSortedMapDecorator.html 887892 2013-11-24 13:43:45Z tn $
046 */
047public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecorator<K, V> implements
048        IterableSortedMap<K, V> {
049
050    /**
051     * Constructor only used in deserialization, do not use otherwise.
052     * @since 3.1
053     */
054    protected AbstractSortedMapDecorator() {
055        super();
056    }
057
058    /**
059     * Constructor that wraps (not copies).
060     *
061     * @param map  the map to decorate, must not be null
062     * @throws IllegalArgumentException if the collection is null
063     */
064    public AbstractSortedMapDecorator(final SortedMap<K, V> map) {
065        super(map);
066    }
067
068    /**
069     * Gets the map being decorated.
070     *
071     * @return the decorated map
072     */
073    @Override
074    protected SortedMap<K, V> decorated() {
075        return (SortedMap<K, V>) super.decorated();
076    }
077
078    //-----------------------------------------------------------------------
079    public Comparator<? super K> comparator() {
080        return decorated().comparator();
081    }
082
083    public K firstKey() {
084        return decorated().firstKey();
085    }
086
087    public K lastKey() {
088        return decorated().lastKey();
089    }
090
091    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
092        return decorated().subMap(fromKey, toKey);
093    }
094
095    public SortedMap<K, V> headMap(final K toKey) {
096        return decorated().headMap(toKey);
097    }
098
099    public SortedMap<K, V> tailMap(final K fromKey) {
100        return decorated().tailMap(fromKey);
101    }
102
103    public K previousKey(final K key) {
104        final SortedMap<K, V> headMap = headMap(key);
105        return headMap.isEmpty() ? null : headMap.lastKey();
106    }
107
108    public K nextKey(final K key) {
109        final Iterator<K> it = tailMap(key).keySet().iterator();
110        it.next();
111        return it.hasNext() ? it.next() : null;
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    public OrderedMapIterator<K, V> mapIterator() {
119        return new SortedMapIterator<K, V>(entrySet());
120    }
121
122    /**
123     * OrderedMapIterator implementation.
124     *
125     * @param <K>  the key type
126     * @param <V>  the value type
127     */
128    protected static class SortedMapIterator<K, V> extends EntrySetToMapIteratorAdapter<K, V>
129            implements OrderedMapIterator<K, V> {
130
131        /**
132         * Create a new AbstractSortedMapDecorator.SortedMapIterator.
133         * @param entrySet  the entrySet to iterate
134         */
135        protected SortedMapIterator(final Set<Map.Entry<K, V>> entrySet) {
136            super(entrySet);
137        }
138
139        /**
140         * {@inheritDoc}
141         */
142        @Override
143        public synchronized void reset() {
144            super.reset();
145            iterator = new ListIteratorWrapper<Map.Entry<K, V>>(iterator);
146        }
147
148        /**
149         * {@inheritDoc}
150         */
151        public boolean hasPrevious() {
152            return ((ListIterator<Map.Entry<K, V>>) iterator).hasPrevious();
153        }
154
155        /**
156         * {@inheritDoc}
157         */
158        public K previous() {
159            entry = ((ListIterator<Map.Entry<K, V>>) iterator).previous();
160            return getKey();
161        }
162    }
163}