Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Vorburger2015-01-29 16:32:08 +0000
committerMichael Vorburger2015-01-29 16:32:08 +0000
commitd7c9541ecf75488f96717ae8b79d8e5855263c1f (patch)
treeb5074155261f108c10e76df24963f7bb6d390e8f
parentca7c690cce066b637da05b9041cc71fdbe1957b7 (diff)
downloadorg.eclipse.emf.eson-PerfAbstractValueSwitchAvoidToStringAndReParse.tar.gz
org.eclipse.emf.eson-PerfAbstractValueSwitchAvoidToStringAndReParse.tar.xz
org.eclipse.emf.eson-PerfAbstractValueSwitchAvoidToStringAndReParse.zip
[performance] AbstractValueSwitch doesn't really need toString() andPerfAbstractValueSwitchAvoidToStringAndReParse
reparse all values, type casting is (hopefully) faster
-rw-r--r--plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AbstractValueSwitch.java44
-rw-r--r--plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AttributeBuilder.java12
2 files changed, 44 insertions, 12 deletions
diff --git a/plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AbstractValueSwitch.java b/plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AbstractValueSwitch.java
index cbd6a2e..39981d6 100644
--- a/plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AbstractValueSwitch.java
+++ b/plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AbstractValueSwitch.java
@@ -16,10 +16,12 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
+import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EEnumLiteral;
public abstract class AbstractValueSwitch<T> {
+ private static final Logger logger = Logger.getLogger(AbstractValueSwitch.class);
protected abstract T caseString(String value);
@@ -42,23 +44,48 @@ public abstract class AbstractValueSwitch<T> {
protected abstract T caseDate(Date value);
public T doSwitch(Class<?> clazz, Object value) throws IllegalArgumentException {
+ try {
+ return doSwitchUnsafe(clazz,value);
+ } catch (RuntimeException e) {
+ logger.warn("doSwitch() got a RuntimeException (probably ClassCastException or NumberFormatException) for clazz: " + clazz.toString() + ", value class: " + value.getClass().toString() + ", value: " + value.toString());
+ return null;
+ }
+ }
+
+ protected T doSwitchUnsafe(Class<?> clazz, Object value) throws IllegalArgumentException {
if (clazz == long.class || clazz == Long.class) {
- return caseLong(Long.valueOf(value.toString()));
+// return caseLong(Long.valueOf(value.toString()));
+// if (value instanceof Long)
+ return caseLong((Long) value);
+// else
}
if (clazz == double.class || clazz == Double.class) {
- return caseDouble(Double.valueOf(value.toString()));
+// return caseDouble(Double.valueOf(value.toString()));
+ return caseDouble((Double) value);
}
if (clazz == BigDecimal.class) {
return caseBigDecimal(new BigDecimal(value.toString()));
}
if (clazz == BigInteger.class) {
- return caseBigInteger(new BigInteger(value.toString()));
+// return caseBigInteger(new BigInteger(value.toString()));
+ if (value instanceof Long)
+ return caseBigInteger(BigInteger.valueOf((Long)value));
+ else if (value instanceof BigInteger)
+ return caseBigInteger((BigInteger) value);
}
if (clazz == short.class || clazz == Short.class) {
- return caseShort(Short.valueOf(value.toString()));
+// return caseShort(Short.valueOf(value.toString()));
+ if (value instanceof Long)
+ return caseShort(((Long) value).shortValue());
+ else if (value instanceof Short)
+ return caseShort((Short) value);
}
if (clazz == int.class || clazz == Integer.class) {
- return caseInt(Integer.valueOf(value.toString()));
+// return caseInt(Integer.valueOf(value.toString()));
+ if (value instanceof Long)
+ return caseInt(((Long) value).intValue());
+ else if (value instanceof Integer)
+ return caseInt(((Integer) value));
}
if (value instanceof EEnumLiteral) {
return caseEnum((EEnumLiteral) value);
@@ -67,9 +94,14 @@ public abstract class AbstractValueSwitch<T> {
return caseEnum((Enumerator) value);
}
if (clazz == boolean.class || clazz == Boolean.class) {
- return caseBoolean(Boolean.valueOf(value.toString()));
+// return caseBoolean(Boolean.valueOf(value.toString()));
+ if (value instanceof Boolean)
+ return caseBoolean((Boolean) value);
+ else // EFactoryJavaValidator et al currently rely on this working like this default fall back (not entirely correct, strictly speaking)
+ return caseBoolean(Boolean.FALSE);
}
if (clazz == String.class) {
+ // String.toString(), which just return this, may be faster than an explicit cast
return caseString(value.toString());
}
if (clazz == Date.class) {
diff --git a/plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AttributeBuilder.java b/plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AttributeBuilder.java
index f1b50a7..88387af 100644
--- a/plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AttributeBuilder.java
+++ b/plugins/org.eclipse.emf.eson/src/org/eclipse/emf/eson/building/AttributeBuilder.java
@@ -50,15 +50,15 @@ public class AttributeBuilder extends FeatureBuilder {
}
Class<?> clazz = eAttribute.getEAttributeType().getInstanceClass();
- try {
- newValue = convertToTargetType(clazz, newValue, getFeature());
- } catch (RuntimeException e) {
- throw new ModelBuilderException("convertToTargetType() failed for feature: " + eAttribute.toString(), e);
+ Object convertedNewValue = convertToTargetType(clazz, newValue, getFeature());
+ if (convertedNewValue == null) {
+ // We do this for EFactoryJavaValidator et al
+ convertedNewValue = newValue;
}
- EcoreUtil3.setOrAddValue(getContainer(), eAttribute, newValue);
+ EcoreUtil3.setOrAddValue(getContainer(), eAttribute, convertedNewValue);
}
- protected Object convertToTargetType(Class<?> clazz, Object newValue, Feature feature) throws IllegalArgumentException {
+ protected Object convertToTargetType(Class<?> clazz, Object newValue, Feature feature) {
Object o = new ValueSwitch().doSwitch(clazz, newValue);
if (!clazz.isPrimitive() && o != null && !clazz.isInstance(o)) {
logger.warn("Likely upcoming ClassCastException 'heads up' - failed to convert value '"

Back to the top