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
23 public class ImmutableLinkedHashMap<K, V> extends ImmutableAbstractMap<K, V>
24 {
25
26 private static final long serialVersionUID = 20160507L;
27
28
29 private ImmutableSet<K> cachedKeySet = null;
30
31
32 private ImmutableSet<ImmutableEntry<K, V>> cachedEntrySet = null;
33
34
35
36
37 public ImmutableLinkedHashMap(final Map<K, V> map)
38 {
39 super(new LinkedHashMap<K, V>(map), Immutable.COPY);
40 }
41
42
43
44
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
53
54 public ImmutableLinkedHashMap(final ImmutableAbstractMap<K, V> immutableMap)
55 {
56 super(new LinkedHashMap<K, V>(immutableMap.getMap()), Immutable.COPY);
57 }
58
59
60
61
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
70 @Override
71 protected final Map<K, V> getMap()
72 {
73 return super.getMap();
74 }
75
76
77 @Override
78 public final Map<K, V> toMap()
79 {
80 return new LinkedHashMap<K, V>(getMap());
81 }
82
83
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
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
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
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 }