View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Map.Entry;
7   import java.util.Set;
8   
9   /**
10   * An immutable wrapper for a HashMap.
11   * <p>
12   * Copyright (c) 2016-2019 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
13   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
14   * distributed under a three-clause BSD-style license, which can be found at
15   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
16   * </p>
17   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
19   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20   * @param <K> the key type of content of this Map
21   * @param <V> the value type of content of this Map
22   */
23  public class ImmutableHashMap<K, V> extends ImmutableAbstractMap<K, V>
24  {
25      /** */
26      private static final long serialVersionUID = 20160507L;
27  
28      /** the cached keySet. */
29      private ImmutableSet<K> cachedKeySet = null;
30  
31      /** the cached entrySet. */
32      private ImmutableSet<ImmutableEntry<K, V>> cachedEntrySet = null;
33  
34      /**
35       * @param map Map&lt;K,V&gt;; the map to use for the immutable map.
36       */
37      public ImmutableHashMap(final Map<K, V> map)
38      {
39          super(new HashMap<K, V>(map), Immutable.COPY);
40      }
41  
42      /**
43       * @param map Map&lt;K,V&gt;; the map to use for the immutable map.
44       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
45       */
46      public ImmutableHashMap(final Map<K, V> map, final Immutable copyOrWrap)
47      {
48          super(copyOrWrap == Immutable.COPY ? new HashMap<K, V>(map) : map, copyOrWrap);
49      }
50  
51      /**
52       * @param immutableMap ImmutableAbstractMap&lt;K,V&gt;; the map to use for the immutable map.
53       */
54      public ImmutableHashMap(final ImmutableAbstractMap<K, V> immutableMap)
55      {
56          super(new HashMap<K, V>(immutableMap.getMap()), Immutable.COPY);
57      }
58  
59      /**
60       * @param immutableMap ImmutableAbstractMap&lt;K,V&gt;; the map to use for the immutable map.
61       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
62       */
63      public ImmutableHashMap(final ImmutableAbstractMap<K, V> immutableMap, final Immutable copyOrWrap)
64      {
65          super(copyOrWrap == Immutable.COPY ? new HashMap<K, V>(immutableMap.getMap()) : immutableMap.getMap(), copyOrWrap);
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      protected final Map<K, V> getMap()
71      {
72          return super.getMap();
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final Map<K, V> toMap()
78      {
79          return new HashMap<K, V>(getMap());
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final ImmutableSet<K> keySet()
85      {
86          if (this.cachedKeySet == null)
87          {
88              Set<K> immutableKeySet = new HashSet<>(getMap().keySet());
89              this.cachedKeySet = new ImmutableHashSet<>(immutableKeySet, Immutable.WRAP);
90          }
91          return this.cachedKeySet;
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public ImmutableSet<ImmutableEntry<K, V>> entrySet()
97      {
98          if (this.cachedEntrySet == null)
99          {
100             Set<ImmutableEntry<K, V>> immutableEntrySet = new HashSet<>();
101             for (Entry<K, V> entry : getMap().entrySet())
102             {
103                 immutableEntrySet.add(new ImmutableEntry<>(entry));
104             }
105             this.cachedEntrySet = new ImmutableHashSet<>(immutableEntrySet, Immutable.WRAP);
106         }
107         return this.cachedEntrySet;
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     @SuppressWarnings("checkstyle:designforextension")
113     public String toString()
114     {
115         Map<K, V> map = getMap();
116         if (null == map)
117         {
118             return "ImmutableHashMap []";
119         }
120         return "ImmutableHashMap [" + map.toString() + "]";
121     }
122 
123 }