-
Notifications
You must be signed in to change notification settings - Fork 53
/
UnkeyedDiff.java
executable file
·453 lines (357 loc) · 17 KB
/
UnkeyedDiff.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
/*
* Copyright (c) 2016, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.hk2.xml.internal;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import org.glassfish.hk2.utilities.cache.CacheUtilities;
import org.glassfish.hk2.utilities.cache.Computable;
import org.glassfish.hk2.utilities.cache.ComputationErrorException;
import org.glassfish.hk2.utilities.cache.WeakCARCache;
import org.glassfish.hk2.utilities.reflection.Logger;
import org.glassfish.hk2.xml.internal.Differences.AddData;
import org.glassfish.hk2.xml.internal.Differences.Difference;
import org.glassfish.hk2.xml.internal.Differences.MoveData;
import org.glassfish.hk2.xml.internal.Differences.RemoveData;
import org.glassfish.hk2.xml.jaxb.internal.BaseHK2JAXBBean;
/**
* @author jwells
*
*/
public class UnkeyedDiff {
private final static String UNKEYED_DEBUG_PROPERTY = "org.jvnet.hk2.properties.xml.unkeyed.debug";
private final static boolean UNKEYED_DEBUG = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return Boolean.getBoolean(UNKEYED_DEBUG_PROPERTY);
}
});
private final ReentrantLock lock = new ReentrantLock();
private final List<BaseHK2JAXBBean> legacyList;
private final List<BaseHK2JAXBBean> proposedList;
private final ParentedModel parentModel;
private final BaseHK2JAXBBean parent;
private boolean computed = false;
private Differences finalSolution = null;
private HashMap<Integer, Differences> solution;
private HashSet<Integer> usedLegacy;
private HashSet<Integer> unusedLegacy;
private HashMap<Integer, Differences> proposedAdds;
private HashMap<Integer, SchrodingerSolution> quantumSolutions;
public UnkeyedDiff(List<BaseHK2JAXBBean> legacy, List<BaseHK2JAXBBean> proposed, BaseHK2JAXBBean parent, ParentedModel parentModel) {
if (legacy == null) legacy = Collections.emptyList();
if (proposed == null) proposed = Collections.emptyList();
this.legacyList = new ArrayList<BaseHK2JAXBBean>(legacy);
this.proposedList = new ArrayList<BaseHK2JAXBBean>(proposed);
this.parent = parent;
this.parentModel = parentModel;
}
private static List<BaseHK2JAXBBean> asList(Object a[]) {
if (a == null) return Collections.<BaseHK2JAXBBean>emptyList();
ArrayList<BaseHK2JAXBBean> retVal = new ArrayList<BaseHK2JAXBBean>(a.length);
for (Object o : a) {
retVal.add((BaseHK2JAXBBean) o);
}
return retVal;
}
public UnkeyedDiff(Object legacy[], Object proposed[], BaseHK2JAXBBean parent, ParentedModel parentModel) {
this(asList(legacy), asList(proposed), parent, parentModel);
}
public Differences compute() {
lock.lock();
try {
if (computed) return finalSolution;
Differences retVal = null;
try {
retVal = internalCompute();
}
finally {
if (retVal != null) {
finalSolution = retVal;
computed = true;
}
}
return retVal;
} finally {
lock.unlock();
}
}
private Differences internalCompute() {
Differences retVal = new Differences();
boolean needsChangeOfList = false;
DifferenceTable table = new DifferenceTable();
// First step, calculate the diagonal
boolean diagonalChange = !table.calculateDiagonal();
if (!diagonalChange) {
if (legacyList.size() == proposedList.size()) {
// Exactly the same lists
return retVal;
}
// We are either adding to the end or removing from the end
if (legacyList.size() < proposedList.size()) {
// Adds to the end
for (int lcv = legacyList.size(); lcv < proposedList.size(); lcv++) {
Difference d = new Difference(parent);
d.addAdd(parentModel.getChildXmlTag(), new AddData(proposedList.get(lcv), lcv));
retVal.addDifference(d);
}
return retVal;
}
// Removes from the end, proposed.size < legacy.size
for (int lcv = proposedList.size(); lcv < legacyList.size(); lcv++) {
String xmlTag = parentModel.getChildXmlTag();
Difference d = new Difference(parent);
d.addRemove(xmlTag, new RemoveData(xmlTag, lcv, legacyList.get(lcv)));
retVal.addDifference(d);
}
return retVal;
}
initializeSolution();
// Step one, find all diagonals that are exact, always prefer them over anything else
for (int lcv = 0; lcv < table.getDiagonalSize(); lcv++) {
Differences differences = table.getDiff(lcv, lcv);
if (differences.getDifferences().isEmpty()) {
// This is the best solution for this slot
addSolution(lcv, lcv, differences);
}
}
// Step two, match up any other exact matches
for (int proposedIndex = 0; proposedIndex < proposedList.size(); proposedIndex++) {
if (solution.containsKey(proposedIndex)) {
if (UNKEYED_DEBUG) {
Logger.getLogger().debug("Skipping proposedIndex " + proposedIndex + " since it has already has a solution");
}
continue;
}
int currentBestDiffIndex = -1;
Differences currentBestDiffs = null;
for (int legacyIndex = 0; legacyIndex < legacyList.size(); legacyIndex++) {
if (usedLegacy.contains(legacyIndex)) {
if (UNKEYED_DEBUG) {
Logger.getLogger().debug("Skipping legacyIndex " + legacyIndex + " for proposedIndex " +
proposedIndex + " since it has already has already been used");
}
continue;
}
Differences currentDiffs = table.getDiff(legacyIndex, proposedIndex);
if (currentBestDiffs == null || (currentBestDiffs.getDifferenceCost() > currentDiffs.getDifferenceCost())) {
currentBestDiffs = currentDiffs;
currentBestDiffIndex = legacyIndex;
if (currentDiffs.getDifferences().isEmpty()) {
needsChangeOfList = true;
break;
}
}
}
if (currentBestDiffs == null) {
// We need to add this proposed bean
Differences addMeDifference = new Differences();
Difference difference = new Difference(parent);
difference.addAdd(parentModel.getChildXmlTag(), new AddData(proposedList.get(proposedIndex), proposedIndex));
addMeDifference.addDifference(difference);
needsChangeOfList = true;
addSolution(-1, proposedIndex, addMeDifference);
}
else {
if (currentBestDiffs.getDifferences().isEmpty()) {
// Will never get better than a move
needsChangeOfList = true;
Differences moveMeDifferences = new Differences();
Difference difference = new Difference(parent);
difference.addMove(parentModel.getChildXmlTag(), new MoveData(currentBestDiffIndex, proposedIndex));
moveMeDifferences.addDifference(difference);
addSolution(currentBestDiffIndex, proposedIndex, moveMeDifferences);
}
else {
BaseHK2JAXBBean currentProposed = proposedList.get(proposedIndex);
Differences proposedAdd = proposedAdds.get(proposedIndex);
if (proposedAdd == null) {
proposedAdd = new Differences();
Difference d = new Difference(parent);
d.addAdd(parentModel.getChildXmlTag(), new AddData(currentProposed, proposedIndex));
proposedAdd.addDifference(d);
proposedAdds.put(proposedIndex, proposedAdd);
}
SchrodingerSolution sd = new SchrodingerSolution(currentBestDiffIndex, currentBestDiffs, proposedAdd);
quantumSolutions.put(proposedIndex, sd);
}
}
}
for (Map.Entry<Integer, SchrodingerSolution> entry : quantumSolutions.entrySet()) {
int proposedIndex = entry.getKey();
SchrodingerSolution sd = entry.getValue();
int legacyIndex = sd.legacyIndexOfDiff;
if (usedLegacy.contains(legacyIndex)) {
// No choice, the old best solution is no longer available
needsChangeOfList = true;
addSolution(-1, proposedIndex, sd.proposedAddDifference);
}
else if (unusedLegacy.contains(legacyIndex)) {
// The cost of the add solution now must also contain the cost of the remove of the unused
BaseHK2JAXBBean legacy = legacyList.get(legacyIndex);
int removeCost = Utilities.calculateAddCost(legacy);
int totalAddCost = removeCost + sd.proposedAddDifference.getDifferenceCost();
if (totalAddCost >= sd.legacyDifference.getDifferenceCost()) {
// Change is better
addSolution(legacyIndex, proposedIndex, sd.legacyDifference);
}
else {
needsChangeOfList = true;
addSolution(-1, proposedIndex, sd.proposedAddDifference);
}
}
else {
// Can I get here?
throw new AssertionError("Should not be able to get here");
}
}
// Add all the changes
for (Differences diffs : solution.values()) {
retVal.merge(diffs);
}
// Now add all the removes
for (Integer legacyRemoveIndex : unusedLegacy) {
Difference d = new Difference(parent);
d.addRemove(parentModel.getChildXmlTag(),
new RemoveData(parentModel.getChildXmlTag(), legacyRemoveIndex, legacyList.get(legacyRemoveIndex)));
retVal.addDifference(d);
}
if (UNKEYED_DEBUG) {
Logger.getLogger().debug("needsChangeOfList=" + needsChangeOfList + " with outcome " + retVal);
}
return retVal;
}
private void initializeSolution() {
solution = new HashMap<Integer, Differences>();
usedLegacy = new HashSet<Integer>();
unusedLegacy = new HashSet<Integer>();
for (int lcv = 0; lcv < legacyList.size(); lcv++) {
unusedLegacy.add(lcv);
}
proposedAdds = new HashMap<Integer, Differences>();
quantumSolutions = new HashMap<Integer, SchrodingerSolution>();
}
private void addSolution(int legacyIndex, int proposedIndex, Differences minimum) {
solution.put(proposedIndex, minimum);
if (legacyIndex >= 0) {
usedLegacy.add(legacyIndex);
unusedLegacy.remove(legacyIndex);
}
}
private class DifferenceTable {
private final int min;
private final WeakCARCache<TableKey, Differences> table;
private DifferenceTable() {
min = Math.min(legacyList.size(), proposedList.size());
int tableSize = legacyList.size() * proposedList.size();
table = CacheUtilities.createWeakCARCache(new DifferenceMaker(),
tableSize, false);
}
private boolean calculateDiagonal() {
boolean theSame = true;
for (int lcv = 0; lcv < min; lcv++) {
TableKey diagonalKey = new TableKey(lcv, lcv);
Differences differences = table.compute(diagonalKey);
if (!differences.getDifferences().isEmpty()) {
theSame = false;
}
}
return theSame;
}
private Differences getDiff(int legacyIndex, int proposedIndex) {
return table.compute(new TableKey(legacyIndex, proposedIndex));
}
private int getDiagonalSize() {
return min;
}
@Override
public String toString() {
return "DifferenceTable(min=" + min + " legacySize=" + legacyList.size() + " proposedSize=" + proposedList.size() + "," + System.identityHashCode(this) + ")";
}
}
private class DifferenceMaker implements Computable<TableKey, Differences> {
@Override
public Differences compute(TableKey key)
throws ComputationErrorException {
BaseHK2JAXBBean legacyBean = legacyList.get(key.legacyIndex);
BaseHK2JAXBBean proposedBean = proposedList.get(key.proposedIndex);
Differences retVal = Utilities.getDiff(legacyBean, proposedBean);
return retVal;
}
@Override
public String toString() {
return "DifferenceMaker(" + System.identityHashCode(this) + ")";
}
}
private static class TableKey {
private final int legacyIndex;
private final int proposedIndex;
private final int hash;
private TableKey(int legacyIndex, int proposedIndex) {
this.legacyIndex = legacyIndex;
this.proposedIndex = proposedIndex;
hash = legacyIndex ^ proposedIndex;
}
@Override
public int hashCode() {
return hash;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof TableKey)) return false;
TableKey other = (TableKey) o;
return (other.legacyIndex == legacyIndex) && (other.proposedIndex == proposedIndex);
}
@Override
public String toString() {
return "TableKey(" + legacyIndex + "," + proposedIndex + "," + System.identityHashCode(this) + ")";
}
}
/**
* Schrodinger solution because it isn't one or the other until later
* when we know more about the full solution
*
* @author jwells
*
*/
private static class SchrodingerSolution {
/*
* Solution 0: The legacy modification
*/
private final int legacyIndexOfDiff;
private final Differences legacyDifference;
/*
* Solution 1: The proposed add modification
*/
private final Differences proposedAddDifference;
private SchrodingerSolution(int legacyIndexOfDiff, Differences legacyDifference, Differences proposedAddDifference) {
this.legacyIndexOfDiff = legacyIndexOfDiff;
this.legacyDifference = legacyDifference;
this.proposedAddDifference = proposedAddDifference;
}
@Override
public String toString() {
return "SchrodingerSolution(" + legacyIndexOfDiff + "," + legacyDifference + "," + proposedAddDifference + "," + System.identityHashCode(this) + ")";
}
}
}