View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.map;
18  
19  import java.util.Comparator;
20  import java.util.Iterator;
21  import java.util.ListIterator;
22  import java.util.Map;
23  import java.util.Set;
24  import java.util.SortedMap;
25  
26  import org.apache.commons.collections4.IterableSortedMap;
27  import org.apache.commons.collections4.OrderedMapIterator;
28  import org.apache.commons.collections4.iterators.ListIteratorWrapper;
29  
30  /**
31   * Provides a base decorator that enables additional functionality to be added
32   * to a Map via decoration.
33   * <p>
34   * Methods are forwarded directly to the decorated map.
35   * </p>
36   * <p>
37   * This implementation does not perform any special processing with the map views.
38   * Instead it simply returns the set/collection from the wrapped map. This may be
39   * undesirable, for example if you are trying to write a validating implementation
40   * it would provide a loophole around the validation.
41   * But, you might want that loophole, so this class is kept simple.
42   * </p>
43   *
44   * @param <K> the type of the keys in the map
45   * @param <V> the type of the values in the map
46   * @since 3.0
47   */
48  public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecorator<K, V> implements
49          IterableSortedMap<K, V> {
50  
51      /**
52       * OrderedMapIterator implementation.
53       *
54       * @param <K>  the key type
55       * @param <V>  the value type
56       */
57      protected static class SortedMapIterator<K, V> extends EntrySetToMapIteratorAdapter<K, V>
58              implements OrderedMapIterator<K, V> {
59  
60          /**
61           * Create a new AbstractSortedMapDecorator.SortedMapIterator.
62           * @param entrySet  the entrySet to iterate
63           */
64          protected SortedMapIterator(final Set<Map.Entry<K, V>> entrySet) {
65              super(entrySet);
66          }
67  
68          /**
69           * {@inheritDoc}
70           */
71          @Override
72          public boolean hasPrevious() {
73              return ((ListIterator<Map.Entry<K, V>>) iterator).hasPrevious();
74          }
75  
76          /**
77           * {@inheritDoc}
78           */
79          @Override
80          public K previous() {
81              entry = ((ListIterator<Map.Entry<K, V>>) iterator).previous();
82              return getKey();
83          }
84  
85          /**
86           * {@inheritDoc}
87           */
88          @Override
89          public synchronized void reset() {
90              super.reset();
91              iterator = new ListIteratorWrapper<>(iterator);
92          }
93      }
94  
95      /**
96       * Constructor only used in deserialization, do not use otherwise.
97       * @since 3.1
98       */
99      protected AbstractSortedMapDecorator() {
100     }
101 
102     /**
103      * Constructor that wraps (not copies).
104      *
105      * @param map  the map to decorate, must not be null
106      * @throws NullPointerException if the map is null
107      */
108     public AbstractSortedMapDecorator(final SortedMap<K, V> map) {
109         super(map);
110     }
111 
112     @Override
113     public Comparator<? super K> comparator() {
114         return decorated().comparator();
115     }
116 
117     /**
118      * Gets the map being decorated.
119      *
120      * @return the decorated map
121      */
122     @Override
123     protected SortedMap<K, V> decorated() {
124         return (SortedMap<K, V>) super.decorated();
125     }
126 
127     @Override
128     public K firstKey() {
129         return decorated().firstKey();
130     }
131 
132     @Override
133     public SortedMap<K, V> headMap(final K toKey) {
134         return decorated().headMap(toKey);
135     }
136 
137     @Override
138     public K lastKey() {
139         return decorated().lastKey();
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public OrderedMapIterator<K, V> mapIterator() {
147         return new SortedMapIterator<>(entrySet());
148     }
149 
150     @Override
151     public K nextKey(final K key) {
152         final Iterator<K> it = tailMap(key).keySet().iterator();
153         it.next();
154         return it.hasNext() ? it.next() : null;
155     }
156 
157     @Override
158     public K previousKey(final K key) {
159         final SortedMap<K, V> headMap = headMap(key);
160         return headMap.isEmpty() ? null : headMap.lastKey();
161     }
162 
163     @Override
164     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
165         return decorated().subMap(fromKey, toKey);
166     }
167 
168     @Override
169     public SortedMap<K, V> tailMap(final K fromKey) {
170         return decorated().tailMap(fromKey);
171     }
172 }