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.collections.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.collections.IterableSortedMap;
27  import org.apache.commons.collections.OrderedMapIterator;
28  import org.apache.commons.collections.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   * This implementation does not perform any special processing with the map views.
37   * Instead it simply returns the set/collection from the wrapped map. This may be
38   * undesirable, for example if you are trying to write a validating implementation
39   * it would provide a loophole around the validation.
40   * But, you might want that loophole, so this class is kept simple.
41   *
42   * @param <K> the type of the keys in the map
43   * @param <V> the type of the values in the map
44   * @since 3.0
45   * @version $Id: AbstractSortedMapDecorator.java 1436463 2013-01-21 16:35:01Z tn $
46   */
47  public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecorator<K, V> implements
48          IterableSortedMap<K, V> {
49  
50      /**
51       * Constructor only used in deserialization, do not use otherwise.
52       * @since 3.1
53       */
54      protected AbstractSortedMapDecorator() {
55          super();
56      }
57  
58      /**
59       * Constructor that wraps (not copies).
60       *
61       * @param map  the map to decorate, must not be null
62       * @throws IllegalArgumentException if the collection is null
63       */
64      public AbstractSortedMapDecorator(final SortedMap<K, V> map) {
65          super(map);
66      }
67  
68      /**
69       * Gets the map being decorated.
70       * 
71       * @return the decorated map
72       */
73      @Override
74      protected SortedMap<K, V> decorated() {
75          return (SortedMap<K, V>) super.decorated();
76      }
77  
78      //-----------------------------------------------------------------------
79      public Comparator<? super K> comparator() {
80          return decorated().comparator();
81      }
82  
83      public K firstKey() {
84          return decorated().firstKey();
85      }
86  
87      public K lastKey() {
88          return decorated().lastKey();
89      }
90  
91      public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
92          return decorated().subMap(fromKey, toKey);
93      }
94  
95      public SortedMap<K, V> headMap(final K toKey) {
96          return decorated().headMap(toKey);
97      }
98  
99      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 }