View Javadoc
1   package org.opentrafficsim.base.immutablecollections;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import nl.tudelft.simulation.language.Throw;
7   
8   /**
9    * An immutable wrapper for a HashMap.
10   * <p>
11   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13   * </p>
14   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
15   * initial version May 7, 2016 <br>
16   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
18   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
19   * @param <K> the key type of content of this Map
20   * @param <V> the value type of content of this Map
21   */
22  public class ImmutableHashMap<K, V> extends ImmutableAbstractMap<K, V>
23  {
24      /** */
25      private static final long serialVersionUID = 20160507L;
26  
27      /**
28       * @param map the map to use as the immutable map.
29       */
30      public ImmutableHashMap(final Map<K, V> map)
31      {
32          this(map, Immutable.COPY);
33      }
34  
35      /**
36       * @param map the map to use as the immutable map.
37       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
38       */
39      public ImmutableHashMap(final Map<K, V> map, final Immutable copyOrWrap)
40      {
41          super(copyOrWrap == Immutable.COPY ? new HashMap<K, V>(map) : map, copyOrWrap == Immutable.COPY);
42          Throw.whenNull(copyOrWrap, "the copyOrWrap argument should be Immutable.COPY or Immutable.WRAP");
43      }
44  
45      /**
46       * @param map the map to use as the immutable map.
47       * @param copy boolean; indicates whether the immutable is a copy or a wrap
48       */
49      protected ImmutableHashMap(final Map<K, V> map, final boolean copy)
50      {
51          super(map, copy);
52      }
53  
54      /**
55       * @param immutableMap the map to use as the immutable map.
56       */
57      public ImmutableHashMap(final ImmutableHashMap<K, V> immutableMap)
58      {
59          this(immutableMap, Immutable.COPY);
60      }
61  
62      /**
63       * @param immutableMap the map to use as the immutable map.
64       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
65       */
66      public ImmutableHashMap(final ImmutableHashMap<K, V> immutableMap, final Immutable copyOrWrap)
67      {
68          this(((ImmutableAbstractMap<K, V>) immutableMap).getMap(), copyOrWrap);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      protected final HashMap<K, V> getMap()
74      {
75          return (HashMap<K, V>) super.getMap();
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public final Map<K, V> toMap()
81      {
82          return new HashMap<K, V>(getMap());
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public final ImmutableSet<K> keySet()
88      {
89          return new ImmutableHashSet<K>(getMap().keySet());
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      @SuppressWarnings("checkstyle:designforextension")
95      public String toString()
96      {
97          Map<K, V> map = getMap();
98          if (null == map)
99          {
100             return "ImmutableHashMap []";
101         }
102         return "ImmutableHashMap [" + map.toString() + "]";
103     }
104 
105 }