diff --git a/src/main/java/ca/mcscert/jtet/expression/PartialVariableCollection.java b/src/main/java/ca/mcscert/jtet/expression/PartialVariableCollection.java index 97b54f5aedcb6014962842d661e1d9628fd8d05f..792fa64ae31420d31b5415289e46b3e17d0e2ddd 100644 --- a/src/main/java/ca/mcscert/jtet/expression/PartialVariableCollection.java +++ b/src/main/java/ca/mcscert/jtet/expression/PartialVariableCollection.java @@ -42,15 +42,36 @@ final public class PartialVariableCollection { m_enumerationTypes = Collections.unmodifiableSet(enumerationTypes); } - public PartialVariableCollection(Map variables) { - m_variables = Collections.unmodifiableMap(variables); + /** + * Construct PartialVariableCollection from a list of variables + * + * Often when constructing a PartialVariableCollection, you have a compile time defined list of variables. This + * provides a simple method to construct a partial variable collection from such a list. + * @param variables The variables to store in this collection. + */ + public PartialVariableCollection(Variable... variables) { + this(Arrays.asList(variables)); + } + + /** + * Construct PartialVariableCollection from a collection of variables. + * + * Due to limitations in MATLAB, variadic functions are not an option. Instead create this function to take a collection + * and construct PartialVariableCollections from it. + * @param variables The variables to store in this collection. + */ + public PartialVariableCollection(Collection variables) { + Map variablesMap = new LinkedHashMap(variables.size()); final Set enumerationTypes = new LinkedHashSet(); - for (Variable var : variables.values()) { + for (Variable var : variables) { + variablesMap.put(var.name(), var); if (var.type() instanceof EnumerationVariableType) { enumerationTypes.add((EnumerationVariableType) var.type()); } } + + m_variables = Collections.unmodifiableMap(variablesMap); m_enumerationTypes = Collections.unmodifiableSet(enumerationTypes); } diff --git a/src/test/java/ca/mcscert/jtet/eventbgenerator/ParameterizedEventBTableGeneratorTest.java b/src/test/java/ca/mcscert/jtet/eventbgenerator/ParameterizedEventBTableGeneratorTest.java index eb1b201fcab7c098998c5c3d9c4f4cb0e210bd57..ab158994e6a02ccc0b1febd5bf6a9e29b1252db4 100644 --- a/src/test/java/ca/mcscert/jtet/eventbgenerator/ParameterizedEventBTableGeneratorTest.java +++ b/src/test/java/ca/mcscert/jtet/eventbgenerator/ParameterizedEventBTableGeneratorTest.java @@ -257,12 +257,7 @@ public class ParameterizedEventBTableGeneratorTest { } public static Table initialize(String tableName, String inputVariables, Variable...outputVariables) { - Map outputVariablesMap = new LinkedHashMap(1); - for(Variable outputVariable : outputVariables) { - outputVariablesMap.put(outputVariable.name(), outputVariable); - } - - VariableCollection variableCollection = new VariableCollection(m_variableParser.parseVariables(inputVariables), new PartialVariableCollection(outputVariablesMap)); + VariableCollection variableCollection = new VariableCollection(m_variableParser.parseVariables(inputVariables), new PartialVariableCollection(outputVariables)); return new Table(tableName, variableCollection); } diff --git a/src/test/java/ca/mcscert/jtet/expression/test/PartialVariableCollectionTest.java b/src/test/java/ca/mcscert/jtet/expression/test/PartialVariableCollectionTest.java index 0ee258f8844b0a8734554c837d2882ea06d04f02..d1c8ecae3853757a88bf007c8eb0e2e15e99557a 100644 --- a/src/test/java/ca/mcscert/jtet/expression/test/PartialVariableCollectionTest.java +++ b/src/test/java/ca/mcscert/jtet/expression/test/PartialVariableCollectionTest.java @@ -48,23 +48,14 @@ import static org.junit.Assert.assertThat; public class PartialVariableCollectionTest { @Test public void singleArgumentConstructor() { - final Map variables = new HashMap(); - variables.put("test", new Variable("test", RealVariableType.Type)); + Variable var1 = new Variable("test1", RealVariableType.Type); + Variable var2 = new Variable("test2", RealVariableType.Type); - PartialVariableCollection vars = new PartialVariableCollection(variables); + PartialVariableCollection vars = new PartialVariableCollection(var1, var2); - assertThat(vars.m_variables, is(variables)); - assertThat(vars.m_enumerationTypes, is(notNullValue())); - } - - @Test - public void singleArgumentConstructorNoEnumerations() { - final Map variables = new HashMap(); - variables.put("test", new Variable("test", RealVariableType.Type)); - - PartialVariableCollection vars = new PartialVariableCollection(variables); - - assertThat(vars.m_variables, is(variables)); + assertThat(vars.m_variables.size(), is(2)); + assertThat(vars.m_variables.get("test1"), is(var1)); + assertThat(vars.m_variables.get("test2"), is(var2)); assertThat(vars.m_enumerationTypes, is(notNullValue())); assertThat(vars.m_enumerationTypes.size(), is(0)); } @@ -73,12 +64,12 @@ public class PartialVariableCollectionTest { public void singleArgumentConstructorEnumerations() { final EnumerationVariableType enumerationType = new EnumerationVariableType("testtype"); enumerationType.enumerationValues().add("testconst "); - final Map variables = new HashMap(); - variables.put("test", new Variable("test", enumerationType)); + Variable var = new Variable("test", enumerationType); - PartialVariableCollection vars = new PartialVariableCollection(variables); + PartialVariableCollection vars = new PartialVariableCollection(var); - assertThat(vars.m_variables, is(variables)); + assertThat(vars.m_variables.size(), is(1)); + assertThat(vars.m_variables.get("test"), is(var)); assertThat(vars.m_enumerationTypes, is(notNullValue())); assertThat(vars.m_enumerationTypes.size(), is(1)); assertThat(vars.m_enumerationTypes.contains(enumerationType), is(true)); diff --git a/src/test/java/ca/mcscert/jtet/expression/test/VariableCollectionTest.java b/src/test/java/ca/mcscert/jtet/expression/test/VariableCollectionTest.java index b3f1a0f4c417dc283dd6850dedf6ff18cf899145..059160027159dac4ef58d79713eef7e113460f7c 100644 --- a/src/test/java/ca/mcscert/jtet/expression/test/VariableCollectionTest.java +++ b/src/test/java/ca/mcscert/jtet/expression/test/VariableCollectionTest.java @@ -49,10 +49,7 @@ import static org.junit.Assert.assertThat; public class VariableCollectionTest { @Test public void VariablesAreInBothLists() { - Map vars = new HashMap(); - vars.put("a", new Variable("a", new RealVariableType())); - - VariableCollection toTest = new VariableCollection(new PartialVariableCollection(vars)); + VariableCollection toTest = new VariableCollection(new PartialVariableCollection(new Variable("a", new RealVariableType()))); assertThat(toTest.getInputVariables().size(), is(1)); assertThat(toTest.getInputVariablesAndEnumeratedValues().size(), is(1)); @@ -161,14 +158,7 @@ public class VariableCollectionTest { allTypeSet.addAll(inputTypeSet); allTypeSet.addAll(outputTypeSet); - final Map vars1 = new HashMap(); - vars1.put(var1e.name(), var1e); - vars1.put(var1r.name(), var1r); - - final Map vars2 = new HashMap(); - vars2.put(var2e.name(), var2e); - - final VariableCollection vc = new VariableCollection(new PartialVariableCollection(vars1), new PartialVariableCollection(vars2)); + final VariableCollection vc = new VariableCollection(new PartialVariableCollection(var1e, var1r), new PartialVariableCollection(var2e)); assertThat(vc.getInputVariableTypes(), equalTo(inputTypeSet)); assertThat(vc.getOutputVariableTypes(), equalTo(outputTypeSet)); diff --git a/src/test/java/ca/mcscert/jtet/salgenerator/HierarchicalGridSALGeneratorTest.java b/src/test/java/ca/mcscert/jtet/salgenerator/HierarchicalGridSALGeneratorTest.java index 8b48097adca9b74353e7911f794da87751bb7fb8..b6fd23a1e448ba8bfaac7a3c8af171f12dede3d8 100644 --- a/src/test/java/ca/mcscert/jtet/salgenerator/HierarchicalGridSALGeneratorTest.java +++ b/src/test/java/ca/mcscert/jtet/salgenerator/HierarchicalGridSALGeneratorTest.java @@ -55,9 +55,7 @@ public class HierarchicalGridSALGeneratorTest { @Test public void exerciseCVC3Generator() { Variable outputVar = new Variable("output", new RealVariableType()); - Map variableMap = new HashMap(1); - variableMap.put(outputVar.name(), outputVar); - Table table = new Table(null, new VariableCollection(variableParser.parseVariables("x,z"), new PartialVariableCollection(variableMap))); + Table table = new Table(null, new VariableCollection(variableParser.parseVariables("x,z"), new PartialVariableCollection(outputVar))); List topCells = table.getLeftGrid().getSubHierarchy(); topCells.add(new HierarchicalCell("x > 0")); diff --git a/src/test/java/ca/mcscert/jtet/tabularexpression/test/TableTest.java b/src/test/java/ca/mcscert/jtet/tabularexpression/test/TableTest.java index 5a3cd4de71d1b30ffaa33dfe4604f7474149d639..e8fd37387f144b7ddb092ee3dcef839e180737de 100644 --- a/src/test/java/ca/mcscert/jtet/tabularexpression/test/TableTest.java +++ b/src/test/java/ca/mcscert/jtet/tabularexpression/test/TableTest.java @@ -85,11 +85,9 @@ public class TableTest { @Before public void setupTable() { - Map outputVariablesMap = new HashMap(1); - outputVariablesMap.put("a", m_outputVariable); - PartialVariableCollection outputVariables = new PartialVariableCollection(outputVariablesMap); + PartialVariableCollection outputVariables = new PartialVariableCollection(m_outputVariable); - m_table = new Table(null, new VariableCollection(new PartialVariableCollection(new HashMap()), outputVariables)); + m_table = new Table(null, new VariableCollection(new PartialVariableCollection(), outputVariables)); } Table m_table; Variable m_outputVariable = new Variable("output", null);