| 1 | /* |
| 2 | * Copyright 2006 SDQ Research Group, University of Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.seff.expressions; |
| 5 | |
| 6 | import java.math.BigDecimal; |
| 7 | import java.math.BigInteger; |
| 8 | import java.util.Collections; |
| 9 | import java.util.Map; |
| 10 | |
| 11 | import org.eclipse.core.runtime.IStatus; |
| 12 | import org.eclipse.core.runtime.Status; |
| 13 | import org.eclipse.emf.ecore.EClassifier; |
| 14 | import org.eclipse.emf.ecore.EDataType; |
| 15 | import org.eclipse.emf.ecore.EEnum; |
| 16 | import org.eclipse.emf.ecore.EEnumLiteral; |
| 17 | import org.eclipse.emf.ecore.util.EcoreUtil; |
| 18 | |
| 19 | import de.uka.ipd.sdq.pcm.gmf.seff.part.PalladioComponentModelSeffDiagramEditorPlugin; |
| 20 | |
| 21 | /** |
| 22 | * @generated |
| 23 | */ |
| 24 | public abstract class PalladioComponentModelAbstractExpression { |
| 25 | /** |
| 26 | * @generated |
| 27 | */ |
| 28 | private IStatus status = Status.OK_STATUS; |
| 29 | |
| 30 | /** |
| 31 | * @generated |
| 32 | */ |
| 33 | protected PalladioComponentModelAbstractExpression(String body, |
| 34 | EClassifier context) { |
| 35 | myBody = body; |
| 36 | myContext = context; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @generated |
| 41 | */ |
| 42 | protected void setStatus(int severity, String message, Throwable throwable) { |
| 43 | String pluginID = PalladioComponentModelSeffDiagramEditorPlugin.ID; |
| 44 | this.status = new Status(severity, pluginID, -1, |
| 45 | (message != null) ? message : "", throwable); //$NON-NLS-1$ |
| 46 | if (!this.status.isOK()) { |
| 47 | PalladioComponentModelSeffDiagramEditorPlugin |
| 48 | .getInstance() |
| 49 | .logError( |
| 50 | "Expression problem:" + message + "body:" + body(), throwable); //$NON-NLS-1$ //$NON-NLS-2$ |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @generated |
| 56 | */ |
| 57 | protected abstract Object doEvaluate(Object context, Map env); |
| 58 | |
| 59 | /** |
| 60 | * @generated |
| 61 | */ |
| 62 | public Object evaluate(Object context) { |
| 63 | return evaluate(context, Collections.EMPTY_MAP); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @generated |
| 68 | */ |
| 69 | public Object evaluate(Object context, Map env) { |
| 70 | if (context().isInstance(context)) { |
| 71 | try { |
| 72 | return doEvaluate(context, env); |
| 73 | } catch (Exception e) { |
| 74 | PalladioComponentModelSeffDiagramEditorPlugin |
| 75 | .getInstance() |
| 76 | .logError("Expression evaluation failure: " + body(), e); //$NON-NLS-1$ |
| 77 | } |
| 78 | } |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Expression may return number value which is not directly compatible with feature type (e.g. Double when Integer is expected), or EEnumLiteral meta-object when literal instance is expected |
| 84 | * @generated |
| 85 | */ |
| 86 | public static Object performCast(Object value, EDataType targetType) { |
| 87 | if (targetType instanceof EEnum) { |
| 88 | if (value instanceof EEnumLiteral) { |
| 89 | EEnumLiteral literal = (EEnumLiteral) value; |
| 90 | return (literal.getInstance() != null) ? literal.getInstance() |
| 91 | : literal; |
| 92 | } |
| 93 | } |
| 94 | if (false == value instanceof Number || targetType == null |
| 95 | || targetType.getInstanceClass() == null) { |
| 96 | return value; |
| 97 | } |
| 98 | Class targetClass = targetType.getInstanceClass(); |
| 99 | Number num = (Number) value; |
| 100 | Class valClass = value.getClass(); |
| 101 | Class targetWrapperClass = targetClass; |
| 102 | if (targetClass.isPrimitive()) { |
| 103 | targetWrapperClass = EcoreUtil.wrapperClassFor(targetClass); |
| 104 | } |
| 105 | if (valClass.equals(targetWrapperClass)) { |
| 106 | return value; |
| 107 | } |
| 108 | if (Number.class.isAssignableFrom(targetWrapperClass)) { |
| 109 | if (targetWrapperClass.equals(Byte.class)) |
| 110 | return new Byte(num.byteValue()); |
| 111 | if (targetWrapperClass.equals(Integer.class)) |
| 112 | return new Integer(num.intValue()); |
| 113 | if (targetWrapperClass.equals(Short.class)) |
| 114 | return new Short(num.shortValue()); |
| 115 | if (targetWrapperClass.equals(Long.class)) |
| 116 | return new Long(num.longValue()); |
| 117 | if (targetWrapperClass.equals(BigInteger.class)) |
| 118 | return BigInteger.valueOf(num.longValue()); |
| 119 | if (targetWrapperClass.equals(Float.class)) |
| 120 | return new Float(num.floatValue()); |
| 121 | if (targetWrapperClass.equals(Double.class)) |
| 122 | return new Double(num.doubleValue()); |
| 123 | if (targetWrapperClass.equals(BigDecimal.class)) |
| 124 | return new BigDecimal(num.doubleValue()); |
| 125 | } |
| 126 | return value; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @generated |
| 131 | */ |
| 132 | public IStatus getStatus() { |
| 133 | return status; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @generated |
| 138 | */ |
| 139 | private final String myBody; |
| 140 | |
| 141 | /** |
| 142 | * @generated |
| 143 | */ |
| 144 | public String body() { |
| 145 | return myBody; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @generated |
| 150 | */ |
| 151 | private final EClassifier myContext; |
| 152 | |
| 153 | /** |
| 154 | * @generated |
| 155 | */ |
| 156 | public EClassifier context() { |
| 157 | return myContext; |
| 158 | } |
| 159 | } |