Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 360fa043847cdbef529b0ff7c9ff11ca3d9e449a (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
package org.eclipse.papyrus.requirements.reqif.util;

import java.util.HashMap;
import java.util.List;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.FeatureMapUtil;

public class EqualityHelperWithoutContainment extends HashMap<EObject, EObject>
{
	private static final long serialVersionUID = 1L;

	/**
	 * Returns whether <code>eObject1</code> and <code>eObject2</code> are {@link EqualityHelper equal}
	 * in the context of this helper instance.
	 * @return whether <code>eObject1</code> and <code>eObject2</code> are equal.
	 * @since 2.1.0
	 */
	public boolean equals(EObject eObject1, EObject eObject2)
	{
		// If the first object is null, the second object must be null.
		//
		if (eObject1 == null)
		{
			return eObject2 == null;
		}

		// We know the first object isn't null, so if the second one is, it can't be equal.
		//
		if (eObject2 == null)
		{
			return false;
		}

		// Both eObject1 and eObject2 are not null.
		// If eObject1 has been compared already...
		//
		Object eObject1MappedValue = get(eObject1);
		if (eObject1MappedValue != null)
		{
			// Then eObject2 must be that previous match.
			//
			return eObject1MappedValue == eObject2;
		}

		// If eObject2 has been compared already...
		//
		Object eObject2MappedValue = get(eObject2);
		if (eObject2MappedValue != null)
		{
			// Then eObject1 must be that match.
			//
			return eObject2MappedValue == eObject1;
		}

		// Neither eObject1 nor eObject2 have been compared yet.

		// If eObject1 and eObject2 are the same instance...
		//
		if (eObject1 == eObject2)
		{ 
			// Match them and return true.
			//
			put(eObject1, eObject2);
			put(eObject2, eObject1);
			return true;
		}

		// If eObject1 is a proxy...
		//  
		if (eObject1.eIsProxy())
		{
			// Then the other object must be a proxy with the same URI.
			//
			if (((InternalEObject)eObject1).eProxyURI().equals(((InternalEObject)eObject2).eProxyURI()))
			{
				put(eObject1, eObject2);
				put(eObject2, eObject1);
				return true;
			}
			else
			{
				return false;
			}
		}
		// If eObject1 isn't a proxy but eObject2 is, they can't be equal.
		//
		else if (eObject2.eIsProxy())
		{
			return false;
		}

		// If they don't have the same class, they can't be equal.
		//
		EClass eClass = eObject1.eClass();
		if (eClass != eObject2.eClass())
		{
			return false;
		}

		// Assume from now on that they match.
		//
		put(eObject1, eObject2);
		put(eObject2, eObject1);

		// Check all the values.
		//
		for (int i = 0, size = eClass.getFeatureCount(); i < size; ++i)
		{
			// Ignore derived features.
			//
			EStructuralFeature feature = eClass.getEStructuralFeature(i);
			if (!feature.isDerived())
			{
				if( feature instanceof EReference){
					if(! ((EReference)feature).isContainment()){
						if (!haveEqualFeature(eObject1, eObject2, feature))
						{
							remove(eObject1);
							remove(eObject2);
							return false;
						}
					}
				}
				else{

					if (!haveEqualFeature(eObject1, eObject2, feature))
					{
						remove(eObject1);
						remove(eObject2);
						return false;
					}
				}
			}
		}

		// There's no reason they aren't equal, so they are.
		//
		return true;
	}

	/**
	 * Returns whether <code>list1</code> and <code>list2</code> contain 
	 * {@link #equals(EObject, EObject) equal} {@link EObject}s at the same index.
	 * It is assumed that list1 and list2 only contain EObjects.
	 * @return whether <code>list1</code> and <code>list2</code> contain equal objects.
	 * @since 2.1.0
	 */
	public boolean equals(List<EObject> list1, List<EObject> list2)
	{
		int size = list1.size();
		if (size != list2.size())
		{
			return false;
		}

		for (int i = 0; i < size; i++)
		{
			EObject eObject1 = list1.get(i);
			EObject eObject2 = list2.get(i);
			if (!equals(eObject1, eObject2))
			{
				return false;
			}
		}

		return true;
	}

	/**
	 * Returns whether the two objects have {@link EqualityHelper equal} 
	 * {@link EObject#eIsSet(EStructuralFeature) isSet} states and {@link EObject#eGet(EStructuralFeature) value}s for the feature.
	 * @return whether the two objects have equal isSet states and values for the feature.
	 * @since 2.2.0
	 * @see #equals(EObject, EObject)
	 * @see #equals(List, List)
	 */
	protected boolean haveEqualFeature(EObject eObject1, EObject eObject2, EStructuralFeature feature)
	{
		// If the set states are the same, and the values of the feature are the structurally equal, they are equal.
		//
		final boolean isSet1 = eObject1.eIsSet(feature);
		final boolean isSet2 = eObject2.eIsSet(feature);
		if (isSet1 && isSet2)
		{
			return 
					feature instanceof EReference ?
							haveEqualReference(eObject1, eObject2, (EReference)feature) :
								haveEqualAttribute(eObject1, eObject2, (EAttribute)feature);
		}
		else
		{
			return isSet1 == isSet2;
		}
	}

	/**
	 * Returns whether the two objects have {@link EqualityHelper equal} {@link EObject#eGet(EStructuralFeature) value}s for the reference.
	 * @return whether the two objects have equal values for the reference.
	 * @since 2.1.0
	 * @see #equals(EObject, EObject)
	 * @see #equals(List, List)
	 */
	@SuppressWarnings("unchecked")
	protected boolean haveEqualReference(EObject eObject1, EObject eObject2, EReference reference)
	{
		Object value1 = eObject1.eGet(reference);
		Object value2 = eObject2.eGet(reference);

		return 
				reference.isMany() ?
						equals((List<EObject>)value1, (List<EObject>)value2) :
							equals((EObject)value1, (EObject)value2);
	}


	/**
	 * Returns whether the two objects have {@link EqualityHelper equal} {@link EObject#eGet(EStructuralFeature) value}s for the attribute.
	 * @return whether the two objects have equal values for the attribute.
	 * @since 2.1.0
	 * @see #equalFeatureMaps(FeatureMap, FeatureMap)
	 */
	protected boolean haveEqualAttribute(EObject eObject1, EObject eObject2, EAttribute attribute)
	{
		Object value1 = eObject1.eGet(attribute);
		Object value2 = eObject2.eGet(attribute);

		// If the first value is null, the second value must be null.
		//
		if (value1 == null)
		{
			return value2 == null;
		}

		// Since the first value isn't null, if the second one is, they aren't equal.
		//
		if (value2 == null)
		{
			return false;
		}

		// If this is a feature map...
		//
		if (FeatureMapUtil.isFeatureMap(attribute))
		{
			// The feature maps must be equal.
			//
			FeatureMap featureMap1 = (FeatureMap)value1;
			FeatureMap featureMap2 = (FeatureMap)value2;
			return equalFeatureMaps(featureMap1, featureMap2);
		}
		else
		{
			// The values must be Java equal.
			//
			return equalValues(value1, value2);
		}
	}

	/**
	 * Returns whether value1 and value2 are structurally equal.
	 * The default implementation only checks for Java equality.
	 * @param value1 the first non-null value.
	 * @param value2 the second potentially null value.
	 * @return whether value1 and value2 are structurally equal.
	 * @since 2.10
	 */
	protected boolean equalValues(Object value1, Object value2)
	{
		return value1.equals(value2);
	}

	/**
	 * Returns whether the two feature maps are {@link EqualityHelper equal}.
	 * @return whether the two feature maps are equal.
	 * @since 2.1.0
	 */
	protected boolean equalFeatureMaps(FeatureMap featureMap1, FeatureMap featureMap2)
	{
		// If they don't have the same size, the feature maps aren't equal.
		//
		int size = featureMap1.size();
		if (size != featureMap2.size())
		{
			return false;
		}

		// Compare entries in order.
		//
		for (int i = 0; i < size; i++)
		{
			// If entries don't have the same feature, the feature maps aren't equal.
			//
			EStructuralFeature feature = featureMap1.getEStructuralFeature(i);
			if (feature != featureMap2.getEStructuralFeature(i))
			{
				return false;
			}

			Object value1 = featureMap1.getValue(i);
			Object value2 = featureMap2.getValue(i);

			if (!equalFeatureMapValues(value1, value2, feature))
			{
				return false;
			}
		}

		// There is no reason they aren't equals.
		//
		return true;
	}

	/**
	 * Returns whether the two values of a feature map are {@link EqualityHelper equal}.
	 * @return whether the two values of a feature map are equal.
	 * @since 2.2.0
	 */
	protected boolean equalFeatureMapValues(Object value1, Object value2, EStructuralFeature feature)
	{
		if (feature instanceof EReference)
		{
			// If the referenced EObjects aren't equal, the feature maps aren't equal.
			//
			return equals((EObject)value1, (EObject)value2);
		}
		else
		{
			// If the values aren't Java equal, the feature maps aren't equal.
			//
			return value1 == null ? value2 == null : equalValues(value1, value2);
		}
	}

} // EqualityHelper

Back to the top