View Javadoc
1   package org.djutils.immutablecollections;
2   
3   import java.util.LinkedHashMap;
4   import java.util.LinkedHashSet;
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 ImmutableLinkedHashMap<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 ImmutableLinkedHashMap(final Map<K, V> map)
38      {
39          super(new LinkedHashMap<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 ImmutableLinkedHashMap(final Map<K, V> map, final Immutable copyOrWrap)
47      {
48          super(copyOrWrap == Immutable.COPY ? new LinkedHashMap<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 ImmutableLinkedHashMap(final ImmutableAbstractMap<K, V> immutableMap)
55      {
56          super(new LinkedHashMap<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 ImmutableLinkedHashMap(final ImmutableAbstractMap<K, V> immutableMap, final Immutable copyOrWrap)
64      {
65          super(copyOrWrap == Immutable.COPY ? new LinkedHashMap<K, V>(immutableMap.getMap()) : immutableMap.getMap(),
66                  copyOrWrap);
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      protected final Map<K, V> getMap()
72      {
73          return super.getMap();
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public final Map<K, V> toMap()
79      {
80          return new LinkedHashMap<K, V>(getMap());
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public final ImmutableSet<K> keySet()
86      {
87          if (this.cachedKeySet == null)
88          {
89              Set<K> immutableKeySet = new LinkedHashSet<>(getMap().keySet());
90              this.cachedKeySet = new ImmutableHashSet<>(immutableKeySet, Immutable.WRAP);
91          }
92          return this.cachedKeySet;
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public ImmutableSet<ImmutableEntry<K, V>> entrySet()
98      {
99          if (this.cachedEntrySet == null)
100         {
101             Set<ImmutableEntry<K, V>> immutableEntrySet = new LinkedHashSet<>();
102             for (Entry<K, V> entry : getMap().entrySet())
103             {
104                 immutableEntrySet.add(new ImmutableEntry<>(entry));
105             }
106             this.cachedEntrySet = new ImmutableHashSet<>(immutableEntrySet, Immutable.WRAP);
107         }
108         return this.cachedEntrySet;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public ImmutableCollection<V> values()
114     {
115         if (this.cachedValues == null)
116         {
117             Set<V> immutableValues = new LinkedHashSet<>(getMap().values());
118             this.cachedValues = new ImmutableLinkedHashSet<>(immutableValues, Immutable.WRAP);
119         }
120         return this.cachedValues;
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public final String toString()
126     {
127         Map<K, V> map = getMap();
128         if (null == map)
129         {
130             return "ImmutableLinkedHashMap []";
131         }
132         return "ImmutableLinkedHashMap [" + map.toString() + "]";
133     }
134 
135 }