View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.Comparator;
4   import java.util.Map;
5   import java.util.Map.Entry;
6   import java.util.NavigableMap;
7   import java.util.NavigableSet;
8   import java.util.TreeMap;
9   import java.util.TreeSet;
10  
11  /**
12   * An immutable wrapper for a TreeMap.
13   * <p>
14   * Copyright (c) 2016-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
16   * distributed under a three-clause BSD-style license, which can be found at
17   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
18   * </p>
19   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
21   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   * @param <K> the key type of content of this Map
23   * @param <V> the value type of content of this Map
24   */
25  public class ImmutableTreeMap<K, V> extends ImmutableAbstractMap<K, V> implements ImmutableNavigableMap<K, V>
26  {
27      /** */
28      private static final long serialVersionUID = 20160507L;
29  
30      /** the cached keySet. */
31      private ImmutableSortedSet<K> cachedKeySet = null;
32  
33      /** the cached entrySet. */
34      private ImmutableSortedSet<ImmutableEntry<K, V>> cachedEntrySet = null;
35  
36      /**
37       * @param sortedMap Map&lt;K,V&gt;; the map to use for the immutable map.
38       */
39      public ImmutableTreeMap(final Map<K, V> sortedMap)
40      {
41          super(new TreeMap<K, V>(sortedMap), Immutable.COPY);
42      }
43  
44      /**
45       * @param map NavigableMap&lt;K,V&gt;; the map to use for the immutable map.
46       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
47       */
48      public ImmutableTreeMap(final NavigableMap<K, V> map, final Immutable copyOrWrap)
49      {
50          super(copyOrWrap == Immutable.COPY ? new TreeMap<K, V>(map) : map, copyOrWrap);
51      }
52  
53      /**
54       * @param immutableMap ImmutableAbstractMap&lt;K,V&gt;; the map to use for the immutable map.
55       */
56      public ImmutableTreeMap(final ImmutableAbstractMap<K, V> immutableMap)
57      {
58          super(new TreeMap<K, V>(immutableMap.getMap()), Immutable.COPY);
59      }
60  
61      /**
62       * @param immutableTreeMap ImmutableTreeMap&lt;K,V&gt;; the map to use for the immutable map.
63       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
64       */
65      public ImmutableTreeMap(final ImmutableTreeMap<K, V> immutableTreeMap, final Immutable copyOrWrap)
66      {
67          super(copyOrWrap == Immutable.COPY ? new TreeMap<K, V>(immutableTreeMap.getMap()) : immutableTreeMap.getMap(),
68                  copyOrWrap);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      protected final NavigableMap<K, V> getMap()
74      {
75          return (NavigableMap<K, V>) super.getMap();
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final NavigableMap<K, V> toMap()
81      {
82          return new TreeMap<K, V>(super.getMap());
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public final ImmutableSortedSet<K> keySet()
88      {
89          if (this.cachedKeySet == null)
90          {
91              NavigableSet<K> immutableKeySet = new TreeSet<>(getMap().comparator());
92              immutableKeySet.addAll(getMap().keySet());
93              this.cachedKeySet = new ImmutableTreeSet<>(immutableKeySet, Immutable.WRAP);
94          }
95          return this.cachedKeySet;
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public ImmutableSortedSet<ImmutableEntry<K, V>> entrySet()
101     {
102         if (this.cachedEntrySet == null)
103         {
104             NavigableSet<ImmutableEntry<K, V>> immutableEntrySet = new TreeSet<>(new Comparator<ImmutableEntry<K, V>>()
105             {
106                 /** {@inheritDoc} */
107                 @SuppressWarnings("unchecked")
108                 @Override
109                 public int compare(ImmutableEntry<K, V> o1, ImmutableEntry<K, V> o2)
110                 {
111                     return ((Comparable<K>) o1.getKey()).compareTo(o2.getKey());
112                 }
113 
114             });
115             for (Entry<K, V> entry : getMap().entrySet())
116             {
117                 immutableEntrySet.add(new ImmutableEntry<>(entry));
118             }
119             this.cachedEntrySet = new ImmutableTreeSet<>(immutableEntrySet, Immutable.WRAP);
120         }
121         return this.cachedEntrySet;
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public ImmutableSortedSet<V> values()
127     {
128         if (this.cachedValues == null)
129         {
130             NavigableSet<V> immutableValues = new TreeSet<>(getMap().values());
131             this.cachedValues = new ImmutableTreeSet<>(immutableValues, Immutable.WRAP);
132         }
133         return (ImmutableNavigableSet<V>) this.cachedValues;
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public final Comparator<? super K> comparator()
139     {
140         return getMap().comparator();
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public final ImmutableSortedMap<K, V> subMap(final K fromKey, final K toKey)
146     {
147         return new ImmutableTreeMap<K, V>(getMap().subMap(fromKey, toKey));
148     }
149 
150     /** {@inheritDoc} */
151     @Override
152     public final ImmutableSortedMap<K, V> headMap(final K toKey)
153     {
154         return new ImmutableTreeMap<K, V>(getMap().headMap(toKey));
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     public final ImmutableSortedMap<K, V> tailMap(final K fromKey)
160     {
161         return new ImmutableTreeMap<K, V>(getMap().tailMap(fromKey));
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public final K firstKey()
167     {
168         return getMap().firstKey();
169     }
170 
171     /** {@inheritDoc} */
172     @Override
173     public final K lastKey()
174     {
175         return getMap().lastKey();
176     }
177 
178     /** {@inheritDoc} */
179     @Override
180     public final K lowerKey(final K key)
181     {
182         return getMap().lowerKey(key);
183     }
184 
185     /** {@inheritDoc} */
186     @Override
187     public final K floorKey(final K key)
188     {
189         return getMap().floorKey(key);
190     }
191 
192     /** {@inheritDoc} */
193     @Override
194     public final K ceilingKey(final K key)
195     {
196         return getMap().ceilingKey(key);
197     }
198 
199     /** {@inheritDoc} */
200     @Override
201     public final K higherKey(final K key)
202     {
203         return getMap().higherKey(key);
204     }
205 
206     /** {@inheritDoc} */
207     @Override
208     public final ImmutableNavigableMap<K, V> descendingMap()
209     {
210         return new ImmutableTreeMap<K, V>(getMap().descendingMap());
211     }
212 
213     /** {@inheritDoc} */
214     @Override
215     public final ImmutableNavigableMap<K, V> subMap(final K fromKey, final boolean fromInclusive, final K toKey,
216             final boolean toInclusive)
217     {
218         return new ImmutableTreeMap<K, V>(getMap().subMap(fromKey, fromInclusive, toKey, toInclusive));
219     }
220 
221     /** {@inheritDoc} */
222     @Override
223     public final ImmutableNavigableMap<K, V> headMap(final K toKey, final boolean inclusive)
224     {
225         return new ImmutableTreeMap<K, V>(getMap().headMap(toKey, inclusive));
226     }
227 
228     /** {@inheritDoc} */
229     @Override
230     public final ImmutableNavigableMap<K, V> tailMap(final K fromKey, final boolean inclusive)
231     {
232         return new ImmutableTreeMap<K, V>(getMap().tailMap(fromKey, inclusive));
233     }
234 
235     /** {@inheritDoc} */
236     @Override
237     public final String toString()
238     {
239         NavigableMap<K, V> map = getMap();
240         if (null == map)
241         {
242             return "ImmutableTreeMap []";
243         }
244         return "ImmutableTreeMap [" + map.toString() + "]";
245     }
246 
247 }