1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.sling.commons.resource;
21
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.TreeMap;
28
29 import org.apache.sling.api.resource.ValueMap;
30 import org.apache.sling.api.wrappers.ValueMapDecorator;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 import org.osgi.annotation.versioning.ProviderType;
34
35
36
37
38
39
40
41
42
43
44
45
46 @ProviderType
47 public final class ImmutableValueMap implements ValueMap {
48
49 private final ValueMap map;
50
51
52
53
54 ImmutableValueMap(@NotNull ValueMap map) {
55 this.map = map;
56 }
57
58
59
60
61 ImmutableValueMap(@NotNull Map<String, Object> map) {
62 this.map = new ValueMapDecorator(map);
63 }
64
65 @Override
66 public @Nullable <T> T get(@NotNull String name, @NotNull Class<T> type) {
67 return this.map.get(name, type);
68 }
69
70 @Override
71 public @NotNull <T> T get(@NotNull String name, @NotNull T defaultValue) {
72 return this.map.get(name, defaultValue);
73 }
74
75 @Override
76 public int size() {
77 return this.map.size();
78 }
79
80 @Override
81 public boolean isEmpty() {
82 return this.map.isEmpty();
83 }
84
85 @Override
86 public boolean containsKey(Object key) {
87 return this.map.containsKey(key);
88 }
89
90 @Override
91 public boolean containsValue(Object value) {
92 return this.map.containsValue(value);
93 }
94
95 @Override
96 public Object get(Object key) {
97 return this.map.get(key);
98 }
99
100 @Override
101 public Set<String> keySet() {
102 return this.map.keySet();
103 }
104
105 @Override
106 public Collection<Object> values() {
107 return this.map.values();
108 }
109
110 @Override
111 public Set<Entry<String, Object>> entrySet() {
112 return Collections.unmodifiableSet(this.map.entrySet());
113 }
114
115 @Override
116 public int hashCode() {
117 return this.map.hashCode();
118 }
119
120 @Override
121 public boolean equals(Object obj) {
122 if (!(obj instanceof ImmutableValueMap)) {
123 return false;
124 }
125 return this.map.entrySet().equals(((ImmutableValueMap)obj).map.entrySet());
126 }
127
128 @Override
129 public String toString() {
130 return new TreeMap<>(map).toString();
131 }
132
133
134
135
136
137 @Override
138 @Deprecated(since = "1.0.0")
139 public Object put(String key, Object value) {
140 throw new UnsupportedOperationException();
141 }
142
143
144
145
146 @Override
147 @Deprecated(since = "1.0.0")
148 public Object remove(Object key) {
149 throw new UnsupportedOperationException();
150 }
151
152
153
154
155 @Override
156 @Deprecated(since = "1.0.0")
157 public void putAll(Map<? extends String, ? extends Object> m) {
158 throw new UnsupportedOperationException();
159 }
160
161
162
163
164 @Override
165 @Deprecated(since = "1.0.0")
166 public void clear() {
167 throw new UnsupportedOperationException();
168 }
169
170
171
172
173
174
175
176
177 public static @NotNull ImmutableValueMap of() {
178 return new ImmutableValueMap(EMPTY);
179 }
180
181
182
183
184
185
186
187
188
189
190 public static @NotNull ImmutableValueMap of(@NotNull String k1, @NotNull Object v1) {
191 Map<String, Object> map = new LinkedHashMap<>();
192 map.put(k1, v1);
193 return new ImmutableValueMap(Collections.unmodifiableMap(map));
194 }
195
196
197
198
199
200
201
202
203
204
205 public static @NotNull ImmutableValueMap of(@NotNull String k1, @NotNull Object v1,
206 @NotNull String k2, @NotNull Object v2) {
207 Map<String, Object> map = new LinkedHashMap<>();
208 map.put(k1, v1);
209 map.put(k2, v2);
210 return new ImmutableValueMap(Collections.unmodifiableMap(map));
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224 public static @NotNull ImmutableValueMap of(
225 @NotNull String k1, @NotNull Object v1,
226 @NotNull String k2, @NotNull Object v2,
227 @NotNull String k3, @NotNull Object v3) {
228 Map<String, Object> map = new LinkedHashMap<>();
229 map.put(k1, v1);
230 map.put(k2, v2);
231 map.put(k3, v3);
232 return new ImmutableValueMap(Collections.unmodifiableMap(map));
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 @SuppressWarnings({ "java:S107", "PMD.UseObjectForClearerAPI" })
249 public static @NotNull ImmutableValueMap of(
250 @NotNull String k1, @NotNull Object v1,
251 @NotNull String k2, @NotNull Object v2,
252 @NotNull String k3, @NotNull Object v3,
253 @NotNull String k4, @NotNull Object v4) {
254 Map<String, Object> map = new LinkedHashMap<>();
255 map.put(k1, v1);
256 map.put(k2, v2);
257 map.put(k3, v3);
258 map.put(k4, v4);
259 return new ImmutableValueMap(Collections.unmodifiableMap(map));
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 @SuppressWarnings({ "java:S107", "PMD.UseObjectForClearerAPI" })
278 public static ImmutableValueMap of(
279 @NotNull String k1, @NotNull Object v1,
280 @NotNull String k2, @NotNull Object v2,
281 @NotNull String k3, @NotNull Object v3,
282 @NotNull String k4, @NotNull Object v4,
283 @NotNull String k5, @NotNull Object v5) {
284 Map<String, Object> map = new LinkedHashMap<>();
285 map.put(k1, v1);
286 map.put(k2, v2);
287 map.put(k3, v3);
288 map.put(k4, v4);
289 map.put(k5, v5);
290 return new ImmutableValueMap(Collections.unmodifiableMap(map));
291 }
292
293
294
295
296
297
298
299
300 public static @NotNull Builder builder() {
301 return new Builder();
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 public static @NotNull ImmutableValueMap copyOf(@NotNull Map<String, Object> map) {
319 return new ImmutableValueMap(Collections.unmodifiableMap(map));
320 }
321
322
323
324
325 public static final class Builder {
326
327 private final @NotNull Map<String, Object> map = new LinkedHashMap<>();
328
329
330
331
332
333
334
335
336 public @NotNull Builder put(@NotNull String key, @NotNull Object value) {
337 map.put(key, value);
338 return this;
339 }
340
341
342
343
344
345
346
347 public @NotNull Builder put(@NotNull Entry<String, Object> entry) {
348 return put(entry.getKey(), entry.getValue());
349 }
350
351
352
353
354
355
356
357
358 public @NotNull Builder putAll(@NotNull Map<String, Object> value) {
359 map.putAll(value);
360 return this;
361 }
362
363
364
365
366
367
368 public @NotNull ImmutableValueMap build() {
369 if (map.isEmpty()) {
370 return of();
371 }
372 else {
373 return new ImmutableValueMap(map);
374 }
375 }
376 }
377
378 }