Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6c5e36337472f0847f80f154c0fcdc35c74aebb (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
/*****************************************************************************
 * 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:
 * 
 * 		Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.service.types.utils;

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.uml2.uml.Artifact;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.StructuredClassifier;
import org.eclipse.uml2.uml.util.UMLSwitch;

/**
 * Utility class for Classifiers.
 */
public class ClassifierUtils {

	/**
	 * This method provides a switch to ease the addition of a new Property in a Classifier.
	 * 
	 * @param classifier
	 *        the parent classifier.
	 * @param property
	 *        the contained property.
	 * @return false if the addition fails.
	 */
	public static boolean addOwnedAttribute(Classifier classifier, Property property) {
		AddOwnedAttributeSwitch classifierSwitch = new AddOwnedAttributeSwitch(property);
		return classifierSwitch.doSwitch(classifier);
	}

	/**
	 * This method provides a switch to ease the addition of a new Property in a Classifier.
	 * 
	 * @param classifier
	 *        the parent classifier.
	 * @param property
	 *        the contained property.
	 * @return false if the addition fails.
	 */
	public static List<Property> getOwnedAttributes(Classifier classifier) {
		GetOwnedAttributesSwitch classifierSwitch = new GetOwnedAttributesSwitch();
		return classifierSwitch.doSwitch(classifier);
	}

	/**
	 * Switch implementation for Property addition.
	 */
	private static class AddOwnedAttributeSwitch extends UMLSwitch<Boolean> {

		private final Property property;

		public AddOwnedAttributeSwitch(Property property) {
			super();

			this.property = property;
		}

		@Override
		public Boolean caseArtifact(Artifact object) {
			object.getOwnedAttributes().add(property);
			return Boolean.TRUE;
		}

		@Override
		public Boolean caseDataType(DataType object) {
			object.getOwnedAttributes().add(property);
			return Boolean.TRUE;
		}

		@Override
		public Boolean caseInterface(Interface object) {
			object.getOwnedAttributes().add(property);
			return Boolean.TRUE;
		}

		@Override
		public Boolean caseSignal(Signal object) {
			object.getOwnedAttributes().add(property);
			return Boolean.TRUE;
		}

		@Override
		public Boolean caseStructuredClassifier(StructuredClassifier object) {
			object.getOwnedAttributes().add(property);
			return Boolean.TRUE;
		}

		@Override
		public Boolean caseClass(Class object) {
			object.getOwnedAttributes().add(property);
			return Boolean.TRUE;
		}

		@Override
		public Boolean defaultCase(EObject object) {
			return Boolean.FALSE;
		}
	};

	/**
	 * Switch implementation for Property addition.
	 */
	private static class GetOwnedAttributesSwitch extends UMLSwitch<List<Property>> {

		public GetOwnedAttributesSwitch() {
			super();
		}

		@Override
		public List<Property> caseArtifact(Artifact object) {
			return object.getOwnedAttributes();
		}

		@Override
		public List<Property> caseDataType(DataType object) {
			return object.getOwnedAttributes();
		}

		@Override
		public List<Property> caseInterface(Interface object) {
			return object.getOwnedAttributes();
		}

		@Override
		public List<Property> caseSignal(Signal object) {
			return object.getOwnedAttributes();
		}

		@Override
		public List<Property> caseStructuredClassifier(StructuredClassifier object) {
			return object.getOwnedAttributes();
		}

		@Override
		public List<Property> caseClass(Class object) {
			return object.getOwnedAttributes();
		}

		@Override
		public List<Property> defaultCase(EObject object) {
			return new ArrayList<Property>();
		}
	};

}

Back to the top