Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4e3c9d201166c29432073c0476e2b25528b71631 (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
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 * 
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.emf.newchild.policies;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.papyrus.infra.emf.newchild.ncpolicy.NewChildPolicy;
import org.eclipse.papyrus.infra.emf.newchild.ncpolicy.NewChildPolicySet;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;


public class PolicyMatcher {

	private List<NewChildFillPolicy> fillPolicies;

	public void setPolicies(Collection<NewChildPolicySet> policySets) {
		this.fillPolicies = new LinkedList<NewChildFillPolicy>();
		for(NewChildPolicySet policySet : policySets) {
			for(NewChildPolicy policy : policySet.getPolicies()) {
				NewChildFillPolicy fillPolicy = NewChildFillPolicy.Factory.instance.getPolicy(policy);
				fillPolicies.add(fillPolicy);
			}
		}
	}

	public List<NewChildFillPolicy> getMatchingPolicies(EClass eClass, EStructuralFeature role){
		List<NewChildFillPolicy> matchedPolicies = new LinkedList<NewChildFillPolicy>();

		for (NewChildFillPolicy policy : fillPolicies){
			if(matches(policy, eClass, role)) {
				matchedPolicies.add(policy);
			}
		}

		return matchedPolicies;
	}

	private boolean matches(NewChildFillPolicy policy, EClass eClass, EStructuralFeature role) {
		Collection<EClass> eClasses = policy.getEClasses();
		Collection<String> roles = policy.getRoles();

		if(eClasses.isEmpty() && roles.isEmpty()) {
			return true;
		}

		if(roles.contains(role.getName()) && (eClasses.isEmpty() || isContained(eClass, eClasses))) {
			return true;
		}

		if((roles.isEmpty() || roles.contains(role)) && isContained(eClass, eClasses)) {
			return true;
		}

		return false;
	}

	private boolean isContained(EClass eClass, Collection<EClass> eClasses) {
		for(EClass classToCheck : eClasses) {
			if(EMFHelper.isSubclass(eClass, classToCheck)) {
				return true;
			}
		}
		return false;
	}
}

Back to the top