Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c480d42607f5d1b41b2a2c74168da03363e1e49 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
/*******************************************************************************
 * Copyright (c) 2012, 2016 Obeo and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *     Stefan Dirix - bug 441172
 *     Philip Langer - add additional predicates
 *     Tanja Mayerhofer - bug 501864
 *******************************************************************************/
package org.eclipse.emf.compare.utils;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Predicates.or;
import static com.google.common.collect.Iterables.all;
import static com.google.common.collect.Iterables.any;
import static org.eclipse.emf.compare.ConflictKind.REAL;
import static org.eclipse.emf.compare.DifferenceKind.ADD;
import static org.eclipse.emf.compare.internal.utils.ComparisonUtil.isDeleteOrUnsetDiff;
import static org.eclipse.emf.compare.internal.utils.DiffUtil.getAllAtomicRefiningDiffs;
import static org.eclipse.emf.compare.internal.utils.DiffUtil.getAllRefiningDiffs;

import com.google.common.base.Predicate;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.emf.compare.AttributeChange;
import org.eclipse.emf.compare.Conflict;
import org.eclipse.emf.compare.ConflictKind;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.DifferenceKind;
import org.eclipse.emf.compare.DifferenceSource;
import org.eclipse.emf.compare.DifferenceState;
import org.eclipse.emf.compare.FeatureMapChange;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.ReferenceChange;
import org.eclipse.emf.compare.ResourceLocationChange;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EGenericType;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.FeatureMap;

/**
 * This class will provide a number of Predicates that can be used to retrieve particular {@link Diff}s from
 * an iterable.
 * 
 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
 */
public final class EMFComparePredicates {
	/**
	 * This can be used to test whether a given Diff has no conflict object associated.
	 * 
	 * @since 3.1
	 */
	public static final Predicate<? super Diff> WITHOUT_CONFLICT = new Predicate<Diff>() {
		public boolean apply(Diff input) {
			return input != null && input.getConflict() == null;
		}
	};

	/**
	 * This can be used to check whether a given diff is a containment reference change.
	 * 
	 * @since 3.1
	 */
	public static final Predicate<? super Diff> CONTAINMENT_REFERENCE_CHANGE = new Predicate<Diff>() {
		public boolean apply(Diff input) {
			return input instanceof ReferenceChange
					&& ((ReferenceChange)input).getReference().isContainment();
		}
	};

	/**
	 * Predicate used to know if the given EObject is an EGenericType without eTypeArguments. When an
	 * EGenericType has arguments, it seems that the mutually derived references are not applicable in this
	 * case.
	 * 
	 * @return true, if the given EObject is an EGenericType without eTypeArguments, false otherwise.
	 */
	public static final Predicate<? super EObject> IS_EGENERIC_TYPE_WITHOUT_PARAMETERS = new Predicate<EObject>() {
		public boolean apply(EObject input) {
			final boolean isEGenericWithoutParams;
			if (input instanceof EGenericType && ((EGenericType)input).getETypeArguments().isEmpty()) {
				if (input.eContainer() instanceof EGenericType) {
					EGenericType eGenericTypeContainer = (EGenericType)(input.eContainer());
					isEGenericWithoutParams = !(eGenericTypeContainer.getETypeArguments().contains(input)
							|| input.equals(eGenericTypeContainer.getELowerBound())
							|| input.equals(eGenericTypeContainer.getEUpperBound()));
				} else {
					isEGenericWithoutParams = true;
				}
			} else {
				isEGenericWithoutParams = false;
			}
			return isEGenericWithoutParams;
		}
	};

	/**
	 * This class does not need to be instantiated.
	 */
	private EMFComparePredicates() {
		// Hides default constructor
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the modification of a single-valued
	 * reference going by the given {@code referenceName} on an EObject which name matches
	 * {@code qualifiedName}. This can be used both on three-way and two-way Diffs : if three-way, we'll
	 * consider that the {@code fromQualifiedName} can be either one of the right or origin values, and the
	 * {@code toQualifiedName} to be either left or right. on two-way diffs however, {@code fromQualifiedName}
	 * can only be the right value, and {@code toQualifiedName} will be the left one.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present a ReferenceChange.
	 * @param referenceName
	 *            Name of the single-valued reference on which we expect a change.
	 * @param fromQualifiedName
	 *            The original value of this reference.
	 * @param toQualifiedName
	 *            The value to which this reference has been changed.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> changedReference(final String qualifiedName,
			final String referenceName, final String fromQualifiedName, final String toQualifiedName) {
		final Predicate<? super Diff> valuesMatch = new ReferenceValuesMatch(referenceName, fromQualifiedName,
				toQualifiedName);
		return and(ofKind(DifferenceKind.CHANGE), onEObject(qualifiedName), valuesMatch);
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the addition of a value in a
	 * multi-valued attribute going by {@code attributeName} on an EObject which name matches
	 * {@code qualifiedName}.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present an AttributeChange.
	 * @param attributeName
	 *            Name of the multi-valued attribute on which we expect a change.
	 * @param addedValue
	 *            The value we expect to have been added to this attribute.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> addedToAttribute(final String qualifiedName,
			final String attributeName, final Object addedValue) {
		// This is only meant for multi-valued attributes
		return and(ofKind(DifferenceKind.ADD), onEObject(qualifiedName),
				attributeValueMatch(attributeName, addedValue, true));
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the addition of a value in a
	 * multi-valued reference going by {@code referenceName} on an EObject which name matches
	 * {@code qualifiedName}.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present a ReferenceChange.
	 * @param referenceName
	 *            Name of the multi-valued reference on which we expect a change.
	 * @param addedQualifiedName
	 *            Qualified name of the EObject which we expect to have been added to this reference.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> addedToReference(final String qualifiedName,
			final String referenceName, final String addedQualifiedName) {
		// This is only meant for multi-valued references
		return and(ofKind(DifferenceKind.ADD), onEObject(qualifiedName),
				referenceValueMatch(referenceName, addedQualifiedName, true));
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the addition of a value in a
	 * multi-valued reference going by {@code referenceName} on an EObject which name matches
	 * {@code qualifiedName}.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present a ReferenceChange.
	 * @param referenceName
	 *            Name of the multi-valued reference on which we expect a change.
	 * @param addedQualifiedName
	 *            Qualified name of the EObject which we expect to have been added to this reference.
	 * @param featureDelegateForAddedName
	 *            The optional feature to define the name of the objects which we expect to have been added to
	 *            this reference. May be null.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> addedToReference(final String qualifiedName,
			final String referenceName, final String addedQualifiedName,
			final EStructuralFeature featureDelegateForAddedName) {
		// This is only meant for multi-valued references
		return and(ofKind(DifferenceKind.ADD), onEObject(qualifiedName),
				referenceValueMatch(referenceName, addedQualifiedName, true, featureDelegateForAddedName));
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the moving of a value within a
	 * multi-valued attribute going by {@code attributeName} on an EObject which name matches
	 * {@code qualifiedName}.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present an AttributeChange.
	 * @param attributeName
	 *            Name of the multi-valued attribute on which we expect a change.
	 * @param removedValue
	 *            Value which we expect to have been moved within this attribute.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> movedInAttribute(final String qualifiedName,
			final String attributeName, final Object removedValue) {
		// This is only meant for multi-valued attributes
		return and(ofKind(DifferenceKind.MOVE), onEObject(qualifiedName),
				attributeValueMatch(attributeName, removedValue, true));
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the moving of a value within a
	 * multi-valued reference going by {@code referenceName} on an EObject which name matches
	 * {@code qualifiedName}.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present a ReferenceChange.
	 * @param referenceName
	 *            Name of the multi-valued reference on which we expect a change.
	 * @param removedQualifiedName
	 *            Qualified name of the EObject which we expect to have been moved within this reference.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> movedInReference(final String qualifiedName,
			final String referenceName, final String removedQualifiedName) {
		// This is only meant for multi-valued references
		return and(ofKind(DifferenceKind.MOVE), onEObject(qualifiedName),
				referenceValueMatch(referenceName, removedQualifiedName, true));
	}

	/**
	 * This predicate can be used to check whether a given Diff is a {@link ReferenceChange} representing the
	 * eOpposite of the {@code diff} argument.
	 * 
	 * @param diff
	 *            The {@link ReferenceChange} against which is checked if an eOpposite relation exists.
	 * @return The created predicate.
	 * @since 3.2
	 */
	public static Predicate<Diff> isDiffOnEOppositeOf(final ReferenceChange diff) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input instanceof ReferenceChange
						&& diff.getReference().getEOpposite() == ((ReferenceChange)input).getReference();
			}
		};
	}

	/**
	 * This predicate can be used to check whether a given Diff is equivalent to the {@code diff} argument.
	 * 
	 * @param diff
	 *            The {@link ReferenceChange} against which is checked if an equivalence relation exists.
	 * @return The created predicate.
	 * @since 3.2
	 */
	public static Predicate<Diff> isEquivalentTo(final Diff diff) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input.getEquivalence() != null
						&& input.getEquivalence().getDifferences().contains(diff);
			}
		};
	}

	/**
	 * This predicate can be used to check whether a given Diff is a {@link ReferenceChange} with the same
	 * reference as the {@code diff} argument.
	 * 
	 * @param diff
	 *            The {@link ReferenceChange} against which is checked whether it has the same reference.
	 * @return The created predicate.
	 * @since 3.2
	 */
	public static Predicate<Diff> hasSameReferenceAs(final ReferenceChange diff) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input instanceof ReferenceChange
						&& diff.getReference() == ((ReferenceChange)input).getReference();
			}
		};
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the deletion of a value from a
	 * multi-valued attribute going by {@code attributeName} on an EObject which name matches
	 * {@code qualifiedName}.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present an AttributeChange.
	 * @param attributeName
	 *            Name of the multi-valued attribute on which we expect a change.
	 * @param removedValue
	 *            Value which we expect to have been removed from this attribute.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> removedFromAttribute(final String qualifiedName,
			final String attributeName, final Object removedValue) {
		// This is only meant for multi-valued attributes
		return and(ofKind(DifferenceKind.DELETE), onEObject(qualifiedName),
				attributeValueMatch(attributeName, removedValue, true));
	}

	/**
	 * Indicates whether a diff is part of a real add/add conflict.
	 * 
	 * @return a predicate to check if a diff belongs to an add/add conflict.
	 * @since 3.4
	 */
	public static Predicate<Diff> isInRealAddAddConflict() {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				Conflict conflict = input.getConflict();
				if (conflict != null) {
					if (conflict.getKind() != REAL) {
						return false;
					} else {
						if (all(conflict.getDifferences(), ofKind(ADD))) {
							return true;
						}
					}
				}
				return false;
			}
		};
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the deletion of a value from a
	 * multi-valued reference going by {@code referenceName} on an EObject which name matches
	 * {@code qualifiedName}.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present a ReferenceChange.
	 * @param referenceName
	 *            Name of the multi-valued reference on which we expect a change.
	 * @param removedQualifiedName
	 *            Qualified name of the EObject which we expect to have been removed from this reference.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> removedFromReference(final String qualifiedName,
			final String referenceName, final String removedQualifiedName) {
		// This is only meant for multi-valued references
		return and(ofKind(DifferenceKind.DELETE), onEObject(qualifiedName),
				referenceValueMatch(referenceName, removedQualifiedName, true));
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the deletion of a value from a
	 * multi-valued reference going by {@code referenceName} on an EObject which name matches
	 * {@code qualifiedName}.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present a ReferenceChange.
	 * @param referenceName
	 *            Name of the multi-valued reference on which we expect a change.
	 * @param removedQualifiedName
	 *            Qualified name of the EObject which we expect to have been removed from this reference.
	 * @param featureDelegateForRemovedName
	 *            The optional feature to define the name of the objects which we expect to have been removed
	 *            from this reference. May be null.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> removedFromReference(final String qualifiedName,
			final String referenceName, final String removedQualifiedName,
			final EStructuralFeature featureDelegateForRemovedName) {
		// This is only meant for multi-valued references
		return and(ofKind(DifferenceKind.DELETE), onEObject(qualifiedName), referenceValueMatch(referenceName,
				removedQualifiedName, true, featureDelegateForRemovedName));
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the modification of a single-valued
	 * attribute going by the given {@code attributeName} on an EObject which name matches
	 * {@code qualifiedName}. This can be used both on three-way and two-way Diffs : if three-way, we'll
	 * consider that the {@code fromValue} can be either one of the right or origin values, and the
	 * {@code toValue} to be either left or right. on two-way diffs however, {@code fromValue} can only be the
	 * right value, and {@code toValue} will be the left one.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a String
	 * for us to compare it with the given qualified name.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            Qualified name of the EObject which we expect to present an AttributeChange.
	 * @param attributeName
	 *            Name of the single-valued attribute on which we expect a change.
	 * @param fromValue
	 *            The original value of this attribute.
	 * @param toValue
	 *            The value to which this attribute has been changed.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> changedAttribute(final String qualifiedName,
			final String attributeName, final Object fromValue, final Object toValue) {
		final Predicate<? super Diff> valuesMatch = new AttributeValuesMatch(attributeName, fromValue,
				toValue);
		return and(ofKind(DifferenceKind.CHANGE), onEObject(qualifiedName), valuesMatch);
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the addition of an EObject matching
	 * the given qualified name. Namely, it will check that that Diff is a ReferenceChange, that one of its
	 * Match sides correspond to the given qualified name's ancestors, and that its value correspond to the
	 * given qualified name's last segment.
	 * <p>
	 * For example, {@code added("extlibrary.BookCategory.Encyclopedia")} will check that an EObject named
	 * "Encyclopedia" has been added under the container "extlibrary.BookCategory". Note that
	 * {@code added("emf.compare.Match")} will <b>not</b> match a difference on the EObject
	 * "org.eclipse.emf.compare.Match". The qualified name must be absolute.
	 * </p>
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a
	 * String.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            The qualified name of the EObject we expect to have been added.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> added(final String qualifiedName) {
		final int parentEndIndex = qualifiedName.lastIndexOf('.');
		if (parentEndIndex >= 0) {
			final String ancestors = qualifiedName.substring(0, parentEndIndex);
			final String objectName = qualifiedName.substring(parentEndIndex + 1);
			return and(ofKind(DifferenceKind.ADD), onEObject(ancestors), valueNameMatches(objectName));
		}
		return and(valueNameMatches(qualifiedName), ofKind(DifferenceKind.ADD));
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the move of an EObject matching the
	 * given qualified name. Namely, it will check that that Diff is a ReferenceChange, that one of its Match
	 * sides correspond to the given qualified name's ancestors, and that its value correspond to the given
	 * qualified name's last segment.
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a
	 * String.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            The qualified name of the EObject we expect to have been moved.
	 * @param referenceName
	 *            Name of the reference in which we expect a child to have been added.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> moved(final String qualifiedName, final String referenceName) {
		final int parentEndIndex = qualifiedName.lastIndexOf('.');
		if (parentEndIndex >= 0) {
			final String ancestors = qualifiedName.substring(0, parentEndIndex);
			final String objectName = qualifiedName.substring(parentEndIndex + 1);
			return and(ofKind(DifferenceKind.MOVE), onEObject(ancestors), onFeature(referenceName),
					valueNameMatches(objectName));
		}
		return and(ofKind(DifferenceKind.MOVE), valueNameMatches(qualifiedName), onFeature(referenceName));
	}

	/**
	 * This predicate can be used to check whether a given Diff represents the removal of an EObject matching
	 * the given qualified name. Namely, it will check that that Diff is a ReferenceChange, that one of its
	 * Match sides correspond to the given qualified name's ancestors, and that its value correspond to the
	 * given qualified name's last segment.
	 * <p>
	 * For example, {@code removed("extlibrary.BookCategory.Encyclopedia")} will check that an EObject named
	 * "Encyclopedia" has been removed from the container "extlibrary.BookCategory". Note that
	 * {@code removed("emf.compare.Match")} will <b>not</b> match a difference on the EObject
	 * "org.eclipse.emf.compare.Match". The qualified name must be absolute.
	 * </p>
	 * <p>
	 * Note that in order for this to work, we expect the EObjects to have a "name" feature returning a
	 * String.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            The qualified name of the EObject we expect to have been removed.
	 * @return The created predicate.
	 */
	@SuppressWarnings("unchecked")
	public static Predicate<? super Diff> removed(final String qualifiedName) {
		final int parentEndIndex = qualifiedName.lastIndexOf('.');
		if (parentEndIndex >= 0) {
			final String ancestors = qualifiedName.substring(0, parentEndIndex);
			final String objectName = qualifiedName.substring(parentEndIndex + 1);
			return and(ofKind(DifferenceKind.DELETE), onEObject(ancestors), valueNameMatches(objectName));
		}
		return and(valueNameMatches(qualifiedName), ofKind(DifferenceKind.DELETE));
	}

	/**
	 * This can be used to check that a given Diff correspond to either an {@link AttributeChange}, a
	 * {@link FeatureMapChange} or a {@link ReferenceChange}, and that the corresponding reference or
	 * attribute matches the given {@code featureName}.
	 * 
	 * @param featureName
	 *            Name of the feature on which we expect a change.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> onFeature(final String featureName) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				final EStructuralFeature affectedFeature;
				if (input instanceof AttributeChange) {
					affectedFeature = ((AttributeChange)input).getAttribute();
				} else if (input instanceof ReferenceChange) {
					affectedFeature = ((ReferenceChange)input).getReference();
				} else if (input instanceof FeatureMapChange) {
					affectedFeature = ((FeatureMapChange)input).getAttribute();
				} else {
					return false;
				}
				return featureName.equals(affectedFeature.getName());
			}
		};
	}

	/**
	 * Accept only diffs that inherit either AttributeChange, ReferenceChange, or FeatureMapChange that
	 * concern the given feature.
	 * 
	 * @param feature
	 *            Feature to deal with
	 * @return a new predicate that accepts diffs that concern the given feature.
	 */
	public static Predicate<Diff> onFeature(EStructuralFeature feature) {
		return new OnFeature(feature);
	}

	/**
	 * This can be used to check that a given Diff originates from the given {@code source} side.
	 * 
	 * @param source
	 *            The side from which we expect this diff to originate.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> fromSide(final DifferenceSource source) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input != null && input.getSource() == source;
			}
		};
	}

	/**
	 * This can be used to check that a given Diff originates from the given {@code source} side.
	 * 
	 * @param diff
	 *            The diff the side of which will be used to filter.
	 * @return The created predicate.
	 */
	public static Predicate<Diff> sameSideAs(final Diff diff) {
		if (diff == null) {
			throw new IllegalArgumentException();
		}
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input != null && input.getSource() == diff.getSource();
			}
		};
	}

	/**
	 * This can be used in order to check that a Diff has been detected on the given EObject.
	 * 
	 * @param eObject
	 *            The EObject which we expect the diff to concern.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> onEObject(final EObject eObject) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				if (input == null) {
					return false;
				}

				final Match match = input.getMatch();
				return match.getLeft() == eObject || match.getRight() == eObject
						|| match.getOrigin() == eObject;
			}
		};
	}

	/**
	 * This can be used in order to check whether a Diff has been detected on an EObject matching the given
	 * qualified name.
	 * <p>
	 * For this to work, we expect the EObjects to have a feature named "name" returning a String.
	 * </p>
	 * 
	 * @param qualifiedName
	 *            The qualified name of the EObject we expect that diff to concern.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> onEObject(final String qualifiedName) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				if (input == null || input instanceof ResourceLocationChange) {
					return false;
				}

				final Match match = input.getMatch();
				return match(match.getLeft(), qualifiedName, null)
						|| match(match.getRight(), qualifiedName, null)
						|| match(match.getOrigin(), qualifiedName, null);
			}
		};
	}

	/**
	 * This can be used in order to check whether a Diff has been detected on an EObject matching the given
	 * qualified name or the qualified name under the given feature.
	 * <p>
	 * For this to work, we expect the EObjects to have a feature named "name" returning a String or to have
	 * the given feature (String or EObject with a feature named "name").
	 * </p>
	 * 
	 * @param qualifiedName
	 *            The qualified name of the EObject we expect that diff to concern.
	 * @param featureDelegate
	 *            The optional feature to define the name of the objects. May be null.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> onEObject(final String qualifiedName,
			final EStructuralFeature featureDelegate) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				if (input == null) {
					return false;
				}

				final Match match = input.getMatch();
				return match(match.getLeft(), qualifiedName, featureDelegate)
						|| match(match.getRight(), qualifiedName, featureDelegate)
						|| match(match.getOrigin(), qualifiedName, featureDelegate);
			}
		};
	}

	/**
	 * This predicate can be used to check whether a particular diff is of the given {@code kind}. This is
	 * mainly used to differentiate additions from deletions.
	 * 
	 * @param kind
	 *            The kind we expect this diff to have.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> ofKind(final DifferenceKind kind) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input != null && input.getKind() == kind;
			}
		};
	}

	/**
	 * Accept only diffs of the given kinds.
	 * 
	 * @param kind1
	 *            first kind of diff to accept
	 * @param kind2
	 *            second kind of diff to accept
	 * @return The created predicate.
	 */
	public static Predicate<Diff> ofKind(final DifferenceKind kind1, final DifferenceKind kind2) {
		checkNotNull(kind1);
		checkNotNull(kind2);
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input != null && (input.getKind() == kind1 || input.getKind() == kind2);
			}
		};
	}

	/**
	 * Accept only diffs whose value matches the given value.
	 * 
	 * @param helper
	 *            The helper to match values
	 * @param value
	 *            The value to match
	 * @return The created predicate.
	 */
	public static Predicate<Diff> valueMatches(final IEqualityHelper helper, final Object value) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				if (input instanceof ReferenceChange) {
					return helper.matchingValues(value, ((ReferenceChange)input).getValue());
				} else if (input instanceof AttributeChange) {
					return helper.matchingValues(value, ((AttributeChange)input).getValue());
				} else if (input instanceof FeatureMapChange) {
					return helper.matchingValues(value,
							((FeatureMap.Entry)((FeatureMapChange)input).getValue()).getValue());
				}
				return false;
			}
		};
	}

	/**
	 * This predicate can be used in order to check that a particular Diff describes either a
	 * {@link ReferenceChange}, {@link AttributeChange} or {@link FeatureMapChange} for the given
	 * {@code expectedValue}.
	 * <p>
	 * For example, this could be used to check that the given value has indeed been added to a reference or
	 * attribute, though such checks are more easily performed through {@link #addedIn(EObject, EObject)} or
	 * {@link #removedFrom(EObject, EObject)}.
	 * </p>
	 * 
	 * @param expectedValue
	 *            The value which we expect to have changed and detected through a Diff.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> valueIs(final Object expectedValue) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				final Object value;
				if (input instanceof ReferenceChange) {
					value = ((ReferenceChange)input).getValue();
				} else if (input instanceof AttributeChange) {
					value = ((AttributeChange)input).getValue();
				} else if (input instanceof FeatureMapChange) {
					value = ((FeatureMapChange)input).getValue();
				} else {
					return false;
				}

				return value == expectedValue || (value != null && value.equals(expectedValue));
			}
		};
	}

	/**
	 * This predicate can be used to check whether a given Diff describes an AttributeChange with the given
	 * {@code attributeName} and which changed value corresponds to the given {@code expectedValue}.
	 * 
	 * @param attributeName
	 *            The name of the attribute for which we seek an AttributeChange.
	 * @param expectedValue
	 *            The value we expect to correspond to this AttributeChange.
	 * @param multiValued
	 *            Tells us to check for either multi- or single-valued reference changes.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> attributeValueMatch(final String attributeName,
			final Object expectedValue, final boolean multiValued) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				if (input instanceof AttributeChange
						&& ((AttributeChange)input).getAttribute().getName().equals(attributeName)
						&& ((AttributeChange)input).getAttribute().isMany() == multiValued) {
					final Object value = ((AttributeChange)input).getValue();
					return input.getMatch().getComparison().getEqualityHelper().matchingAttributeValues(value,
							expectedValue);
				}
				return false;
			}
		};
	}

	/**
	 * This predicate can be used to check whether a given Diff describes a ReferenceChange with the given
	 * {@code referenceName} and which changed value corresponds to the given {@code qualifiedName}.
	 * <p>
	 * For this to work, we expect the EObject to have a feature named "name" returning a String for us to try
	 * and match it.
	 * </p>
	 * 
	 * @param referenceName
	 *            The reference for which we seek a ReferenceChange.
	 * @param qualifiedName
	 *            The qualified name of the EObject on which we detected a change.
	 * @param multiValued
	 *            Tells us to check for either multi- or single-valued reference changes.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> referenceValueMatch(final String referenceName,
			final String qualifiedName, final boolean multiValued) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				if (input instanceof ReferenceChange
						&& ((ReferenceChange)input).getReference().getName().equals(referenceName)
						&& ((ReferenceChange)input).getReference().isMany() == multiValued) {
					final EObject value = ((ReferenceChange)input).getValue();
					return qualifiedName != null && match(value, qualifiedName, null);
				}
				return false;
			}
		};
	}

	/**
	 * This predicate can be used to check whether a given Diff describes a ReferenceChange with the given
	 * {@code referenceName} and which changed value corresponds to the given {@code qualifiedName} or the
	 * qualified name under the given {@code featureDelegate}.
	 * <p>
	 * For this to work, we expect the EObject to have a feature named "name" returning a String or to have
	 * the given feature (String or EObject with a feature named "name") for us to try and match it.
	 * </p>
	 * 
	 * @param referenceName
	 *            The reference for which we seek a ReferenceChange.
	 * @param qualifiedName
	 *            The qualified name of the EObject on which we detected a change.
	 * @param multiValued
	 *            Tells us to check for either multi- or single-valued reference changes.
	 * @param featureDelegate
	 *            The optional feature to define the name of the objects. May be null.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> referenceValueMatch(final String referenceName,
			final String qualifiedName, final boolean multiValued, final EStructuralFeature featureDelegate) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				if (input instanceof ReferenceChange
						&& ((ReferenceChange)input).getReference().getName().equals(referenceName)
						&& ((ReferenceChange)input).getReference().isMany() == multiValued) {
					final EObject value = ((ReferenceChange)input).getValue();
					return qualifiedName != null && match(value, qualifiedName, featureDelegate);
				}
				return false;
			}
		};
	}

	/**
	 * This can be used to check whether a given Diff describes either a {@link ReferenceChange} on an EObject
	 * which name is {@code expectedName}.
	 * <p>
	 * For this to work, we expect the EObject to have a feature named "name" returning a String for us to try
	 * and match it.
	 * </p>
	 * 
	 * @param expectedName
	 *            The name of the EObject which we expect as a changed reference value.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> valueNameMatches(final String expectedName) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				final EObject value;
				if (input instanceof ReferenceChange) {
					value = ((ReferenceChange)input).getValue();
				} else {
					return false;
				}
				return internalMatch(value, expectedName, null);
			}
		};
	}

	/**
	 * This can be used to check whether a given Diff describes either a {@link ReferenceChange} on an EObject
	 * which name is {@code expectedName} or which the given feature provides the {@code expectedName}.
	 * <p>
	 * For this to work, we expect the EObject to have a feature named "name" returning a String or to have
	 * the given feature (String or EObject with a feature named "name") for us to try and match it.
	 * </p>
	 * 
	 * @param expectedName
	 *            The name of the EObject which we expect as a changed reference value.
	 * @param featureDelegate
	 *            The optional feature to define the name of the objects. May be null.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> valueNameMatches(final String expectedName,
			final EStructuralFeature featureDelegate) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				final EObject value;
				if (input instanceof ReferenceChange) {
					value = ((ReferenceChange)input).getValue();
				} else {
					return false;
				}
				return internalMatch(value, expectedName, featureDelegate);
			}
		};
	}

	/**
	 * This can be used to check whether a given Diff has a conflict of one of the given type.
	 * 
	 * @param kinds
	 *            Type(s) of the conflict(s) we seek.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> hasConflict(final ConflictKind... kinds) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input != null && input.getConflict() != null
						&& Arrays.asList(kinds).contains(input.getConflict().getKind());
			}
		};
	}

	/**
	 * This can be used to check whether a given Diff is in (one of) the given state(s).
	 * 
	 * @param states
	 *            State(s) in which we need a Diff to be.
	 * @return The created predicate.
	 */
	public static Predicate<? super Diff> hasState(final DifferenceState... states) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input != null && Arrays.asList(states).contains(input.getState());
			}
		};
	}

	/**
	 * Predicate builder for diffs that can conflict with the given diff.
	 * 
	 * @param diff
	 *            The diff
	 * @return A predicate that accepts diffs that might conflict with the given diff.
	 */
	public static Predicate<Diff> possiblyConflictingWith(Diff diff) {
		return new ConflictCandidateFilter(diff);
	}

	/**
	 * This can be used to check whether a given Conflict is of one of the given kind.
	 * 
	 * @param kinds
	 *            Type(s) of the conflict(s) we seek.
	 * @return The created predicate.
	 */
	public static Predicate<? super Conflict> containsConflictOfTypes(final ConflictKind... kinds) {
		return new Predicate<Conflict>() {
			public boolean apply(Conflict input) {
				return input != null && input.getKind() != null
						&& Arrays.asList(kinds).contains(input.getKind());
			}
		};
	}

	/**
	 * This can be used to check whether a given diff is a containment reference change.
	 * 
	 * @return The created predicate.
	 * @deprecated use {@link #CONTAINMENT_REFERENCE_CHANGE};
	 */
	@Deprecated
	public static Predicate<? super Diff> containmentReferenceChange() {
		return CONTAINMENT_REFERENCE_CHANGE;
	}

	/**
	 * This will be used to check that a given {@link EObject} corresponds to the given {@code qualifiedName}.
	 * <p>
	 * For example, {@code match("extlibrary.BookCategory.Encyclopedia")} will return {@code true} for an
	 * EObject named "Encyclopedia" under the container "extlibrary.BookCategory". Note, however that
	 * {@code match("emf.compare.Match")} will <b>not</b> match the EObject "org.eclipse.emf.compare.Match".
	 * The qualified name must be absolute.
	 * </p>
	 * <p>
	 * For this to work, we expect the EObject to have a feature named "name" returning a String or to have
	 * the given feature (String or EObject with a feature named "name") for us to try and match it. See also
	 * {@link #getNameFeature(EObject)}.
	 * </p>
	 * 
	 * @param eObject
	 *            The EObject which qualified name we are to check.
	 * @param qualifiedName
	 *            The expected, <b>absolute</b> qualified name of the given {@code eObject}.
	 * @param featureDelegate
	 *            The optional feature to define the name of the objects. May be null.
	 * @return {@code true} if the given {@code eObject} matches the given {@code qualifiedName},
	 *         {@code false} if not, or if we could not determine the "name" feature of that EObject.
	 * @see #getNameFeature(EObject)
	 */
	private static boolean match(EObject eObject, String qualifiedName, EStructuralFeature featureDelegate) {
		if (eObject == null || qualifiedName == null || qualifiedName.length() == 0) {
			return false;
		}
		final String[] names = qualifiedName.split("\\."); //$NON-NLS-1$

		int current = names.length - 1;
		boolean matches = internalMatch(eObject, names[current--], featureDelegate);
		if (matches) {
			EObject container = eObject.eContainer();
			while (matches && container != null && current >= 0) {
				matches = internalMatch(container, names[current--], featureDelegate);
				container = container.eContainer();
			}
			// This qualified name does not match if there was still a container "above"
			// "emf.compare.Match" does not match the EObject "org.eclipse.emf.compare.Match"
			matches = matches && container == null;
		}

		return matches;
	}

	/**
	 * This will be used to check whether a given Object matches the given {@code qualifiedName}, considering
	 * {@code null} as legal values. Namely, this will return {@code true} in the following cases :
	 * <ul>
	 * <li>both {@code eObject} and {@code qualifiedName} are {@code null}</li>
	 * <li>eObject is an instance of {@link EObject} and its qualified name matches the given
	 * {@code qualifiedName} according to the semantics of {@link #match(EObject, String)}</li>
	 * </ul>
	 * 
	 * @param eObject
	 *            The Object which qualified name we are to check. May be {@code null}.
	 * @param qualifiedName
	 *            The expected, <b>absolute</b> qualified name of the given {@code eObject}. May be
	 *            {@code null}.
	 * @return {@code true} if the given {@code eObject} matches the given {@code qualifiedName},
	 *         {@code false} if not, or if we could not determine the "name" feature of that EObject.
	 * @see #match(EObject, String)
	 */
	private static boolean matchAllowingNull(Object eObject, String qualifiedName) {
		if (eObject == null) {
			return qualifiedName == null;
		}
		return qualifiedName != null && eObject instanceof EObject
				&& match((EObject)eObject, qualifiedName, null);
	}

	/**
	 * Checks that the given {@code eObject}'s name is equal to {@code name}.
	 * <p>
	 * For this to work, we expect the EObject to have a feature named "name" returning a String or to have
	 * the given feature (String or EObject with a feature named "name") for us to try and match it. See also
	 * {@link #getNameFeature(EObject)}.
	 * </p>
	 * 
	 * @param eObject
	 *            the EObject which name we are to check.
	 * @param name
	 *            The expected name of {@code eObject}.
	 * @param featureDelegate
	 *            The optional feature to define the name of the objects. May be null.
	 * @return {@code true} if the given {@code eObject}'s name is equal to the given {@code name},
	 *         {@code false} if not, or if we could not determine the "name" feature of that EObject.
	 * @see #getNameFeature(EObject)
	 */
	private static boolean internalMatch(EObject eObject, String name, EStructuralFeature featureDelegate) {
		final EStructuralFeature nameFeature = getNameFeature(eObject);
		boolean match = false;
		if (nameFeature != null) {
			final Object featureValue = eObject.eGet(nameFeature);
			if (featureValue instanceof String) {
				match = featureValue.equals(name);
			}
		} else if (featureDelegate != null && !featureDelegate.isMany()) {
			final Object featureValue = ReferenceUtil.safeEGet(eObject, featureDelegate);
			if (featureValue instanceof String) {
				match = featureValue.equals(name);
			} else if (featureValue instanceof EObject) {
				match = internalMatch((EObject)featureValue, name, null);
			}
		}
		return match;
	}

	/**
	 * Tries and determine the "name" feature of the given EObject. By default, we only consider
	 * {@link ENamedElement#name} or a feature of the given {@code eObject}'s EClass which would be named
	 * "name".
	 * 
	 * @param eObject
	 *            The EObject for which we are trying to determine a name.
	 * @return The name feature of the given EObject if we could find one, {@code null} otherwise.
	 */
	private static EStructuralFeature getNameFeature(EObject eObject) {
		if (eObject instanceof ENamedElement) {
			return EcorePackage.eINSTANCE.getENamedElement_Name();
		}
		EStructuralFeature nameFeature = null;
		final Iterator<EStructuralFeature> features = eObject.eClass().getEAllStructuralFeatures().iterator();
		while (nameFeature == null && features.hasNext()) {
			final EStructuralFeature feature = features.next();
			if ("name".equals(feature.getName())) { //$NON-NLS-1$
				nameFeature = feature;
			}
		}
		return nameFeature;
	}

	/**
	 * This predicate can be used to check whether any refining diff of a given diff fulfills the given
	 * predicate.
	 * 
	 * @param predicate
	 *            The predicate to check.
	 * @return The predicate.
	 * @since 3.4
	 */
	public static Predicate<Diff> anyRefining(final Predicate<? super Diff> predicate) {
		return new Predicate<Diff>() {
			public boolean apply(Diff diff) {
				return diff != null && any(getAllRefiningDiffs(diff), predicate);
			}
		};
	}

	/**
	 * This predicate can be used to check whether any refined diff of a given diff fulfills the given
	 * predicate.
	 * 
	 * @param predicate
	 *            The predicate to check.
	 * @return The predicate.
	 * @since 3.4
	 */
	public static Predicate<Diff> anyRefined(final Predicate<? super Diff> predicate) {
		return new Predicate<Diff>() {
			public boolean apply(Diff input) {
				return input != null && any(input.getRefines(), predicate);
			}
		};
	}

	/**
	 * This predicate can be used to check whether a diff has refiningDiffs AND all these refining diffs
	 * fulfill the given predicate.
	 * <p>
	 * <b>BEWARE: If the given diff has no refining diff, the predicate returns <code>false</code>.</b>
	 * </p>
	 * 
	 * @param predicate
	 *            The predicate to check on each 'atomic' (i.e. not refined) refining diff.
	 * @return The predicate.
	 * @since 3.4
	 */
	public static Predicate<Diff> allAtomicRefining(final Predicate<? super Diff> predicate) {
		return new Predicate<Diff>() {
			public boolean apply(Diff diff) {
				Set<Diff> atomicRefiningDiffs = getAllAtomicRefiningDiffs(diff);
				if (atomicRefiningDiffs.isEmpty()) {
					return false;
				}
				return all(atomicRefiningDiffs, predicate);
			}
		};
	}

	/**
	 * This predicate can be used to check whether a diff is in a conflict directly or indirectly.
	 * <p>
	 * A diff is directly in a conflict if it {@link #hasConflict(ConflictKind...) has a conflict}. A diff is
	 * indirectly in a conflict, if one of its refining diffs is in a conflict.
	 * </p>
	 * 
	 * @param kinds
	 *            Type(s) of the conflict(s) we seek.
	 * @return The created predicate.
	 * @since 3.4
	 */
	public static Predicate<? super Diff> hasDirectOrIndirectConflict(final ConflictKind... kinds) {
		return or(hasConflict(kinds), anyRefining(hasConflict(kinds)));
	}

	/**
	 * This predicate can be used to check whether a diff is not in a conflict directly or indirectly.
	 * <p>
	 * A diff is directly in a conflict if it {@link #hasConflict(ConflictKind...) has a conflict}. A diff is
	 * indirectly in a conflict, if one of its refining diffs is in a conflict.
	 * </p>
	 * 
	 * @param kinds
	 *            Type(s) of the conflict(s) we seek.
	 * @return The created predicate.
	 * @since 3.4
	 */
	public static Predicate<? super Diff> hasNoDirectOrIndirectConflict(final ConflictKind... kinds) {
		return not(hasDirectOrIndirectConflict(kinds));
	}

	/**
	 * This particular predicate will be used to check that a given Diff corresponds to a ReferenceChange on a
	 * given reference, with known "original" and "changed" values.
	 * 
	 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
	 */
	private static final class ReferenceValuesMatch implements Predicate<Diff> {
		/** Name of the reference we expect to have been changed. */
		private final String referenceName;

		/** Qualified name of the expected original value of this reference. */
		private final String fromQualifiedName;

		/** Qualified name of the value to which this reference is expected to have changed. */
		private final String toQualifiedName;

		/**
		 * Instantiates this predicate given the values it is meant to match.
		 * 
		 * @param referenceName
		 *            Name of the single-valued reference on which we expect a change.
		 * @param fromQualifiedName
		 *            The original value of this reference.
		 * @param toQualifiedName
		 *            The value to which this reference has been changed.
		 */
		ReferenceValuesMatch(String referenceName, String fromQualifiedName, String toQualifiedName) {
			this.referenceName = referenceName;
			this.fromQualifiedName = fromQualifiedName;
			this.toQualifiedName = toQualifiedName;
		}

		/**
		 * {@inheritDoc}
		 * 
		 * @see com.google.common.base.Predicate#apply(java.lang.Object)
		 */
		public boolean apply(Diff input) {
			// Note that this is not meant for many-valued references
			if (input instanceof ReferenceChange
					&& ((ReferenceChange)input).getReference().getName().equals(referenceName)
					&& !((ReferenceChange)input).getReference().isMany()) {
				final EReference reference = ((ReferenceChange)input).getReference();
				final Match match = input.getMatch();
				final Object leftValue;
				if (match.getLeft() != null) {
					leftValue = match.getLeft().eGet(reference);
				} else {
					leftValue = null;
				}
				final Object rightValue;
				if (match.getRight() != null) {
					rightValue = match.getRight().eGet(reference);
				} else {
					rightValue = null;
				}
				final Object originValue;
				if (match.getOrigin() != null) {
					originValue = match.getOrigin().eGet(reference);
				} else {
					originValue = null;
				}

				// "from" is either right or origin
				boolean applies = false;
				if (matchAllowingNull(originValue, fromQualifiedName)) {
					// "from" is origin, "to" can be either left or right
					applies = matchAllowingNull(leftValue, toQualifiedName)
							|| matchAllowingNull(rightValue, toQualifiedName);
				} else if (matchAllowingNull(rightValue, fromQualifiedName)) {
					// "from" is right, "to" can only be left
					applies = matchAllowingNull(leftValue, toQualifiedName);
				}
				return applies;
			}
			return false;
		}
	}

	/**
	 * This particular predicate will be used to check that a given Diff corresponds to an AttributeChange on
	 * a given attribute, with known "original" and "changed" values.
	 * 
	 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
	 */
	private static final class AttributeValuesMatch implements Predicate<Diff> {
		/** Name of the attribute we expect to have been changed. */
		private final String attributeName;

		/** The expected original value of this attribute. */
		private final Object fromValue;

		/** The value to which this attribute is expected to have changed. */
		private final Object toValue;

		/**
		 * Instantiates this predicate given the values it is meant to match.
		 * 
		 * @param attributeName
		 *            Name of the single-valued attribute on which we expect a change.
		 * @param fromValue
		 *            The original value of this attribute.
		 * @param toValue
		 *            The value to which this attribute has been changed.
		 */
		AttributeValuesMatch(String attributeName, Object fromValue, Object toValue) {
			this.attributeName = attributeName;
			this.fromValue = fromValue;
			this.toValue = toValue;
		}

		/**
		 * Checks whether the two given Objects match : they are either both {@code null}, the same instance,
		 * or their "equals" returns {@code true}. If neither is {@code true}, we assume that these two
		 * Objects don't match.
		 * <p>
		 * Do note that "unset" values are in fact set to the empty String instead of {@code null}. We will
		 * thus consider {@code null} equal to the empty String here.
		 * </p>
		 * 
		 * @param attributeValue
		 *            The reference value, first of the two Objects to compare.
		 * @param expectedValue
		 *            The expected value, second of the two Objects to compare.
		 * @return {@code true} if these two Objects are equal, {@code false} otherwise.
		 */
		private static boolean equalAttributeValues(Object attributeValue, Object expectedValue) {
			// Using == to handle the "null" case
			boolean equal = expectedValue == attributeValue
					|| expectedValue != null && expectedValue.equals(attributeValue);
			// Consider that null is equal to the empty string (unset attributes)
			if (!equal) {
				equal = "".equals(attributeValue) && expectedValue == null || "".equals(expectedValue) //$NON-NLS-1$ //$NON-NLS-2$
						&& attributeValue == null;
			}
			return equal;
		}

		/**
		 * {@inheritDoc}
		 * 
		 * @see com.google.common.base.Predicate#apply(java.lang.Object)
		 */
		public boolean apply(Diff input) {
			// Note that this is not meant for multi-valued attributes
			if (input instanceof AttributeChange
					&& ((AttributeChange)input).getAttribute().getName().equals(attributeName)
					&& !((AttributeChange)input).getAttribute().isMany()) {
				final EAttribute attribute = ((AttributeChange)input).getAttribute();
				final Match match = input.getMatch();

				final Object leftValue;
				if (match.getLeft() != null) {
					leftValue = match.getLeft().eGet(attribute);
				} else {
					leftValue = attribute.getDefaultValue();
				}

				final Object rightValue;
				if (match.getRight() != null) {
					rightValue = match.getRight().eGet(attribute);
				} else {
					rightValue = attribute.getDefaultValue();
				}

				final Object originValue;
				if (match.getOrigin() != null) {
					originValue = match.getOrigin().eGet(attribute);
				} else {
					originValue = attribute.getDefaultValue();
				}

				final Object actualFrom;
				if (fromValue == null) {
					actualFrom = attribute.getDefaultValue();
				} else {
					actualFrom = fromValue;
				}
				final Object actualTo;
				if (toValue == null) {
					actualTo = attribute.getDefaultValue();
				} else {
					actualTo = toValue;
				}

				// "from" is either right or origin
				boolean applies = false;
				if (equalAttributeValues(actualFrom, originValue)) {
					// "from" is origin, "to" can be either left or right
					applies = equalAttributeValues(actualTo, leftValue)
							|| equalAttributeValues(actualTo, rightValue);
				} else if (equalAttributeValues(actualFrom, rightValue)) {
					// "from" is right, "to" can only be left
					applies = equalAttributeValues(actualTo, leftValue);
				}
				return applies;
			}
			return false;
		}
	}

	/**
	 * Predicate for diffs taht concern a given feature.
	 * 
	 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
	 */
	private static class OnFeature implements Predicate<Diff> {
		/** The feature. */
		private final EStructuralFeature feature;

		/**
		 * Constructor.
		 * 
		 * @param feature
		 *            the feature
		 */
		OnFeature(EStructuralFeature feature) {
			this.feature = checkNotNull(feature);
		}

		/**
		 * Apply the predicate.
		 * 
		 * @param input
		 *            The diff to filter.
		 * @return true if and only if input concerns the given feature.
		 */
		public boolean apply(Diff input) {
			if (input == null) {
				return false;
			}
			boolean apply = false;
			if (input instanceof ReferenceChange) {
				apply = ((ReferenceChange)input).getReference() == feature;
			} else if (input instanceof AttributeChange) {
				apply = ((AttributeChange)input).getAttribute() == feature;
			} else if (input instanceof FeatureMapChange) {
				apply = ((FeatureMapChange)input).getAttribute() == feature;
			}
			return apply;
		}
	}

	/**
	 * This will be used to filter out the list of potential candidates for conflict with a given Diff.
	 * 
	 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
	 */
	private static final class ConflictCandidateFilter implements Predicate<Diff> {
		/** The Diff for which we seek conflict candidates. */
		private final Diff diff;

		/**
		 * Instantiates our filtering Predicate given the reference Diff for which to seek potential
		 * conflicts.
		 * 
		 * @param diff
		 *            The Diff for which we seek conflict candidates, must not be null.
		 */
		ConflictCandidateFilter(Diff diff) {
			this.diff = checkNotNull(diff);
		}

		/**
		 * {@inheritDoc}
		 * 
		 * @see com.google.common.base.Predicate#apply(java.lang.Object)
		 */
		public boolean apply(Diff input) {
			return !(input instanceof ResourceLocationChange) && canConflictWith(input);
		}

		/**
		 * Checks if the given {@link Diff diff1} can be in conflict with the given {@link Diff diff2}.
		 * <p>
		 * Notably, we don't need to try and detect a conflict between two diffs if they're one and the same
		 * or if they have already been detected as a conflicting couple. Likewise, there can be no conflict
		 * if the two diffs originate from the same side.
		 * </p>
		 * <p>
		 * bug 381143 : we'll also remove any containment deletion diff on other Matches from here.
		 * </p>
		 * 
		 * @param other
		 *            candidate difference to consider for conflict detection.
		 * @return {@code true} if the two given diffs can conflict, {@code false} otherwise.
		 */
		private boolean canConflictWith(Diff other) {
			if (diff == other || diff.getSource() == other.getSource()) {
				return false;
			}
			final Conflict conflict = diff.getConflict();
			boolean canConflict = false;
			if (conflict == null || !conflict.getDifferences().contains(other)) {
				if (diff.getMatch() != other.getMatch() && other instanceof ReferenceChange
						&& ((ReferenceChange)other).getReference().isContainment()) {
					canConflict = !isDeleteOrUnsetDiff(other);
				} else {
					canConflict = true;
				}
			}
			return canConflict;
		}
	}

}

Back to the top