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 LinkedHashMap.
11   * <p>
12   * Copyright (c) 2016-2024 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   * @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 ImmutableLinkedHashMap<K, V> extends ImmutableAbstractMap<K, V>
23  {
24      /** */
25      private static final long serialVersionUID = 20160507L;
26  
27      /** the cached keySet. */
28      private ImmutableSet<K> cachedKeySet = null;
29  
30      /** the cached entrySet. */
31      private ImmutableSet<ImmutableEntry<K, V>> cachedEntrySet = null;
32  
33      /**
34       * @param map Map&lt;K,V&gt;; the map to use for the immutable map.
35       */
36      public ImmutableLinkedHashMap(final Map<K, V> map)
37      {
38          super(new LinkedHashMap<K, V>(map), Immutable.COPY);
39      }
40  
41      /**
42       * @param map Map&lt;K,V&gt;; the map to use for the immutable map.
43       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
44       */
45      public ImmutableLinkedHashMap(final Map<K, V> map, final Immutable copyOrWrap)
46      {
47          super(copyOrWrap == Immutable.COPY ? new LinkedHashMap<K, V>(map) : map, copyOrWrap);
48      }
49  
50      /**
51       * @param immutableMap ImmutableAbstractMap&lt;K,V&gt;; the map to use for the immutable map.
52       */
53      public ImmutableLinkedHashMap(final ImmutableAbstractMap<K, V> immutableMap)
54      {
55          super(new LinkedHashMap<K, V>(immutableMap.getUnderlyingMap()), Immutable.COPY);
56      }
57  
58      /**
59       * @param immutableMap ImmutableAbstractMap&lt;K,V&gt;; the map to use for the immutable map.
60       * @param copyOrWrap COPY stores a safe, internal copy of the collection; WRAP stores a pointer to the original collection
61       */
62      public ImmutableLinkedHashMap(final ImmutableAbstractMap<K, V> immutableMap, final Immutable copyOrWrap)
63      {
64          super(copyOrWrap == Immutable.COPY ? new LinkedHashMap<K, V>(immutableMap.getUnderlyingMap())
65                  : immutableMap.getUnderlyingMap(), copyOrWrap);
66      }
67  
68      @Override
69      protected final Map<K, V> getUnderlyingMap()
70      {
71          return super.getUnderlyingMap();
72      }
73  
74      @Override
75      public final Map<K, V> toMap()
76      {
77          return new LinkedHashMap<K, V>(getUnderlyingMap());
78      }
79  
80      @Override
81      public final ImmutableSet<K> keySet()
82      {
83          if (this.cachedKeySet == null)
84          {
85              Set<K> immutableKeySet = new LinkedHashSet<>(getUnderlyingMap().keySet());
86              this.cachedKeySet = new ImmutableHashSet<>(immutableKeySet, Immutable.WRAP);
87          }
88          return this.cachedKeySet;
89      }
90  
91      @Override
92      public ImmutableSet<ImmutableEntry<K, V>> entrySet()
93      {
94          if (this.cachedEntrySet == null)
95          {
96              Set<ImmutableEntry<K, V>> immutableEntrySet = new LinkedHashSet<>();
97              for (Entry<K, V> entry : getUnderlyingMap().entrySet())
98              {
99                  immutableEntrySet.add(new ImmutableEntry<>(entry));
100             }
101             this.cachedEntrySet = new ImmutableHashSet<>(immutableEntrySet, Immutable.WRAP);
102         }
103         return this.cachedEntrySet;
104     }
105 
106     @Override
107     public ImmutableCollection<V> values()
108     {
109         if (this.cachedValues == null)
110         {
111             Set<V> immutableValues = new LinkedHashSet<>(getUnderlyingMap().values());
112             this.cachedValues = new ImmutableLinkedHashSet<>(immutableValues, Immutable.WRAP);
113         }
114         return this.cachedValues;
115     }
116 
117     @Override
118     public final String toString()
119     {
120         Map<K, V> map = getUnderlyingMap();
121         if (null == map)
122         {
123             return "ImmutableLinkedHashMap []";
124         }
125         return "ImmutableLinkedHashMap [" + map.toString() + "]";
126     }
127 
128 }