/
MapXNodeImpl.java
504 lines (434 loc) · 13.3 KB
/
MapXNodeImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* Copyright (c) 2010-2018 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.prism.impl.xnode;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.Visitor;
import com.evolveum.midpoint.prism.xnode.MapXNode;
import com.evolveum.midpoint.prism.xnode.RootXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.NotNull;
public class MapXNodeImpl extends XNodeImpl implements MapXNode, Map<QName, XNodeImpl> {
// We want to maintain ordering, hence the List
private List<Entry> subnodes = new ArrayList<>();
public int size() {
return subnodes.size();
}
public boolean isEmpty() {
return subnodes.isEmpty();
}
public boolean containsKey(Object key) {
if (!(key instanceof QName)) {
throw new IllegalArgumentException("Key must be QName, but it is "+key);
}
return findEntry((QName)key) != null;
}
public boolean containsValue(Object value) {
if (!(value instanceof XNodeImpl)) {
throw new IllegalArgumentException("Value must be XNode, but it is "+value);
}
return findEntry((XNodeImpl)value) != null;
}
public XNodeImpl get(Object key) {
if (!(key instanceof QName)) {
throw new IllegalArgumentException("Key must be QName, but it is "+key);
}
Entry entry = findEntry((QName)key);
if (entry == null) {
return null;
}
return entry.getValue();
}
public XNodeImpl put(Map.Entry<QName, XNodeImpl> entry) {
return put(entry.getKey(), entry.getValue());
}
public XNodeImpl put(QName key, XNode value) {
return put(key, (XNodeImpl) value);
}
public XNodeImpl put(QName key, XNodeImpl value) {
XNodeImpl previous = removeEntry(key);
subnodes.add(new Entry(key, value));
return previous;
}
public Entry putReturningEntry(QName key, XNodeImpl value, boolean doNotRemovePrevious) {
if (!doNotRemovePrevious) {
removeEntry(key);
}
Entry e = new Entry(key, value);
subnodes.add(e);
return e;
}
public XNodeImpl remove(Object key) {
if (!(key instanceof QName)) {
throw new IllegalArgumentException("Key must be QName, but it is "+key);
}
return removeEntry((QName)key);
}
public void putAll(Map<? extends QName, ? extends XNodeImpl> m) {
for (Map.Entry<?, ?> entry: m.entrySet()) {
put((QName)entry.getKey(), (XNodeImpl)entry.getValue());
}
}
public void clear() {
subnodes.clear();
}
@NotNull
public Set<QName> keySet() {
Set<QName> keySet = new HashSet<>();
for (Entry entry: subnodes) {
keySet.add(entry.getKey());
}
return keySet;
}
@NotNull
public Collection<XNodeImpl> values() {
Collection<XNodeImpl> values = new ArrayList<>(subnodes.size());
for (Entry entry: subnodes) {
values.add(entry.getValue());
}
return values;
}
@Override
public java.util.Map.Entry<QName, XNodeImpl> getSingleSubEntry(String errorContext) throws SchemaException {
if (isEmpty()) {
return null;
}
if (size() > 1) {
throw new SchemaException("More than one element in " + errorContext +" : "+dumpKeyNames());
}
return subnodes.get(0);
}
@Override
public RootXNode getSingleSubEntryAsRoot(String errorContext) throws SchemaException {
java.util.Map.Entry<QName, XNodeImpl> entry = getSingleSubEntry(errorContext);
return entry != null ? new RootXNodeImpl(entry.getKey(), entry.getValue()) : null;
}
public Entry getSingleEntryThatDoesNotMatch(QName... excludedKeys) throws SchemaException {
Entry found = null;
OUTER: for (Entry subentry: subnodes) {
for (QName excludedKey: excludedKeys) {
if (QNameUtil.match(subentry.getKey(), excludedKey)) {
continue OUTER;
}
}
if (found != null) {
throw new SchemaException("More than one extension subnode found under "+this+": "+found.getKey()+" and "+subentry.getKey());
} else {
found = subentry;
}
}
return found;
}
@NotNull
public Set<java.util.Map.Entry<QName, XNodeImpl>> entrySet() {
Set<java.util.Map.Entry<QName, XNodeImpl>> entries = new Set<Map.Entry<QName, XNodeImpl>>() {
@Override
public int size() {
return subnodes.size();
}
@Override
public boolean isEmpty() {
return subnodes.isEmpty();
}
@Override
public boolean contains(Object o) {
return subnodes.contains(o);
}
@Override
public Iterator<java.util.Map.Entry<QName, XNodeImpl>> iterator() {
return (Iterator)subnodes.iterator();
}
@Override
public Object[] toArray() {
return subnodes.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return subnodes.toArray(a);
}
@Override
public boolean add(java.util.Map.Entry<QName, XNodeImpl> e) {
throw new UnsupportedOperationException();
// put(e.getKey(), e.getValue());
// return true;
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection<?> c) {
return subnodes.containsAll(c);
}
@Override
public boolean addAll(Collection<? extends java.util.Map.Entry<QName, XNodeImpl>> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
};
return entries;
}
public <T> T getParsedPrimitiveValue(QName key, QName typeName) throws SchemaException {
XNodeImpl xnode = get(key);
if (xnode == null) {
return null;
}
if (!(xnode instanceof PrimitiveXNodeImpl<?>)) {
throw new SchemaException("Expected that field "+key+" will be primitive, but it is "+xnode.getDesc());
}
PrimitiveXNodeImpl<T> xprim = (PrimitiveXNodeImpl<T>)xnode;
return xprim.getParsedValue(typeName, null); // TODO expected class
}
public void merge(MapXNodeImpl other) {
for (java.util.Map.Entry<QName, XNodeImpl> otherEntry: other.entrySet()) {
QName otherKey = otherEntry.getKey();
XNodeImpl otherValue = otherEntry.getValue();
merge (otherKey, otherValue);
}
}
public void merge(QName otherKey, XNode otherValue) {
XNodeImpl myValue = get(otherKey);
if (myValue == null) {
put(otherKey, otherValue);
} else {
ListXNodeImpl myList;
if (myValue instanceof ListXNodeImpl) {
myList = (ListXNodeImpl)myValue;
} else {
myList = new ListXNodeImpl();
myList.add(myValue);
put(otherKey, myList);
}
if (otherValue instanceof ListXNodeImpl) {
myList.addAll((ListXNodeImpl)otherValue);
} else {
myList.add((XNodeImpl) otherValue);
}
}
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
for (Entry subentry: subnodes) {
if (subentry.value != null) {
subentry.value.accept(visitor);
} else {
//throw new IllegalStateException("null value of key " + subentry.key + " in map: " + debugDump());
}
}
}
public boolean equals(Object o) {
if (!(o instanceof MapXNodeImpl)){
return false;
}
MapXNodeImpl other = (MapXNodeImpl) o;
return MiscUtil.unorderedCollectionEquals(this.entrySet(), other.entrySet());
}
public int hashCode() {
int result = 0xCAFEBABE;
for (XNodeImpl node : this.values()) {
if (node != null){
result = result ^ node.hashCode(); // using XOR instead of multiplying and adding in order to achieve commutativity
}
}
return result;
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.debugDumpMapMultiLine(sb, this, indent, true, dumpSuffix());
return sb.toString();
}
@Override
public String getDesc() {
return "map";
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("XNode(map:"+subnodes.size()+" entries)");
sb.append("\n");
subnodes.forEach(entry -> {
sb.append(entry.toString());
sb.append("; \n");
});
return sb.toString();
}
private Entry findEntry(QName qname) {
for (Entry entry: subnodes) {
if (QNameUtil.match(qname,entry.getKey())) {
return entry;
}
}
return null;
}
private Entry findEntry(XNodeImpl xnode) {
for (Entry entry: subnodes) {
if (entry.getValue().equals(xnode)) {
return entry;
}
}
return null;
}
private XNodeImpl removeEntry(QName key) {
Iterator<Entry> iterator = subnodes.iterator();
while (iterator.hasNext()) {
Entry entry = iterator.next();
if (QNameUtil.match(key,entry.getKey())) {
iterator.remove();
return entry.getValue();
}
}
return null;
}
public String dumpKeyNames() {
StringBuilder sb = new StringBuilder();
Iterator<Entry> iterator = subnodes.iterator();
while (iterator.hasNext()) {
Entry entry = iterator.next();
sb.append(PrettyPrinter.prettyPrint(entry.getKey()));
if (iterator.hasNext()) {
sb.append(",");
}
}
return sb.toString();
}
public void qualifyKey(QName key, String newNamespace) {
for (Entry entry : subnodes) {
if (key.equals(entry.getKey())) {
entry.qualifyKey(newNamespace);
}
}
}
public XNodeImpl replace(QName key, XNodeImpl value) {
for (Entry entry : subnodes) {
if (entry.getKey().equals(key)) {
XNodeImpl previous = entry.getValue();
entry.setValue(value);
return previous;
}
}
return put(key, value);
}
public RootXNodeImpl getEntryAsRoot(@NotNull QName key) {
XNodeImpl xnode = get(key);
return xnode != null ? new RootXNodeImpl(key, xnode) : null;
}
@NotNull
public RootXNodeImpl getSingleEntryMapAsRoot() {
if (!isSingleEntryMap()) {
throw new IllegalStateException("Expected to be called on single-entry map");
}
QName key = keySet().iterator().next();
return new RootXNodeImpl(key, get(key));
}
private static class Entry implements Map.Entry<QName, XNodeImpl>, Serializable {
private QName key;
private XNodeImpl value;
public Entry(QName key) {
super();
this.key = key;
}
public Entry(QName key, XNodeImpl value) {
super();
this.key = key;
this.value = value;
}
public void qualifyKey(String newNamespace) {
Validate.notNull(key, "Key is null");
if (StringUtils.isNotEmpty(key.getNamespaceURI())) {
throw new IllegalStateException("Cannot qualify already qualified key: " + key);
}
key = new QName(newNamespace, key.getLocalPart());
}
@Override
public QName getKey() {
return key;
}
@Override
public XNodeImpl getValue() {
return value;
}
@Override
public XNodeImpl setValue(XNodeImpl value) {
this.value = value;
return value;
}
@Override
public String toString() {
return "E(" + key + ": " + value + ")";
}
/**
* Compares two entries of the MapXNode.
*
* It is questionable whether to compare QNames exactly or approximately (using QNameUtil.match) here.
* For the time being, exact comparison was chosen. The immediate reason is to enable correct
* processing of diff on RawTypes (e.g. to make "debug edit" to be able to change from xyz to c:xyz
* in element names, see MID-1969).
*
* TODO: In the long run, we have to think out where exactly we want to use approximate matching of QNames.
* E.g. it is reasonable to use it only where "deployer input" is expected (e.g. import of data objects),
* not in the internals of midPoint.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Entry entry = (Entry) o;
if (key != null ? !key.equals(entry.key) : entry.key != null) return false;
if (value != null ? !value.equals(entry.value) : entry.value != null) return false;
return true;
}
@Override
public int hashCode() {
int result = key != null && key.getLocalPart() != null ? key.getLocalPart().hashCode() : 0;
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
}
@Override
public MapXNodeImpl clone() {
return (MapXNodeImpl) super.clone(); // fixme brutal hack
}
@Override
public Map<QName, ? extends XNode> asMap() {
return this;
}
}