Skip to main content
summaryrefslogtreecommitdiffstats
blob: d701c22dbde412b3e5cd1ac84cd293532cb56095 (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
/*******************************************************************************
 * Copyright (c) 2010, 2011 Technical University of Denmark.
 * 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:
 *    Patrick Koenemann, DTU Informatics - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.mpatch.apply.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.compare.mpatch.IElementReference;
import org.eclipse.emf.compare.mpatch.ModelDescriptorReference;
import org.eclipse.emf.compare.mpatch.extension.ResolvedSymbolicReferences.ValidationResult;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;

/**
 * Wrapper class for Symbolic reference checks.
 * 
 * @author Patrick Koenemann (pk@imm.dtu.dk)
 */
class SymRefCheck {

	/**
	 * The symbolic references that was checked. Please see constructor documentation for details.
	 */
	final IElementReference symRef;

	/**
	 * The validation result. Please see constructor documentation for details.
	 */
	final ValidationResult validationResult;

	/**
	 * The list of resolved model elements. Please see constructor documentation for details.
	 */
	final List<EObject> elements;

	/**
	 * Whether this is an internal reference. Please see constructor documentation for details.
	 */
	final boolean internal;

	/**
	 * Check whether the given symbolic references is resolved successfully.
	 * 
	 * The result should check in this order:
	 * <ol>
	 * <li><code>symRef</code> might be <code>null</code>!
	 * <li>if <code>internal</code> is true, ignore <code>elements</code> and the <code>validationResult</code>
	 * <li>then, if the bounds are ok and some elements of the correct type are resolved, <code>validationResult</code>
	 * is <code>null</code>. otherwise, <code>validationResult</code> either results in
	 * {@link ValidationResult#REFERENCE} or {@link ValidationResult#STATE_INVALID}.
	 * </ol>
	 * 
	 * 
	 * @param symRef
	 *            A given symbolic reference.
	 * @param map
	 *            The resolution of it.
	 * @param type
	 *            The type which all resolved elements must have.
	 * @param upperBound
	 *            The maximum number of resolved elements.
	 */
	public SymRefCheck(IElementReference symRef, Map<IElementReference, List<EObject>> map, EClassifier type,
			int upperBound) {
		// default values:
		ValidationResult validation = null;
		elements = new ArrayList<EObject>();
		boolean internal = false;

		// perform checks:
		if (symRef == null) {
			// default values
		} else if (symRef instanceof ModelDescriptorReference) {
			internal = true;
		} else {
			// symref is set, so we need to check the resolution!
			final List<EObject> list = map.get(symRef);
			if (list != null)
				elements.addAll(list);
			if (elements.size() == 0 || (elements.size() > upperBound && upperBound > 0)) {
				validation = ValidationResult.REFERENCE;
			} else {

				// check type
				for (EObject element : elements) {
					if (!type.isInstance(element)) {
						validation = ValidationResult.STATE_INVALID;
						break;
					}
				}
			}
		}
		this.symRef = symRef;
		this.validationResult = validation;
		this.internal = internal;
	}
}

Back to the top