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
11
12
13
14
15
16
17
18
19
20
21
22 public class ImmutableLinkedHashMap<K, V> extends ImmutableAbstractMap<K, V>
23 {
24
25 private ImmutableSet<K> cachedKeySet = null;
26
27
28 private ImmutableSet<ImmutableEntry<K, V>> cachedEntrySet = null;
29
30
31
32
33 public ImmutableLinkedHashMap(final Map<K, V> map)
34 {
35 super(new LinkedHashMap<K, V>(map), Immutable.COPY);
36 }
37
38
39
40
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
49
50 public ImmutableLinkedHashMap(final ImmutableAbstractMap<K, V> immutableMap)
51 {
52 super(new LinkedHashMap<K, V>(immutableMap.getUnderlyingMap()), Immutable.COPY);
53 }
54
55
56
57
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 }