Skip to main content
summaryrefslogtreecommitdiffstats
blob: aa2fb25622ce44dca3fc1af123ee8087c4f531ea (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
/*******************************************************************************
 * Copyright (c) 2013, 2016 Obeo.
 * 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
 *******************************************************************************/
package org.eclipse.emf.compare.rcp.ui.internal.structuremergeviewer.filters.impl;

import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.or;
import static org.eclipse.emf.compare.ConflictKind.REAL;
import static org.eclipse.emf.compare.DifferenceKind.ADD;
import static org.eclipse.emf.compare.DifferenceKind.DELETE;
import static org.eclipse.emf.compare.DifferenceKind.MOVE;
import static org.eclipse.emf.compare.DifferenceSource.LEFT;
import static org.eclipse.emf.compare.DifferenceSource.RIGHT;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.CONTAINMENT_REFERENCE_CHANGE;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.fromSide;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.hasNoDirectOrIndirectConflict;
import static org.eclipse.emf.compare.utils.EMFComparePredicates.ofKind;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;

import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.DifferenceSource;
import org.eclipse.emf.compare.Match;
import org.eclipse.emf.compare.ResourceAttachmentChange;
import org.eclipse.emf.compare.rcp.ui.structuremergeviewer.filters.AbstractDifferenceFilter;
import org.eclipse.emf.compare.utils.MatchUtil;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.tree.TreeNode;

/**
 * A filter used by default that filters out cascading differences (differences located under a Match that is
 * either ADDed or DELETEd on the diff's side). The MOVE differences are not hidden by this filter.
 * Differences hidden are all those that match the following criteria:
 * <ul>
 * <li>this.kind != MOVE</li>
 * <li>this.conflict == null && this.'indirect real conflicts' is empty</li>
 * <li>this.refines is empty</li>
 * <li>this is located inside a TreeNode that represents a Match that is either ADDed or DELETEd, and for
 * which the diff that represents this addition or deletion is not refined by this.</li>
 * </ul>
 * 
 * @author <a href="mailto:axel.richard@obeo.fr">Axel Richard</a>
 * @since 4.0
 */
public class CascadingDifferencesFilter extends AbstractDifferenceFilter {

	/**
	 * The predicate used by this filter when it is selected.
	 */
	private static final Predicate<? super EObject> PREDICATE_WHEN_SELECTED = new Predicate<EObject>() {
		public boolean apply(EObject input) {
			boolean ret = false;
			if (input instanceof TreeNode) {
				TreeNode treeNode = (TreeNode)input;
				EObject data = treeNode.getData();
				if (data instanceof Diff && !(data instanceof ResourceAttachmentChange)) {
					Diff diff = (Diff)data;
					if (diff.getKind() != MOVE && hasNoDirectOrIndirectConflict(REAL).apply(diff)
							&& diff.getRefines().isEmpty()) {
						TreeNode parent = treeNode.getParent();
						if (parent != null && parent.getData() instanceof Match) {
							Match parentMatch = (Match)parent.getData();
							ret = isInsideAddOrDeleteTreeNode(diff, parent);
							if (!ret && isAddOrDeleteMatch(parentMatch, diff.getSource())) {
								ret = !and(
										or(CONTAINMENT_REFERENCE_CHANGE, REFINED_BY_CONTAINMENT_REF_CHANGE),
										ofKind(ADD, DELETE)).apply(diff);
							}
						}
					}
				}
			}
			return ret;
		}

		private boolean isInsideAddOrDeleteTreeNode(Diff diff, TreeNode parent) {
			boolean ret = false;
			DifferenceSource side = diff.getSource();
			TreeNode grandParent = parent.getParent();
			Match grandParentMatch = null;
			if (grandParent != null && grandParent.getData() instanceof Match) {
				grandParentMatch = (Match)grandParent.getData();
			}
			if (isAddOrDeleteMatch(grandParentMatch, side)) {
				// The ancestor has been added/deleted, we must filter the current diff
				// _unless_ it is refined by the diff that represents the grand-parent
				// add/delete
				Diff addOrDeleteDiff = findAddOrDeleteDiff(grandParentMatch, side);
				if (addOrDeleteDiff != null) {
					if (diff.getRefinedBy().contains(addOrDeleteDiff)) {
						// recurse
						ret = isInsideAddOrDeleteTreeNode(diff, grandParent);
					} else {
						ret = true;
					}
				}
			}
			return ret;
		}

		private Diff findAddOrDeleteDiff(Match match, DifferenceSource side) {
			final Iterable<Diff> addOrDeleteContainmentDiffs = MatchUtil
					.findAddOrDeleteContainmentDiffs(match);
			if (addOrDeleteContainmentDiffs != null) {
				final UnmodifiableIterator<Diff> sideChanges = Iterators
						.filter(addOrDeleteContainmentDiffs.iterator(), fromSide(side));
				if (sideChanges.hasNext()) {
					return sideChanges.next();
				}
			}
			return null;
		}

		/**
		 * Indicate whether a Match is that of an object that was either added or deleted on the given side.
		 * 
		 * @param match
		 *            The match
		 * @param side
		 *            The side
		 * @return <code>true</code> if the matched object is present on side but not on origin or vice-versa.
		 *         <code>false</code> if match is <code>null</code>.
		 */
		protected boolean isAddOrDeleteMatch(Match match, DifferenceSource side) {
			if (match == null) {
				return false;
			}
			if (match.getComparison().isThreeWay()) {
				return (MatchUtil.getMatchedObject(match, side) == null) != (match.getOrigin() == null);
			}
			return (MatchUtil.getMatchedObject(match, side) == null) != (MatchUtil.getMatchedObject(match,
					opposite(side)) == null);
		}

		protected DifferenceSource opposite(DifferenceSource side) {
			switch (side) {
				case LEFT:
					return RIGHT;
				case RIGHT:
					return LEFT;
				default:
					throw new IllegalArgumentException("Source value not supported: " + side); //$NON-NLS-1$
			}
		}
	};

	private static final Predicate<Diff> REFINED_BY_CONTAINMENT_REF_CHANGE = new Predicate<Diff>() {
		public boolean apply(Diff input) {
			return Iterables.any(input.getRefinedBy(), CONTAINMENT_REFERENCE_CHANGE);
		}
	};

	@Override
	public Predicate<? super EObject> getPredicateWhenSelected() {
		return PREDICATE_WHEN_SELECTED;
	}
}

Back to the top