View Javadoc
1   package org.opentrafficsim.core.immutablecollections;
2   
3   import java.util.Comparator;
4   import java.util.HashSet;
5   import java.util.NavigableMap;
6   import java.util.SortedMap;
7   import java.util.SortedSet;
8   import java.util.TreeMap;
9   
10  /**
11   * An immutable wrapper for a TreeMap.
12   * <p>
13   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * </p>
16   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
17   * initial version May 7, 2016 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
21   * @param <K> the key type of content of this Map
22   * @param <V> the value type of content of this Map
23   */
24  public class ImmutableTreeMap<K, V> extends ImmutableAbstractMap<K, V> implements ImmutableNavigableMap<K, V>
25  {
26      /** */
27      private static final long serialVersionUID = 20160507L;
28  
29      /**
30       * @param sortedMap the map to use as the immutable map.
31       */
32      public ImmutableTreeMap(final SortedMap<K, V> sortedMap)
33      {
34          super(new TreeMap<K, V>(sortedMap));
35      }
36  
37      /**
38       * @param sortedMap the map to use as the immutable map.
39       */
40      public ImmutableTreeMap(final ImmutableSortedMap<K, V> sortedMap)
41      {
42          this(sortedMap.toMap());
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      protected final NavigableMap<K, V> getMap()
48      {
49          return (NavigableMap<K, V>) super.getMap();
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public final NavigableMap<K, V> toMap()
55      {
56          return new TreeMap<K, V>(super.getMap());
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public final ImmutableSortedSet<K> keySet()
62      {
63          return new ImmutableTreeSet<K>((SortedSet<K>) getMap().keySet());
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public final Comparator<? super K> comparator()
69      {
70          return getMap().comparator();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public final ImmutableSortedMap<K, V> subMap(final K fromKey, final K toKey)
76      {
77          return new ImmutableTreeMap<K, V>(getMap().subMap(fromKey, toKey));
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public final ImmutableSortedMap<K, V> headMap(final K toKey)
83      {
84          return new ImmutableTreeMap<K, V>(getMap().headMap(toKey));
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public final ImmutableSortedMap<K, V> tailMap(final K fromKey)
90      {
91          return new ImmutableTreeMap<K, V>(getMap().tailMap(fromKey));
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public final K firstKey()
97      {
98          return getMap().firstKey();
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public final K lastKey()
104     {
105         return getMap().lastKey();
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public final K lowerKey(final K key)
111     {
112         return getMap().lowerKey(key);
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public final K floorKey(final K key)
118     {
119         return getMap().floorKey(key);
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public final K ceilingKey(final K key)
125     {
126         return getMap().ceilingKey(key);
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public final K higherKey(final K key)
132     {
133         return getMap().higherKey(key);
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public final ImmutableNavigableMap<K, V> descendingMap()
139     {
140         return new ImmutableTreeMap<K, V>(getMap().descendingMap());
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public final ImmutableNavigableMap<K, V> subMap(final K fromKey, final boolean fromInclusive, final K toKey,
146             final boolean toInclusive)
147     {
148         return new ImmutableTreeMap<K, V>(getMap().subMap(fromKey, fromInclusive, toKey, toInclusive));
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final ImmutableNavigableMap<K, V> headMap(final K toKey, final boolean inclusive)
154     {
155         return new ImmutableTreeMap<K, V>(getMap().headMap(toKey, inclusive));
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     public final ImmutableNavigableMap<K, V> tailMap(final K fromKey, final boolean inclusive)
161     {
162         return new ImmutableTreeMap<K, V>(getMap().tailMap(fromKey, inclusive));
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public final String toString()
168     {
169         NavigableMap<K, V> map = getMap();
170         if (null == map)
171         {
172             return "ImmutableTreeMap []";
173         }
174         return "ImmutableTreeMap [" + map.toString() + "]";
175     }
176 
177 }