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="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   * @author <a href="http://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
45       *            collection
46       */
47      public ImmutableHashMap(final Map<K, V> map, final Immutable copyOrWrap)
48      {
49          super(copyOrWrap == Immutable.COPY ? new HashMap<K, V>(map) : map, copyOrWrap);
50      }
51  
52      /**
53       * @param immutableMap ImmutableAbstractMap&lt;K,V&gt;; the map to use for the immutable map.
54       */
55      public ImmutableHashMap(final ImmutableAbstractMap<K, V> immutableMap)
56      {
57          super(new HashMap<K, V>(immutableMap.getMap()), Immutable.COPY);
58      }
59  
60      /**
61       * @param immutableMap ImmutableAbstractMap&lt;K,V&gt;; the map to use for the immutable map.
62       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original
63       *            collection
64       */
65      public ImmutableHashMap(final ImmutableAbstractMap<K, V> immutableMap, final Immutable copyOrWrap)
66      {
67          super(copyOrWrap == Immutable.COPY ? new HashMap<K, V>(immutableMap.getMap()) : immutableMap.getMap(),
68                  copyOrWrap);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      protected final Map<K, V> getMap()
74      {
75          return 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          if (this.cachedKeySet == null)
90          {
91              Set<K> immutableKeySet = new HashSet<>(getMap().keySet());
92              this.cachedKeySet = new ImmutableHashSet<>(immutableKeySet, Immutable.WRAP);
93          }
94          return this.cachedKeySet;
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public ImmutableSet<ImmutableEntry<K, V>> entrySet()
100     {
101         if (this.cachedEntrySet == null)
102         {
103             Set<ImmutableEntry<K,V>> immutableEntrySet = new HashSet<>();
104             for (Entry<K, V> entry : getMap().entrySet())
105             {
106                 immutableEntrySet.add(new ImmutableEntry<>(entry));
107             }
108             this.cachedEntrySet = new ImmutableHashSet<>(immutableEntrySet, Immutable.WRAP);
109         }
110         return this.cachedEntrySet;
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     @SuppressWarnings("checkstyle:designforextension")
116     public String toString()
117     {
118         Map<K, V> map = getMap();
119         if (null == map)
120         {
121             return "ImmutableHashMap []";
122         }
123         return "ImmutableHashMap [" + map.toString() + "]";
124     }
125 
126 }