/
Item.java
1025 lines (909 loc) · 30.8 KB
/
Item.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010-2017 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;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.DebugDumpable;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import org.jetbrains.annotations.NotNull;
import javax.xml.namespace.QName;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Item is a common abstraction of Property and PropertyContainer.
* <p>
* This is supposed to be a superclass for all items. Items are things
* that can appear in property containers, which generally means only a property
* and property container itself. Therefore this is in fact superclass for those
* two definitions.
*
* @author Radovan Semancik
*/
public abstract class Item<V extends PrismValue, D extends ItemDefinition> implements Itemable, DebugDumpable, Visitable, PathVisitable, Serializable, Revivable {
private static final long serialVersionUID = 510000191615288733L;
// The object should basically work without definition and prismContext. This is the
// usual case when it is constructed "out of the blue", e.g. as a new JAXB object
// It may not work perfectly, but basic things should work
protected QName elementName;
protected PrismValue parent;
protected D definition;
@NotNull protected final List<V> values = new ArrayList<>();
private transient Map<String,Object> userData = new HashMap<>();;
protected boolean immutable;
protected boolean incomplete;
protected transient PrismContext prismContext; // beware, this one can easily be null
/**
* This is used for definition-less construction, e.g. in JAXB beans.
*
* The constructors should be used only occasionally (if used at all).
* Use the factory methods in the ResourceObjectDefintion instead.
*/
Item(QName elementName) {
super();
this.elementName = elementName;
}
Item(QName elementName, PrismContext prismContext) {
super();
this.elementName = elementName;
this.prismContext = prismContext;
}
/**
* The constructors should be used only occasionally (if used at all).
* Use the factory methods in the ResourceObjectDefintion instead.
*/
Item(QName elementName, D definition, PrismContext prismContext) {
super();
this.elementName = elementName;
this.definition = definition;
this.prismContext = prismContext;
}
/**
* Returns applicable property definition.
* <p>
* May return null if no definition is applicable or the definition is not
* know.
*
* @return applicable property definition
*/
public D getDefinition() {
return definition;
}
public boolean hasCompleteDefinition() {
return getDefinition() != null;
}
/**
* Returns the name of the property.
* <p>
* The name is a QName. It uniquely defines a property.
* <p>
* The name may be null, but such a property will not work.
* <p>
* The name is the QName of XML element in the XML representation.
*
* @return property name
*/
@Override
public QName getElementName() {
return elementName;
}
/**
* Sets the name of the property.
* <p>
* The name is a QName. It uniquely defines a property.
* <p>
* The name may be null, but such a property will not work.
* <p>
* The name is the QName of XML element in the XML representation.
*
* @param elementName the name to set
*/
public void setElementName(QName elementName) {
checkMutability();
this.elementName = elementName;
}
/**
* Sets applicable property definition.
*
* @param definition the definition to set
*/
public void setDefinition(D definition) {
checkMutability();
checkDefinition(definition);
this.definition = definition;
}
/**
* Returns a display name for the property type.
* <p>
* Returns null if the display name cannot be determined.
* <p>
* The display name is fetched from the definition. If no definition
* (schema) is available, the display name will not be returned.
*
* @return display name for the property type
*/
public String getDisplayName() {
return getDefinition() == null ? null : getDefinition().getDisplayName();
}
/**
* Returns help message defined for the property type.
* <p>
* Returns null if the help message cannot be determined.
* <p>
* The help message is fetched from the definition. If no definition
* (schema) is available, the help message will not be returned.
*
* @return help message for the property type
*/
public String getHelp() {
return getDefinition() == null ? null : getDefinition().getHelp();
}
/**
* Flag that indicates incomplete item. If set to true then the
* values in this item are not complete. If this flag is true
* then it can be assumed that the object that this item represents
* has at least one value. This is a method how to indicate that
* the item really has some values, but are not here. This may
* be used for variety of purposes. It may indicate that the
* account has a password, but the password value is not revealed.
* This may indicate that a user has a photo, but the photo was not
* requested and therefore is not returned. This may be used to indicate
* that only part of the attribute values were returned from the search.
* And so on.
*/
public boolean isIncomplete() {
return incomplete;
}
public void setIncomplete(boolean incomplete) {
this.incomplete = incomplete;
}
@Override
public PrismContext getPrismContext() {
if (prismContext != null) {
return prismContext;
} else if (parent != null) {
return parent.getPrismContext();
} else {
return null;
}
}
// Primarily for testing
public PrismContext getPrismContextLocal() {
return prismContext;
}
public void setPrismContext(PrismContext prismContext) {
this.prismContext = prismContext;
}
public PrismValue getParent() {
return parent;
}
public void setParent(PrismValue parentValue) {
if (this.parent != null && parentValue != null && this.parent != parentValue) {
throw new IllegalStateException("Attempt to reset parent of item "+this+" from "+this.parent+" to "+parentValue);
}
// Immutability check can be skipped, as setting the parent doesn't alter this object.
// However, if existing parent itself is immutable, adding/removing its child item will cause the exception.
this.parent = parentValue;
}
public ItemPath getPath() {
if (parent == null) {
return new ItemPath(getElementName());
}
return parent.getPath().subPath(getElementName());
}
public Map<String, Object> getUserData() {
if (userData == null) {
userData = new HashMap<>();
}
if (immutable) {
return Collections.unmodifiableMap(userData); // TODO beware, objects in userData themselves are mutable
} else {
return userData;
}
}
public Object getUserData(String key) {
// TODO make returned data immutable (?)
return getUserData().get(key);
}
public void setUserData(String key, Object value) {
checkMutability();
getUserData().put(key, value);
}
@NotNull
public List<V> getValues() {
return values;
}
public V getValue(int index) {
if (index < 0) {
index = values.size() + index;
}
return values.get(index);
}
public abstract <X> X getRealValue();
public abstract <X> Collection<X> getRealValues();
public boolean hasValue(PrismValue value, boolean ignoreMetadata) {
return (findValue(value, ignoreMetadata) != null);
}
public boolean hasValue(PrismValue value) {
return hasValue(value, false);
}
public boolean hasRealValue(PrismValue value) {
return hasValue(value, true);
}
public boolean isSingleValue() {
// TODO what about dynamic definitions? See MID-3922
if (getDefinition() != null) {
if (getDefinition().isMultiValue()) {
return false;
}
}
return values.size() <= 1;
}
/**
* Returns value that is equal or equivalent to the provided value.
* The returned value is an instance stored in this item, while the
* provided value argument may not be.
*/
public PrismValue findValue(PrismValue value, boolean ignoreMetadata) {
for (PrismValue myVal : getValues()) {
if (myVal.equalsComplex(value, ignoreMetadata, false)) {
return myVal;
}
}
return null;
}
public List<? extends PrismValue> findValuesIgnoreMetadata(PrismValue value) {
return getValues().stream()
.filter(v -> v.equalsComplex(value, true, false))
.collect(Collectors.toList());
}
/**
* Returns value that is previous to the specified value.
* Note that the order is semantically insignificant and this is used only
* for presentation consistency in order-sensitive formats such as XML or JSON.
*/
public PrismValue getPreviousValue(PrismValue value) {
PrismValue previousValue = null;
for (PrismValue myVal : getValues()) {
if (myVal == value) {
return previousValue;
}
previousValue = myVal;
}
throw new IllegalStateException("The value "+value+" is not any of "+this+" values, therefore cannot determine previous value");
}
/**
* Returns values that is following the specified value.
* Note that the order is semantically insignificant and this is used only
* for presentation consistency in order-sensitive formats such as XML or JSON.
*/
public PrismValue getNextValue(PrismValue value) {
Iterator<V> iterator = getValues().iterator();
while (iterator.hasNext()) {
PrismValue myVal = iterator.next();
if (myVal == value) {
if (iterator.hasNext()) {
return iterator.next();
} else {
return null;
}
}
}
throw new IllegalStateException("The value "+value+" is not any of "+this+" values, therefore cannot determine next value");
}
public Collection<V> getClonedValues() {
Collection<V> clonedValues = new ArrayList<V>(getValues().size());
for (V val: getValues()) {
clonedValues.add((V)val.clone());
}
return clonedValues;
}
public boolean contains(V value) {
return contains(value, false);
}
public boolean containsEquivalentValue(V value) {
return contains(value, true);
}
public boolean contains(V value, boolean ignoreMetadata, Comparator<V> comparator) {
if (comparator == null){
return contains(value, ignoreMetadata);
} else{
for (V myValue: getValues()) {
if (comparator.compare(myValue, value) == 0) {
return true;
}
}
}
return false;
}
public boolean contains(V value, boolean ignoreMetadata) {
for (V myValue: getValues()) {
if (myValue.equals(value, ignoreMetadata)) {
return true;
}
}
return false;
}
public boolean containsRealValue(V value) {
for (V myValue: getValues()) {
if (myValue.equalsRealValue(value)) {
return true;
}
}
return false;
}
public boolean valuesExactMatch(Collection<V> matchValues, Comparator<V> comparator) {
return MiscUtil.unorderedCollectionCompare(values, matchValues, comparator );
}
public int size() {
return values.size();
}
public boolean addAll(Collection<V> newValues) throws SchemaException {
checkMutability(); // TODO consider weaker condition, like testing if there's a real change
boolean changed = false;
for (V val: newValues) {
if (add(val)) {
changed = true;
}
}
return changed;
}
public boolean add(@NotNull V newValue) throws SchemaException {
return add(newValue, true);
}
public boolean add(@NotNull V newValue, boolean checkUniqueness) throws SchemaException {
checkMutability();
if (newValue.getPrismContext() == null) {
newValue.setPrismContext(prismContext);
}
newValue.setParent(this);
if (checkUniqueness && containsEquivalentValue(newValue)) {
return false;
}
D definition = getDefinition();
if (definition != null) {
if (!isEmpty() && definition.isSingleValue()) {
throw new SchemaException("Attempt to put more than one value to single-valued item " + this + "; newly added value: " + newValue);
}
newValue.applyDefinition(definition, false);
}
return values.add(newValue);
}
public boolean removeAll(Collection<V> newValues) {
checkMutability(); // TODO consider if there is real change
boolean changed = false;
for (V val: newValues) {
if (remove(val)) {
changed = true;
}
}
return changed;
}
public boolean remove(V newValue) {
checkMutability(); // TODO consider if there is real change
boolean changed = false;
Iterator<V> iterator = values.iterator();
while (iterator.hasNext()) {
V val = iterator.next();
// the same algorithm as when deleting the item value from delete delta
// TODO either make equalsRealValue return false if both PCVs have IDs and these IDs are different
// TODO or include a special test condition here; see MID-3828
if (val.representsSameValue(newValue, false) || val.equalsRealValue(newValue)) {
iterator.remove();
changed = true;
}
}
return changed;
}
public V remove(int index) {
checkMutability(); // TODO consider if there is real change
return values.remove(index);
}
public void replaceAll(Collection<V> newValues) throws SchemaException {
checkMutability(); // TODO consider if there is real change
values.clear();
addAll(newValues);
}
public void replace(V newValue) {
checkMutability(); // TODO consider if there is real change
values.clear();
newValue.setParent(this);
values.add(newValue);
}
public void clear() {
checkMutability(); // TODO consider if there is real change
values.clear();
}
public void normalize() {
checkMutability(); // TODO consider if there is real change
for (V value : values) {
value.normalize();
}
}
/**
* Merge all the values of other item to this item.
*/
public void merge(Item<V,D> otherItem) throws SchemaException {
for (V otherValue: otherItem.getValues()) {
if (!contains(otherValue)) {
add((V) otherValue.clone());
}
}
}
public abstract Object find(ItemPath path);
public abstract <IV extends PrismValue,ID extends ItemDefinition> PartiallyResolvedItem<IV,ID> findPartial(ItemPath path);
// We want this method to be consistent with property diff
public ItemDelta<V,D> diff(Item<V,D> other) {
return diff(other, true, false);
}
// We want this method to be consistent with property diff
public ItemDelta<V,D> diff(Item<V,D> other, boolean ignoreMetadata, boolean isLiteral) {
List<? extends ItemDelta> itemDeltas = new ArrayList<>();
diffInternal(other, itemDeltas, ignoreMetadata, isLiteral);
if (itemDeltas.isEmpty()) {
return null;
}
if (itemDeltas.size() > 1) {
throw new UnsupportedOperationException("Item multi-delta diff is not supported yet");
}
return itemDeltas.get(0);
}
protected void diffInternal(Item<V,D> other, Collection<? extends ItemDelta> deltas,
boolean ignoreMetadata, boolean isLiteral) {
ItemDelta delta = createDelta();
if (other == null) {
//other doesn't exist, so delta means delete all values
for (PrismValue value : getValues()) {
PrismValue valueClone = value.clone();
delta.addValueToDelete(valueClone);
}
} else {
// the other exists, this means that we need to compare the values one by one
Collection<PrismValue> outstandingOtheValues = new ArrayList<PrismValue>(other.getValues().size());
outstandingOtheValues.addAll(other.getValues());
for (PrismValue thisValue : getValues()) {
Iterator<PrismValue> iterator = outstandingOtheValues.iterator();
boolean found = false;
while (iterator.hasNext()) {
PrismValue otherValue = iterator.next();
if (thisValue.representsSameValue(otherValue, true) || delta == null) {
found = true;
// Matching IDs, look inside to figure out internal deltas
thisValue.diffMatchingRepresentation(otherValue, deltas,
ignoreMetadata, isLiteral);
// No need to process this value again
iterator.remove();
break;
// TODO either make equalsRealValue return false if both PCVs have IDs and these IDs are different
// TODO or include a special test condition here; see MID-3828
} else if (thisValue.equalsComplex(otherValue, ignoreMetadata, isLiteral)) {
found = true;
// same values. No delta
// No need to process this value again
iterator.remove();
break;
}
}
if (!found) {
// We have the value and the other does not, this is delete of the entire value
delta.addValueToDelete(thisValue.clone());
}
}
// outstandingOtheValues are those values that the other has and we could not
// match them to any of our values. These must be new values to add
for (PrismValue outstandingOtherValue : outstandingOtheValues) {
delta.addValueToAdd(outstandingOtherValue.clone());
}
// Some deltas may need to be polished a bit. E.g. transforming
// add/delete delta to a replace delta.
delta = fixupDelta(delta, other, ignoreMetadata);
}
if (delta != null && !delta.isEmpty()) {
((Collection)deltas).add(delta);
}
}
protected ItemDelta<V,D> fixupDelta(ItemDelta<V,D> delta, Item<V,D> other,
boolean ignoreMetadata) {
return delta;
}
/**
* Creates specific subclass of ItemDelta appropriate for type of item that this definition
* represents (e.g. PropertyDelta, ContainerDelta, ...)
*/
public abstract ItemDelta<V,D> createDelta();
public abstract ItemDelta<V,D> createDelta(ItemPath path);
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
for(PrismValue value: getValues()) {
value.accept(visitor);
}
}
@Override
public void accept(Visitor visitor, ItemPath path, boolean recursive) {
// This implementation is supposed to only work for non-hierarchical items, such as properties and references.
// hierarchical items must override it.
if (recursive) {
accept(visitor);
} else {
visitor.visit(this);
}
}
public void filterValues(Function<V, Boolean> function) {
Iterator<V> iterator = values.iterator();
while (iterator.hasNext()) {
Boolean keep = function.apply(iterator.next());
if (keep == null || !keep) {
iterator.remove();
}
}
}
public void applyDefinition(D definition) throws SchemaException {
applyDefinition(definition, true);
}
public void applyDefinition(D definition, boolean force) throws SchemaException {
checkMutability(); // TODO consider if there is real change
if (definition != null) {
checkDefinition(definition);
}
if (this.prismContext == null && definition != null) {
this.prismContext = definition.getPrismContext();
}
this.definition = definition;
for (PrismValue pval: getValues()) {
pval.applyDefinition(definition, force);
}
}
public void revive(PrismContext prismContext) throws SchemaException {
// it is necessary to do e.g. PolyString recomputation even if PrismContext is set
if (this.prismContext == null) {
this.prismContext = prismContext;
if (definition != null) {
definition.revive(prismContext);
}
}
for (V value: values) {
value.revive(prismContext);
}
}
public abstract Item clone();
protected void copyValues(Item clone) {
clone.elementName = this.elementName;
clone.definition = this.definition;
clone.prismContext = this.prismContext;
// Do not clone parent so the cloned item can be safely placed to
// another item
clone.parent = null;
clone.userData = MiscUtil.cloneMap(this.userData);
clone.incomplete = this.incomplete;
// Also do not copy 'immutable' flag so the clone is free to be modified
}
protected void propagateDeepCloneDefinition(boolean ultraDeep, D clonedDefinition) {
// nothing to do by default
}
public static <T extends Item> Collection<T> cloneCollection(Collection<T> items) {
Collection<T> clones = new ArrayList<T>(items.size());
for (T item: items) {
clones.add((T)item.clone());
}
return clones;
}
/**
* Sets all parents to null. This is good if the items are to be "transplanted" into a
* different Containerable.
*/
public static <T extends Item> Collection<T> resetParentCollection(Collection<T> items) {
for (T item: items) {
item.setParent(null);
}
return items;
}
public static <T extends Item> T createNewDefinitionlessItem(QName name, Class<T> type, PrismContext prismContext) {
T item = null;
try {
Constructor<T> constructor = type.getConstructor(QName.class);
item = constructor.newInstance(name);
if (prismContext != null) {
item.revive(prismContext);
}
} catch (Exception e) {
throw new SystemException("Error creating new definitionless "+type.getSimpleName()+": "+e.getClass().getName()+" "+e.getMessage(),e);
}
return item;
}
public void checkConsistence(boolean requireDefinitions, ConsistencyCheckScope scope) {
checkConsistenceInternal(this, requireDefinitions, false, scope);
}
public void checkConsistence(boolean requireDefinitions, boolean prohibitRaw) {
checkConsistenceInternal(this, requireDefinitions, prohibitRaw, ConsistencyCheckScope.THOROUGH);
}
public void checkConsistence(boolean requireDefinitions, boolean prohibitRaw, ConsistencyCheckScope scope) {
checkConsistenceInternal(this, requireDefinitions, prohibitRaw, scope);
}
public void checkConsistence() {
checkConsistenceInternal(this, false, false, ConsistencyCheckScope.THOROUGH);
}
public void checkConsistence(ConsistencyCheckScope scope) {
checkConsistenceInternal(this, false, false, scope);
}
public void checkConsistenceInternal(Itemable rootItem, boolean requireDefinitions, boolean prohibitRaw, ConsistencyCheckScope scope) {
ItemPath path = getPath();
if (elementName == null) {
throw new IllegalStateException("Item "+this+" has no name ("+path+" in "+rootItem+")");
}
if (definition != null) {
checkDefinition(definition);
} else if (requireDefinitions && !isRaw()) {
throw new IllegalStateException("No definition in item "+this+" ("+path+" in "+rootItem+")");
}
for (V val: values) {
if (prohibitRaw && val.isRaw()) {
throw new IllegalStateException("Raw value "+val+" in item "+this+" ("+path+" in "+rootItem+")");
}
if (val == null) {
throw new IllegalStateException("Null value in item "+this+" ("+path+" in "+rootItem+")");
}
if (val.getParent() == null) {
throw new IllegalStateException("Null parent for value "+val+" in item "+this+" ("+path+" in "+rootItem+")");
}
if (val.getParent() != this) {
throw new IllegalStateException("Wrong parent for value "+val+" in item "+this+" ("+path+" in "+rootItem+"), "+
"bad parent: " + val.getParent());
}
val.checkConsistenceInternal(rootItem, requireDefinitions, prohibitRaw, scope);
}
}
protected abstract void checkDefinition(D def);
public void assertDefinitions() throws SchemaException {
assertDefinitions("");
}
public void assertDefinitions(String sourceDescription) throws SchemaException {
assertDefinitions(false, sourceDescription);
}
public void assertDefinitions(boolean tolarateRawValues, String sourceDescription) throws SchemaException {
if (tolarateRawValues && isRaw()) {
return;
}
if (definition == null) {
throw new SchemaException("No definition in "+this+" in "+sourceDescription);
}
}
/**
* Returns true is all the values are raw.
*/
public boolean isRaw() {
for (V val: getValues()) {
if (!val.isRaw()) {
return false;
}
}
return true;
}
/**
* Returns true is at least one of the values is raw.
*/
public boolean hasRaw() {
for (V val: getValues()) {
if (val.isRaw()) {
return true;
}
}
return false;
}
public boolean isEmpty() {
return getValues().isEmpty();
}
@Override
public int hashCode() {
int valuesHash = 0;
if (values != null) {
valuesHash = MiscUtil.unorderedCollectionHashcode(values, null);
}
if (valuesHash == 0) {
// empty or non-significant container. We do not want this to destroy hashcode of
// parent item
return 0;
}
final int prime = 31;
int result = 1;
result = prime * result + ((elementName == null) ? 0 : elementName.hashCode());
result = prime * result + valuesHash;
return result;
}
public boolean equalsRealValue(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item<?,?> other = (Item<?,?>) obj;
if (elementName == null) {
if (other.elementName != null)
return false;
} else if (!elementName.equals(other.elementName))
return false;
// Do not compare parent at all. This is not relevant.
if (values == null) {
if (other.values != null)
return false;
} else if (!equalsRealValues(this.values, other.values))
return false;
return true;
}
private boolean equalsRealValues(List<V> thisValue, List<?> otherValues) {
return MiscUtil.unorderedCollectionEquals(thisValue, otherValues,
(o1, o2) -> {
if (o1 instanceof PrismValue && o2 instanceof PrismValue) {
PrismValue v1 = (PrismValue)o1;
PrismValue v2 = (PrismValue)o2;
return v1.equalsRealValue(v2);
} else {
return false;
}
});
}
private boolean match(List<V> thisValue, List<?> otherValues) {
return MiscUtil.unorderedCollectionEquals(thisValue, otherValues,
(o1, o2) -> {
if (o1 instanceof PrismValue && o2 instanceof PrismValue) {
PrismValue v1 = (PrismValue)o1;
PrismValue v2 = (PrismValue)o2;
return v1.match(v2);
} else {
return false;
}
});
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item<?,?> other = (Item<?,?>) obj;
if (definition == null) {
if (other.definition != null)
return false;
} else if (!definition.equals(other.definition))
return false;
if (elementName == null) {
if (other.elementName != null)
return false;
} else if (!elementName.equals(other.elementName))
return false;
if (incomplete != other.incomplete) {
return false;
}
// Do not compare parent at all. This is not relevant.
if (values == null) {
if (other.values != null)
return false;
} else if (!MiscUtil.unorderedCollectionEquals(this.values, other.values))
return false;
return true;
}
public boolean match(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item<?,?> other = (Item<?,?>) obj;
if (definition == null) {
if (other.definition != null)
return false;
} else if (!definition.equals(other.definition))
return false;
if (elementName == null) {
if (other.elementName != null)
return false;
} else if (!elementName.equals(other.elementName))
return false;
if (incomplete != other.incomplete) {
return false;
}
// Do not compare parent at all. This is not relevant.
if (values == null) {
if (other.values != null)
return false;
} else if (!match(this.values, other.values))
return false;
return true;
}
/**
* Returns true if this item is metadata item that should be ignored
* for metadata-insensitive comparisons and hashCode functions.
*/
public boolean isMetadata() {
D def = getDefinition();
if (def != null) {
return def.isOperational();
} else {
return false;
}
}
@Override
public String toString() {
return getClass().getSimpleName() + "(" + PrettyPrinter.prettyPrint(getElementName()) + ")";
}
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
if (DebugUtil.isDetailedDebugDump()) {
sb.append(getDebugDumpClassName()).append(": ");
}
sb.append(DebugUtil.formatElementName(getElementName()));
return sb.toString();
}
/**
* Return a human readable name of this class suitable for logs.
*/
protected String getDebugDumpClassName() {
return "Item";
}
protected void appendDebugDumpSuffix(StringBuilder sb) {
if (incomplete) {
sb.append(" (incomplete)");
}
}
public boolean isImmutable() {
return immutable;
}
public void setImmutable(boolean immutable) {
this.immutable = immutable;
for (V value : getValues()) {
value.setImmutable(immutable);
}
}
protected void checkMutability() {
if (immutable) {
throw new IllegalStateException("An attempt to modify an immutable item: " + toString());
}
}