Debian Codegraph @

plugins/org.eclipse.emf.codegen.ecore/templates/model/SwitchClass.javajet

raw
1 <%@ jet package="org.eclipse.emf.codegen.ecore.templates.model" imports="java.util.* org.eclipse.emf.codegen.ecore.genmodel.* org.eclipse.emf.codegen.ecore.genmodel.util.GenModelUtil" class="SwitchClass"%>
2 <%
3 /**
4 * Copyright (c) 2002-2006 IBM Corporation and others.
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v2.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v20.html
9 *
10 * Contributors:
11 * IBM - Initial API and implementation
12 */
13 %>
14 <%GenPackage genPackage = (GenPackage)argument; GenModel genModel=genPackage.getGenModel();%>
15 <%final boolean isJDK50 = genModel.getComplianceLevel().getValue() >= GenJDKLevel.JDK50;%>
16 <%boolean supportsComposition = genModel.getRuntimeVersion().getValue() >= GenRuntimeVersion.EMF27_VALUE;%>
17 <%@ include file="../Header.javajetinc"%>
18 package <%=genPackage.getUtilitiesPackageName()%>;
19
20 <%if (supportsComposition) {%>
21 <%genModel.addImport("org.eclipse.emf.ecore.EObject");%>
22 <%genModel.addImport("org.eclipse.emf.ecore.EPackage");%>
23 <%genModel.addImport("org.eclipse.emf.ecore.util.Switch");%>
24 <%} else {%>
25 <%genModel.addImport("org.eclipse.emf.ecore.EClass");%>
26 <%genModel.addImport("org.eclipse.emf.ecore.EObject");%>
27 <%}%>
28 <%if (!genPackage.hasJavaLangConflict() && !genPackage.getUtilitiesPackageName().equals(genPackage.getInterfacePackageName())) genModel.addImport(genPackage.getInterfacePackageName() + ".*");%>
29 <%
30 String templateParameterName = null;
31 if (genModel.useGenerics())
32 {
33 Set<String> usedNames = new HashSet<String>();
34 for (GenEnum genEnum : genPackage.getGenEnums())
35 {
36 usedNames.add(genEnum.getName());
37 }
38 for (GenClass genClass : genPackage.getAllSwitchGenClasses())
39 {
40 if (!genClass.isDynamic() && !genClass.isExternalInterface())
41 {
42 usedNames.add(genClass.getName());
43 }
44 for (GenTypeParameter genTypeParameter : genClass.getGenTypeParameters())
45 {
46 usedNames.add(genTypeParameter.getName());
47 }
48 }
49 templateParameterName = "T";
50 for (int i = 1; usedNames.contains(templateParameterName); ++i)
51 {
52 templateParameterName = "T" + i;
53 }
54 }
55 %>
56 <%String templateParameters = genModel.useGenerics() ? "<" + templateParameterName + ">" : "";%>
57 <%String returnType = genModel.useGenerics() ? templateParameterName : genModel.getImportedName("java.lang.Object");%>
58 <%genModel.markImportLocation(stringBuffer);%>
59
60 /**
61 * <!-- begin-user-doc -->
62 * The <b>Switch</b> for the model's inheritance hierarchy.
63 * It supports the call {@link #doSwitch(EObject) doSwitch(object)}
64 * to invoke the <code>caseXXX</code> method for each class of the model,
65 * starting with the actual class of the object
66 * and proceeding up the inheritance hierarchy
67 * until a non-null result is returned,
68 * which is the result of the switch.
69 * <!-- end-user-doc -->
70 * @see <%=genPackage.getQualifiedPackageInterfaceName()%>
71 <%if (genPackage.hasAPITags()) {%>
72 * <%=genPackage.getAPITags(genModel.getIndentation(stringBuffer))%>
73 <%}%>
74 * @generated
75 */
76 <%if (isJDK50 && genPackage.hasAPIDeprecatedTag()) {%>
77 @Deprecated
78 <%} else if (isJDK50 && GenModelUtil.hasAPIDeprecatedTag(genPackage.getGenClasses())) {%>
79 @SuppressWarnings("deprecation")
80 <%}%>
81 public class <%=genPackage.getSwitchClassName()%><%=templateParameters%><%if (supportsComposition){%> extends Switch<%=templateParameters%><%}%>
82 {
83 <%if (genModel.hasCopyrightField()) {%>
84 /**
85 * <!-- begin-user-doc -->
86 * <!-- end-user-doc -->
87 * @generated
88 */
89 public static final <%=genModel.getImportedName("java.lang.String")%> copyright = <%=genModel.getCopyrightFieldLiteral()%>;<%=genModel.getNonNLS()%>
90
91 <%}%>
92 /**
93 * The cached model package
94 * <!-- begin-user-doc -->
95 * <!-- end-user-doc -->
96 * @generated
97 */
98 protected static <%=genPackage.getImportedPackageInterfaceName()%> modelPackage;
99
100 /**
101 * Creates an instance of the switch.
102 * <!-- begin-user-doc -->
103 * <!-- end-user-doc -->
104 * @generated
105 */
106 public <%=genPackage.getSwitchClassName()%>()
107 {
108 if (modelPackage == null)
109 {
110 modelPackage = <%=genPackage.getImportedPackageInterfaceName()%>.eINSTANCE;
111 }
112 }
113
114 <%if (supportsComposition) {%>
115 /**
116 * Checks whether this is a switch for the given package.
117 * <!-- begin-user-doc -->
118 * <!-- end-user-doc -->
119 * @param ePackage the package in question.
120 * @return whether this is a switch for the given package.
121 * @generated
122 */
123 @Override
124 protected boolean isSwitchFor(EPackage ePackage)
125 {
126 return ePackage == modelPackage;
127 }
128 <%} else { boolean isUnnecessaryElse = genModel.isUnnecessaryElse(); String indent = isUnnecessaryElse ? "" : "\t";%>
129 /**
130 * Calls <code>caseXXX</code> for each class of the model until one returns a non null result; it yields that result.
131 * <!-- begin-user-doc -->
132 * <!-- end-user-doc -->
133 * @return the first non-null result returned by a <code>caseXXX</code> call.
134 * @generated
135 */
136 public <%=returnType%> doSwitch(EObject theEObject)
137 {
138 return doSwitch(theEObject.eClass(), theEObject);
139 }
140
141 /**
142 * Calls <code>caseXXX</code> for each class of the model until one returns a non null result; it yields that result.
143 * <!-- begin-user-doc -->
144 * <!-- end-user-doc -->
145 * @return the first non-null result returned by a <code>caseXXX</code> call.
146 * @generated
147 */
148 protected <%=returnType%> doSwitch(EClass theEClass, EObject theEObject)
149 {
150 if (theEClass.eContainer() == modelPackage)
151 {
152 return doSwitch(theEClass.getClassifierID(), theEObject);
153 }
154 <%if (!isUnnecessaryElse) {%>
155 else
156 {
157 <%}%>
158 <%=indent%><%=genModel.getImportedName("java.util.List")%><%if (genModel.useGenerics()) {%><<%=genModel.getImportedName("org.eclipse.emf.ecore.EClass")%>><%}%> eSuperTypes = theEClass.getESuperTypes();
159 <%=indent%>return
160 <%=indent%> eSuperTypes.isEmpty() ?
161 <%=indent%> defaultCase(theEObject) :
162 <%=indent%> doSwitch(<%if (!genModel.useGenerics()) {%>(EClass)<%}%>eSuperTypes.get(0), theEObject);
163 <%if (!isUnnecessaryElse) {%>
164 }
165 <%}%>
166 }
167 <%}%>
168
169 /**
170 * Calls <code>caseXXX</code> for each class of the model until one returns a non null result; it yields that result.
171 * <!-- begin-user-doc -->
172 * <!-- end-user-doc -->
173 * @return the first non-null result returned by a <code>caseXXX</code> call.
174 * @generated
175 */
176 <%if (supportsComposition) {%>
177 @Override
178 <%}%>
179 protected <%=returnType%> doSwitch(int classifierID, EObject theEObject)
180 {
181 switch (classifierID)
182 {
183 <%for (GenClass genClass : genPackage.getGenClasses()) {%>
184 <%if (!genClass.isEObject()) { String result = "result".equals(genClass.getSafeUncapName()) ? "theResult" : "result"; %>
185 case <%=genPackage.getImportedPackageInterfaceName()%>.<%=genPackage.getClassifierID(genClass)%>:
186 {
187 <%if (genClass.isUncheckedCast() || genClass.isMapEntry() && isJDK50) {%>@SuppressWarnings("unchecked") <%}%><%=genClass.getImportedInterfaceName()%><%=genClass.getInterfaceWildTypeArguments()%> <%=genClass.getSafeUncapName()%> = (<%=genClass.getImportedInterfaceName()%><%=genClass.getInterfaceWildTypeArguments()%>)theEObject;
188 <%=returnType%> <%=result%> = case<%=genPackage.getClassUniqueName(genClass)%>(<%=genClass.getSafeUncapName()%>);
189 <%for (GenClass baseGenClass : genClass.getSwitchGenClasses()) {%>
190 if (<%=result%> == null) <%=result%> = <%if (genClass.isRawBaseClass(baseGenClass)) {%>(<%=returnType%>)<%}%>case<%=genPackage.getClassUniqueName(baseGenClass)%>(<%if (genClass.isMapEntry()) {%>(<%=baseGenClass.getImportedInterfaceName()%><%=baseGenClass.getInterfaceTypeArguments()%>)<%}%><%=genClass.getSafeUncapName()%>);
191 <%}%>
192 if (<%=result%> == null) <%=result%> = defaultCase(theEObject);
193 return <%=result%>;
194 }
195 <%}%>
196 <%}%>
197 default: return defaultCase(theEObject);
198 }
199 }
200 <%for (GenClass genClass : genPackage.getAllSwitchGenClasses()) {%>
201
202 /**
203 * Returns the result of interpreting the object as an instance of '<em><%=genClass.getFormattedName()%></em>'.
204 * <!-- begin-user-doc -->
205 * This implementation returns null;
206 * returning a non-null result will terminate the switch.
207 * <!-- end-user-doc -->
208 * @param object the target of the switch.
209 * @return the result of interpreting the object as an instance of '<em><%=genClass.getFormattedName()%></em>'.
210 * @see #doSwitch(org.eclipse.emf.ecore.EObject) doSwitch(EObject)
211 <%if (genClass.hasAPITags()) {%>
212 * <%=genClass.getAPITags(genModel.getIndentation(stringBuffer))%>
213 <%}%>
214 * @generated
215 */
216 <%if (isJDK50 && genClass.hasAPIDeprecatedTag()) {%>
217 @Deprecated
218 <%}%>
219 public <%=genClass.getTypeParameters()%><%=returnType%> case<%=genPackage.getClassUniqueName(genClass)%>(<%=genClass.getImportedInterfaceName()%><%=genClass.getInterfaceTypeArguments()%> object)
220 {
221 return null;
222 }
223 <%}%>
224
225 /**
226 * Returns the result of interpreting the object as an instance of '<em>EObject</em>'.
227 * <!-- begin-user-doc -->
228 * This implementation returns null;
229 * returning a non-null result will terminate the switch, but this is the last case anyway.
230 * <!-- end-user-doc -->
231 * @param object the target of the switch.
232 * @return the result of interpreting the object as an instance of '<em>EObject</em>'.
233 * @see #doSwitch(org.eclipse.emf.ecore.EObject)
234 * @generated
235 */
236 <%if (supportsComposition) {%>
237 @Override
238 <%}%>
239 public <%=returnType%> defaultCase(EObject object)
240 {
241 return null;
242 }
243
244 } //<%=genPackage.getSwitchClassName()%>
245 <%genModel.emitSortedImports();%>