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