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 */
046public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecorator<K, V> implements
047        IterableSortedMap<K, V> {
048
049    /**
050     * Constructor only used in deserialization, do not use otherwise.
051     * @since 3.1
052     */
053    protected AbstractSortedMapDecorator() {
054        super();
055    }
056
057    /**
058     * Constructor that wraps (not copies).
059     *
060     * @param map  the map to decorate, must not be null
061     * @throws NullPointerException if the map is null
062     */
063    public AbstractSortedMapDecorator(final SortedMap<K, V> map) {
064        super(map);
065    }
066
067    /**
068     * Gets the map being decorated.
069     *
070     * @return the decorated map
071     */
072    @Override
073    protected SortedMap<K, V> decorated() {
074        return (SortedMap<K, V>) super.decorated();
075    }
076
077    //-----------------------------------------------------------------------
078    @Override
079    public Comparator<? super K> comparator() {
080        return decorated().comparator();
081    }
082
083    @Override
084    public K firstKey() {
085        return decorated().firstKey();
086    }
087
088    @Override
089    public K lastKey() {
090        return decorated().lastKey();
091    }
092
093    @Override
094    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
095        return decorated().subMap(fromKey, toKey);
096    }
097
098    @Override
099    public SortedMap<K, V> headMap(final K toKey) {
100        return decorated().headMap(toKey);
101    }
102
103    @Override
104    public SortedMap<K, V> tailMap(final K fromKey) {
105        return decorated().tailMap(fromKey);
106    }
107
108    @Override
109    public K previousKey(final K key) {
110        final SortedMap<K, V> headMap = headMap(key);
111        return headMap.isEmpty() ? null : headMap.lastKey();
112    }
113
114    @Override
115    public K nextKey(final K key) {
116        final Iterator<K> it = tailMap(key).keySet().iterator();
117        it.next();
118        return it.hasNext() ? it.next() : null;
119    }
120
121    /**
122     * {@inheritDoc}
123     */
124    @Override
125    public OrderedMapIterator<K, V> mapIterator() {
126        return new SortedMapIterator<>(entrySet());
127    }
128
129    /**
130     * OrderedMapIterator implementation.
131     *
132     * @param <K>  the key type
133     * @param <V>  the value type
134     */
135    protected static class SortedMapIterator<K, V> extends EntrySetToMapIteratorAdapter<K, V>
136            implements OrderedMapIterator<K, V> {
137
138        /**
139         * Create a new AbstractSortedMapDecorator.SortedMapIterator.
140         * @param entrySet  the entrySet to iterate
141         */
142        protected SortedMapIterator(final Set<Map.Entry<K, V>> entrySet) {
143            super(entrySet);
144        }
145
146        /**
147         * {@inheritDoc}
148         */
149        @Override
150        public synchronized void reset() {
151            super.reset();
152            iterator = new ListIteratorWrapper<>(iterator);
153        }
154
155        /**
156         * {@inheritDoc}
157         */
158        @Override
159        public boolean hasPrevious() {
160            return ((ListIterator<Map.Entry<K, V>>) iterator).hasPrevious();
161        }
162
163        /**
164         * {@inheritDoc}
165         */
166        @Override
167        public K previous() {
168            entry = ((ListIterator<Map.Entry<K, V>>) iterator).previous();
169            return getKey();
170        }
171    }
172}