001: package newprocess.diagram.expressions;
002:
003: import java.math.BigDecimal;
004: import java.math.BigInteger;
005:
006: import java.util.Collection;
007: import java.util.Collections;
008: import java.util.Iterator;
009: import java.util.Map;
010:
011: import newprocess.diagram.part.New_processDiagramEditorPlugin;
012:
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.core.runtime.Status;
016:
017: import org.eclipse.emf.ecore.EClassifier;
018: import org.eclipse.emf.ecore.EObject;
019: import org.eclipse.emf.ecore.EStructuralFeature;
020: import org.eclipse.emf.ecore.ETypedElement;
021:
022: import org.eclipse.emf.ecore.util.EcoreUtil;
023:
024: /**
025: * @generated
026: */
027: public abstract class New_processAbstractExpression {
028: /**
029: * @generated
030: */
031: private static final boolean DISABLED_NO_IMPL_EXCEPTION_LOG = Boolean
032: .valueOf(
033: Platform
034: .getDebugOption(New_processDiagramEditorPlugin
035: .getInstance().getBundle()
036: .getSymbolicName()
037: + "/debug/disableNoExprImplExceptionLog"))
038: .booleanValue();
039: /**
040: * @generated
041: */
042: private String body;
043: /**
044: * @generated
045: */
046: private EClassifier context;
047: /**
048: * @generated
049: */
050: private Map env;
051: /**
052: * @generated
053: */
054: private IStatus status = Status.OK_STATUS;
055:
056: /**
057: * @generated
058: */
059: protected New_processAbstractExpression(EClassifier context) {
060: this .context = context;
061: }
062:
063: /**
064: * @generated
065: */
066: protected New_processAbstractExpression(String body,
067: EClassifier context, Map env) {
068: this .body = body;
069: this .context = context;
070: this .env = env;
071: }
072:
073: /**
074: * @generated
075: */
076: protected void setStatus(int severity, String message,
077: Throwable throwable) {
078: String pluginID = New_processDiagramEditorPlugin.ID;
079: this .status = new Status(severity, pluginID, -1,
080: (message != null) ? message : "", throwable); //$NON-NLS-1$
081: if (!this .status.isOK()) {
082: New_processDiagramEditorPlugin
083: .getInstance()
084: .logError(
085: "Expression problem:" + message + "body:" + body, throwable); //$NON-NLS-1$ //$NON-NLS-2$
086:
087: }
088: }
089:
090: /**
091: * @generated
092: */
093: protected abstract Object doEvaluate(Object context, Map env);
094:
095: /**
096: * @generated
097: */
098: public Object evaluate(Object context) {
099: return evaluate(context, Collections.EMPTY_MAP);
100: }
101:
102: /**
103: * @generated
104: */
105: public Object evaluate(Object context, Map env) {
106: if (context().isInstance(context)) {
107: try {
108: return doEvaluate(context, env);
109: } catch (Exception e) {
110: if (DISABLED_NO_IMPL_EXCEPTION_LOG
111: && e instanceof NoImplException) {
112: return null;
113: }
114: New_processDiagramEditorPlugin.getInstance().logError(
115: "Expression evaluation failure: " + body, e);
116: }
117: }
118: return null;
119: }
120:
121: /**
122: * @generated
123: */
124: public IStatus getStatus() {
125: return status;
126: }
127:
128: /**
129: * @generated
130: */
131: public String body() {
132: return body;
133: }
134:
135: /**
136: * @generated
137: */
138: public EClassifier context() {
139: return context;
140: }
141:
142: /**
143: * @generated
144: */
145: public Map environment() {
146: return env;
147: }
148:
149: /**
150: * @generated
151: */
152: public void assignTo(EStructuralFeature feature, EObject target) {
153: Object value = evaluate(target);
154: value = (value != null) ? performCast(value, feature) : null;
155: if (feature.isMany()) {
156: Collection destCollection = (Collection) target
157: .eGet(feature);
158: destCollection.clear();
159: if (value instanceof Collection) {
160: Collection valueCollection = (Collection) value;
161: for (Iterator it = valueCollection.iterator(); it
162: .hasNext();) {
163: destCollection.add(performCast(it.next(), feature));
164: }
165: } else {
166: destCollection.add(value);
167: }
168: return;
169: }
170: target.eSet(feature, value);
171: }
172:
173: /**
174: * @generated
175: */
176: protected Object performCast(Object value, ETypedElement targetType) {
177: if (targetType.getEType() == null
178: || targetType.getEType().getInstanceClass() == null) {
179: return value;
180: }
181: Class targetClass = targetType.getEType().getInstanceClass();
182: if (value != null && value instanceof Number) {
183: Number num = (Number) value;
184: Class valClass = value.getClass();
185: Class targetWrapperClass = targetClass;
186: if (targetClass.isPrimitive()) {
187: targetWrapperClass = EcoreUtil
188: .wrapperClassFor(targetClass);
189: }
190: if (valClass.equals(targetWrapperClass)) {
191: return value;
192: }
193: if (Number.class.isAssignableFrom(targetWrapperClass)) {
194: if (targetWrapperClass.equals(Byte.class))
195: return new Byte(num.byteValue());
196: if (targetWrapperClass.equals(Integer.class))
197: return new Integer(num.intValue());
198: if (targetWrapperClass.equals(Short.class))
199: return new Short(num.shortValue());
200: if (targetWrapperClass.equals(Long.class))
201: return new Long(num.longValue());
202: if (targetWrapperClass.equals(BigInteger.class))
203: return BigInteger.valueOf(num.longValue());
204: if (targetWrapperClass.equals(Float.class))
205: return new Float(num.floatValue());
206: if (targetWrapperClass.equals(Double.class))
207: return new Double(num.doubleValue());
208: if (targetWrapperClass.equals(BigDecimal.class))
209: return new BigDecimal(num.doubleValue());
210: }
211: }
212: return value;
213: }
214:
215: /**
216: * @generated
217: */
218: public static final New_processAbstractExpression createNullExpression(
219: EClassifier context) {
220: return new New_processAbstractExpression(context) {
221: protected Object doEvaluate(Object context, Map env) {
222: // TODO - log entry about not provider available for this expression
223: return null;
224: }
225: };
226: }
227:
228: /**
229: * @generated
230: */
231: public static class NoImplException extends RuntimeException {
232: /**
233: * @generated
234: */
235: public NoImplException(String message) {
236: super(message);
237: }
238: }
239: }
|