View Javadoc
1   package org.opentrafficsim.core.immutablecollections;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   
7   /**
8    * An immutable wrapper for a HashMap.
9    * <p>
10   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
12   * </p>
13   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
14   * initial version May 7, 2016 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
18   * @param <K> the key type of content of this Map
19   * @param <V> the value type of content of this Map
20   */
21  public class ImmutableHashMap<K, V> extends ImmutableAbstractMap<K, V>
22  {
23      /** */
24      private static final long serialVersionUID = 20160507L;
25  
26      /**
27       * @param map the set to use as the immutable set.
28       */
29      public ImmutableHashMap(final Map<? extends K, ? extends V> map)
30      {
31          super(new HashMap<K, V>(map));
32      }
33  
34      /**
35       * @param immutableMap the set to use as the immutable set.
36       */
37      public ImmutableHashMap(final ImmutableHashMap<? extends K, ? extends V> immutableMap)
38      {
39          this(immutableMap.toMap());
40      }
41  
42      /** {@inheritDoc} */
43      @Override
44      protected final HashMap<K, V> getMap()
45      {
46          return (HashMap<K, V>) super.getMap();
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      public final Map<K, V> toMap()
52      {
53          return new HashMap<K, V>(getMap());
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public final ImmutableSet<K> keySet()
59      {
60          return new ImmutableHashSet<K>(getMap().keySet());
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public final String toString()
66      {
67          Map<K, V> map = getMap();
68          if (null == map)
69          {
70              return "ImmutableHashMap []";
71          }
72          return "ImmutableHashMap [" + map.toString() + "]";
73      }
74  
75  }