/
ParameterizedEquivalenceStrategy.java
283 lines (238 loc) · 10.7 KB
/
ParameterizedEquivalenceStrategy.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
/*
* Copyright (c) 2010-2018 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.prism.equivalence;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismValue;
import java.util.HashMap;
import java.util.Map;
/**
* Implementation of EquivalenceStrategy that uses a parametrization of built-in equals/hashCode/diff methods.
*
* These strategies are still in progress and (most probably) will be changed before 4.0 release.
*
* The difference between REAL_VALUE and IGNORE_METADATA is to be established yet.
*
* Basically, REAL_VALUE is oriented towards the effective content of the item or value.
* Contrary to IGNORE_METADATA it ignores element names and reference filters (if OID is present).
*/
public class ParameterizedEquivalenceStrategy implements EquivalenceStrategy {
/**
* The (almost) highest level of recognition. Useful e.g. for comparing values for the purpose of XML editing.
* Still, ignores e.g. definitions, parent objects, origin, immutability flag, etc.
*
* Corresponds to pre-4.0 flags ignoreMetadata = false, literal = true.
*/
public static final ParameterizedEquivalenceStrategy LITERAL;
/**
* As LITERAL but ignores XML namespace prefixes.
* Also fills-in default relation name if not present (when comparing reference values).
*
* Currently this is the default for equals/hashCode.
*
* Roughly corresponds to pre-4.0 flags ignoreMetadata = false, literal = false.
*/
public static final ParameterizedEquivalenceStrategy NOT_LITERAL;
/**
* Ignores metadata, typically operational items and values, container IDs, and origin information.
* However, takes OID-ful reference filters into account.
*
* Corresponds to pre-4.0 flags ignoreMetadata = true, literal = false.
*/
public static final ParameterizedEquivalenceStrategy IGNORE_METADATA;
/**
* Ignores metadata, typically operational items and values and origin information.
*
* Container IDs are taken into account only if they directly contradict each other, meaning both values being compared
* do have them and they are different.
*
* Currently this is the default for diff and for delta application.
*
* EXPERIMENTAL
*/
public static final ParameterizedEquivalenceStrategy IGNORE_METADATA_CONSIDER_DIFFERENT_IDS;
/**
* As IGNORE_METADATA, but takes XML namespace prefixes into account.
*
* It is not clear in which situations this should be needed. But we include it here for compatibility reasons.
* Historically it is used on a few places in midPoint.
*
* Corresponds to pre-4.0 flags ignoreMetadata = true, literal = true.
*/
public static final ParameterizedEquivalenceStrategy LITERAL_IGNORE_METADATA;
/**
* Compares the real content if prism structures.
* Corresponds to "equalsRealValue" method used in pre-4.0.
*
* It is to be seen if operational data should be considered in this mode (they are ignored now).
* So, currently this is the most lax way of determining equivalence.
*/
public static final ParameterizedEquivalenceStrategy REAL_VALUE;
/**
* As REAL_VALUE but treats values with different non-null IDs as not equivalent.
*
* EXPERIMENTAL
*/
public static final ParameterizedEquivalenceStrategy REAL_VALUE_CONSIDER_DIFFERENT_IDS;
public static final ParameterizedEquivalenceStrategy DEFAULT_FOR_EQUALS;
public static final ParameterizedEquivalenceStrategy DEFAULT_FOR_DIFF;
public static final ParameterizedEquivalenceStrategy DEFAULT_FOR_DELTA_APPLICATION;
private static final Map<String, String> NICE_NAMES = new HashMap<>();
static {
LITERAL = new ParameterizedEquivalenceStrategy();
LITERAL.literalDomComparison = true;
LITERAL.consideringOperationalData = true;
LITERAL.consideringContainerIds = true;
LITERAL.consideringDifferentContainerIds = true;
LITERAL.consideringValueOrigin = false;
LITERAL.consideringReferenceFilters = true;
LITERAL.compareElementNames = true;
putIntoNiceNames(LITERAL, "LITERAL");
NOT_LITERAL = new ParameterizedEquivalenceStrategy();
NOT_LITERAL.literalDomComparison = false;
NOT_LITERAL.consideringOperationalData = true;
NOT_LITERAL.consideringContainerIds = true;
NOT_LITERAL.consideringDifferentContainerIds = true;
NOT_LITERAL.consideringValueOrigin = false;
NOT_LITERAL.consideringReferenceFilters = true;
NOT_LITERAL.compareElementNames = true;
putIntoNiceNames(NOT_LITERAL, "NOT_LITERAL");
IGNORE_METADATA = new ParameterizedEquivalenceStrategy();
IGNORE_METADATA.literalDomComparison = false;
IGNORE_METADATA.consideringOperationalData = false;
IGNORE_METADATA.consideringContainerIds = false;
IGNORE_METADATA.consideringDifferentContainerIds = false;
IGNORE_METADATA.consideringValueOrigin = false;
IGNORE_METADATA.consideringReferenceFilters = true;
IGNORE_METADATA.compareElementNames = true; //???
putIntoNiceNames(IGNORE_METADATA, "IGNORE_METADATA");
IGNORE_METADATA_CONSIDER_DIFFERENT_IDS = new ParameterizedEquivalenceStrategy();
IGNORE_METADATA_CONSIDER_DIFFERENT_IDS.literalDomComparison = false;
IGNORE_METADATA_CONSIDER_DIFFERENT_IDS.consideringOperationalData = false;
IGNORE_METADATA_CONSIDER_DIFFERENT_IDS.consideringContainerIds = false;
IGNORE_METADATA_CONSIDER_DIFFERENT_IDS.consideringDifferentContainerIds = true;
IGNORE_METADATA_CONSIDER_DIFFERENT_IDS.consideringValueOrigin = false;
IGNORE_METADATA_CONSIDER_DIFFERENT_IDS.consideringReferenceFilters = true;
IGNORE_METADATA_CONSIDER_DIFFERENT_IDS.compareElementNames = true; //???
putIntoNiceNames(IGNORE_METADATA_CONSIDER_DIFFERENT_IDS, "IGNORE_METADATA_CONSIDER_DIFFERENT_IDS");
LITERAL_IGNORE_METADATA = new ParameterizedEquivalenceStrategy();
LITERAL_IGNORE_METADATA.literalDomComparison = true;
LITERAL_IGNORE_METADATA.consideringOperationalData = false;
LITERAL_IGNORE_METADATA.consideringContainerIds = false;
LITERAL_IGNORE_METADATA.consideringDifferentContainerIds = false;
LITERAL_IGNORE_METADATA.consideringValueOrigin = false;
LITERAL_IGNORE_METADATA.consideringReferenceFilters = true;
LITERAL_IGNORE_METADATA.compareElementNames = true;
putIntoNiceNames(LITERAL_IGNORE_METADATA, "LITERAL_IGNORE_METADATA");
REAL_VALUE = new ParameterizedEquivalenceStrategy();
REAL_VALUE.literalDomComparison = false;
REAL_VALUE.consideringOperationalData = false;
REAL_VALUE.consideringContainerIds = false;
REAL_VALUE.consideringDifferentContainerIds = false;
REAL_VALUE.consideringValueOrigin = false;
REAL_VALUE.consideringReferenceFilters = false;
REAL_VALUE.compareElementNames = false;
putIntoNiceNames(REAL_VALUE, "REAL_VALUE");
REAL_VALUE_CONSIDER_DIFFERENT_IDS = new ParameterizedEquivalenceStrategy();
REAL_VALUE_CONSIDER_DIFFERENT_IDS.literalDomComparison = false;
REAL_VALUE_CONSIDER_DIFFERENT_IDS.consideringOperationalData = false;
REAL_VALUE_CONSIDER_DIFFERENT_IDS.consideringContainerIds = false;
REAL_VALUE_CONSIDER_DIFFERENT_IDS.consideringDifferentContainerIds = true;
REAL_VALUE_CONSIDER_DIFFERENT_IDS.consideringValueOrigin = false;
REAL_VALUE_CONSIDER_DIFFERENT_IDS.consideringReferenceFilters = false;
REAL_VALUE_CONSIDER_DIFFERENT_IDS.compareElementNames = false;
putIntoNiceNames(REAL_VALUE_CONSIDER_DIFFERENT_IDS, "REAL_VALUE_CONSIDER_DIFFERENT_IDS");
DEFAULT_FOR_EQUALS = NOT_LITERAL;
DEFAULT_FOR_DIFF = IGNORE_METADATA_CONSIDER_DIFFERENT_IDS;
DEFAULT_FOR_DELTA_APPLICATION = IGNORE_METADATA_CONSIDER_DIFFERENT_IDS;
}
private static void putIntoNiceNames(ParameterizedEquivalenceStrategy strategy, String name) {
NICE_NAMES.put(strategy.getDescription(), name);
}
private boolean literalDomComparison; // L
private boolean consideringOperationalData; // O
private boolean consideringContainerIds; // I
private boolean consideringDifferentContainerIds; // i
private boolean consideringValueOrigin; // o
private boolean consideringReferenceFilters; // F
private boolean compareElementNames; // E
public String getDescription() {
return (literalDomComparison ? "L" : "-") +
(consideringOperationalData ? "O" : "-") +
(consideringContainerIds ? "I" : "-") +
(consideringDifferentContainerIds ? "i" : "-") +
(consideringValueOrigin ? "o" : "-") +
(consideringReferenceFilters ? "F" : "-") +
(compareElementNames ? "E" : "-");
}
@Override
public boolean equals(Item<?, ?> first, Item<?, ?> second) {
return first == second || first != null && second != null && first.equals(second, this);
}
@Override
public boolean equals(PrismValue first, PrismValue second) {
return first == second || first != null && second != null && first.equals(second, this);
}
@Override
public int hashCode(Item<?, ?> item) {
return item != null ? item.hashCode(this) : 0;
}
@Override
public int hashCode(PrismValue value) {
return value != null ? value.hashCode(this) : 0;
}
public boolean isConsideringDefinitions() {
return false;
}
public boolean isConsideringElementNames() {
return compareElementNames;
}
public void setCompareElementNames(boolean compareElementNames) {
this.compareElementNames = compareElementNames;
}
public boolean isConsideringValueOrigin() {
return consideringValueOrigin;
}
public void setConsideringValueOrigin(boolean consideringValueOrigin) {
this.consideringValueOrigin = consideringValueOrigin;
}
public boolean isLiteralDomComparison() {
return literalDomComparison;
}
public void setLiteralDomComparison(boolean literalDomComparison) {
this.literalDomComparison = literalDomComparison;
}
public boolean isConsideringContainerIds() {
return consideringContainerIds;
}
public void setConsideringContainerIds(boolean consideringContainerIds) {
this.consideringContainerIds = consideringContainerIds;
}
public boolean isConsideringDifferentContainerIds() {
return consideringDifferentContainerIds;
}
public void setConsideringDifferentContainerIds(boolean consideringDifferentContainerIds) {
this.consideringDifferentContainerIds = consideringDifferentContainerIds;
}
public boolean isConsideringOperationalData() {
return consideringOperationalData;
}
public void setConsideringOperationalData(boolean consideringOperationalData) {
this.consideringOperationalData = consideringOperationalData;
}
public boolean isConsideringReferenceFilters() {
return consideringReferenceFilters;
}
public void setConsideringReferenceFilters(boolean consideringReferenceFilters) {
this.consideringReferenceFilters = consideringReferenceFilters;
}
@Override
public String toString() {
String desc = getDescription();
String niceName = NICE_NAMES.get(desc);
return niceName != null ? niceName + " (" + desc + ")" : "(" + desc + ")";
}
}