From a46781ce0c477e99fe27b9a2fca07bf76d1d480a Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Sat, 21 Mar 2020 20:58:46 +0200 Subject: [PATCH] Variable with index 0 cannot be handled by FMIL gitlab #17 Change-Id: Ib9255a55005174787c33cc4aa5f0dc2dcc28dcc5 --- .../src/org/simantics/fmil/core/FMIL.java | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java b/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java index ec63279..0e413af 100644 --- a/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java +++ b/org.simantics.fmil.core/src/org/simantics/fmil/core/FMIL.java @@ -123,7 +123,9 @@ public class FMIL { private String[] declaredTypeQuantities; private String[] declaredTypeUnits; - private TObjectIntHashMap variableMap = new TObjectIntHashMap(); + private static int NO_VARIABLE_KEY = -1; + + private TObjectIntHashMap variableMap = new TObjectIntHashMap(10, 0.5f, NO_VARIABLE_KEY); private Set subscriptionSet = new HashSet(); private TIntArrayList subscription = new TIntArrayList(); @@ -137,7 +139,7 @@ public class FMIL { synchronized(syncObject) { // Safety check int vr = variableMap.get(name); - if(vr == 0) return false; + if(vr == NO_VARIABLE_KEY) return false; if(!subscriptionSet.add(name)) return false; subscribedNames.add(name); subscription.add(vr); @@ -358,7 +360,10 @@ public class FMIL { * @throws FMILException */ public void setRealValue(String name, double value) throws FMILException { - setRealValue(variableMap.get(name), value); + int key = variableMap.get(name); + if(key == NO_VARIABLE_KEY) + throw new FMILException("No variable with name " + name); + setRealValue(key, value); } /** @@ -397,7 +402,10 @@ public class FMIL { * @throws FMILException */ public void setIntegerValue(String name, int value) throws FMILException { - setIntegerValue(variableMap.get(name), value); + int key = variableMap.get(name); + if(key == NO_VARIABLE_KEY) + throw new FMILException("No variable with name " + name); + setIntegerValue(key, value); } /** @@ -436,7 +444,10 @@ public class FMIL { * @throws FMILException */ public void setBooleanValue(String name, boolean value) throws FMILException { - setBooleanValue(variableMap.get(name), value); + int key = variableMap.get(name); + if(key == NO_VARIABLE_KEY) + throw new FMILException("No variable with name " + name); + setBooleanValue(key, value); } /** @@ -475,7 +486,10 @@ public class FMIL { * @throws FMILException */ public void setStringValue(String name, String value) throws FMILException { - setStringValue(variableMap.get(name), value); + int key = variableMap.get(name); + if(key == NO_VARIABLE_KEY) + throw new FMILException("No variable with name " + name); + setStringValue(key, value); } /** @@ -918,7 +932,10 @@ public class FMIL { * @throws FMILException */ public double getRealValue(String name) throws FMILException { - double result = getRealValue(variableMap.get(name)); + int key = variableMap.get(name); + if(key == NO_VARIABLE_KEY) + throw new FMILException("No variable with name " + name); + double result = getRealValue(key); if (DEBUG) System.err.println("getRealValue " + name + " = " + result); return result; } @@ -951,7 +968,10 @@ public class FMIL { * @throws FMILException */ public int getIntegerValue(String name) throws FMILException { - int result = getIntegerValue(variableMap.get(name)); + int key = variableMap.get(name); + if(key == NO_VARIABLE_KEY) + throw new FMILException("No variable with name " + name); + int result = getIntegerValue(key); if (DEBUG) System.err.println("getIntegerValue " + name + " = " + result); return result; } @@ -984,7 +1004,10 @@ public class FMIL { * @throws FMILException */ public boolean getBooleanValue(String name) throws FMILException { - boolean result = getBooleanValue(variableMap.get(name)); + int key = variableMap.get(name); + if(key == NO_VARIABLE_KEY) + throw new FMILException("No variable with name " + name); + boolean result = getBooleanValue(key); if (DEBUG) System.err.println("getBooleanValue " + name + " = " + result); return result; } @@ -1017,7 +1040,10 @@ public class FMIL { * @throws FMILException */ public String getStringValue(String name) throws FMILException { - String result = getStringValue(variableMap.get(name)); + int key = variableMap.get(name); + if(key == NO_VARIABLE_KEY) + throw new FMILException("No variable with name " + name); + String result = getStringValue(key); if (DEBUG) System.err.println("getIntegerValue " + name + " = " + result); return result; } -- 2.45.2