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 static final long serialVersionUID = 20160507L;
26
27
28 private ImmutableSet<K> cachedKeySet = null;
29
30
31 private ImmutableSet<ImmutableEntry<K, V>> cachedEntrySet = null;
32
33
34
35
36 public ImmutableLinkedHashMap(final Map<K, V> map)
37 {
38 super(new LinkedHashMap<K, V>(map), Immutable.COPY);
39 }
40
41
42
43
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
52
53 public ImmutableLinkedHashMap(final ImmutableAbstractMap<K, V> immutableMap)
54 {
55 super(new LinkedHashMap<K, V>(immutableMap.getUnderlyingMap()), Immutable.COPY);
56 }
57
58
59
60
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
69 @Override
70 protected final Map<K, V> getUnderlyingMap()
71 {
72 return super.getUnderlyingMap();
73 }
74
75
76 @Override
77 public final Map<K, V> toMap()
78 {
79 return new LinkedHashMap<K, V>(getUnderlyingMap());
80 }
81
82
83 @Override
84 public final ImmutableSet<K> keySet()
85 {
86 if (this.cachedKeySet == null)
87 {
88 Set<K> immutableKeySet = new LinkedHashSet<>(getUnderlyingMap().keySet());
89 this.cachedKeySet = new ImmutableHashSet<>(immutableKeySet, Immutable.WRAP);
90 }
91 return this.cachedKeySet;
92 }
93
94
95 @Override
96 public ImmutableSet<ImmutableEntry<K, V>> entrySet()
97 {
98 if (this.cachedEntrySet == null)
99 {
100 Set<ImmutableEntry<K, V>> immutableEntrySet = new LinkedHashSet<>();
101 for (Entry<K, V> entry : getUnderlyingMap().entrySet())
102 {
103 immutableEntrySet.add(new ImmutableEntry<>(entry));
104 }
105 this.cachedEntrySet = new ImmutableHashSet<>(immutableEntrySet, Immutable.WRAP);
106 }
107 return this.cachedEntrySet;
108 }
109
110
111 @Override
112 public ImmutableCollection<V> values()
113 {
114 if (this.cachedValues == null)
115 {
116 Set<V> immutableValues = new LinkedHashSet<>(getUnderlyingMap().values());
117 this.cachedValues = new ImmutableLinkedHashSet<>(immutableValues, Immutable.WRAP);
118 }
119 return this.cachedValues;
120 }
121
122
123 @Override
124 public final String toString()
125 {
126 Map<K, V> map = getUnderlyingMap();
127 if (null == map)
128 {
129 return "ImmutableLinkedHashMap []";
130 }
131 return "ImmutableLinkedHashMap [" + map.toString() + "]";
132 }
133
134 }