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 1015642 2017-07-18 12:10:19Z chtompki $
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 NullPointerException if the map 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    @Override
080    public Comparator<? super K> comparator() {
081        return decorated().comparator();
082    }
083
084    @Override
085    public K firstKey() {
086        return decorated().firstKey();
087    }
088
089    @Override
090    public K lastKey() {
091        return decorated().lastKey();
092    }
093
094    @Override
095    public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
096        return decorated().subMap(fromKey, toKey);
097    }
098
099    @Override
100    public SortedMap<K, V> headMap(final K toKey) {
101        return decorated().headMap(toKey);
102    }
103
104    @Override
105    public SortedMap<K, V> tailMap(final K fromKey) {
106        return decorated().tailMap(fromKey);
107    }
108
109    @Override
110    public K previousKey(final K key) {
111        final SortedMap<K, V> headMap = headMap(key);
112        return headMap.isEmpty() ? null : headMap.lastKey();
113    }
114
115    @Override
116    public K nextKey(final K key) {
117        final Iterator<K> it = tailMap(key).keySet().iterator();
118        it.next();
119        return it.hasNext() ? it.next() : null;
120    }
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public OrderedMapIterator<K, V> mapIterator() {
127        return new SortedMapIterator<K, V>(entrySet());
128    }
129
130    /**
131     * OrderedMapIterator implementation.
132     *
133     * @param <K>  the key type
134     * @param <V>  the value type
135     */
136    protected static class SortedMapIterator<K, V> extends EntrySetToMapIteratorAdapter<K, V>
137            implements OrderedMapIterator<K, V> {
138
139        /**
140         * Create a new AbstractSortedMapDecorator.SortedMapIterator.
141         * @param entrySet  the entrySet to iterate
142         */
143        protected SortedMapIterator(final Set<Map.Entry<K, V>> entrySet) {
144            super(entrySet);
145        }
146
147        /**
148         * {@inheritDoc}
149         */
150        @Override
151        public synchronized void reset() {
152            super.reset();
153            iterator = new ListIteratorWrapper<Map.Entry<K, V>>(iterator);
154        }
155
156        /**
157         * {@inheritDoc}
158         */
159        @Override
160        public boolean hasPrevious() {
161            return ((ListIterator<Map.Entry<K, V>>) iterator).hasPrevious();
162        }
163
164        /**
165         * {@inheritDoc}
166         */
167        @Override
168        public K previous() {
169            entry = ((ListIterator<Map.Entry<K, V>>) iterator).previous();
170            return getKey();
171        }
172    }
173}