From dce2840ac5c12c000afb6689a895902948da364a Mon Sep 17 00:00:00 2001 From: Matthew Dawson Date: Sat, 25 Oct 2014 14:10:10 -0400 Subject: [PATCH] Simplify PartialVariableCollection's construction. Provide a set of simpler PartialVariableCollection constructors. These take a list and work from there. Note that the original Map version still exists for VariableParser. --- .../expression/PartialVariableCollection.java | 27 +++++++++++++++-- ...ParameterizedEventBTableGeneratorTest.java | 7 +---- .../test/PartialVariableCollectionTest.java | 29 +++++++------------ .../test/VariableCollectionTest.java | 14 ++------- .../HierarchicalGridSALGeneratorTest.java | 4 +-- .../tabularexpression/test/TableTest.java | 6 ++-- 6 files changed, 40 insertions(+), 47 deletions(-) diff --git a/src/main/java/ca/mcscert/jtet/expression/PartialVariableCollection.java b/src/main/java/ca/mcscert/jtet/expression/PartialVariableCollection.java index 97b54f5..792fa64 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 eb1b201..ab15899 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 0ee258f..d1c8eca 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 b3f1a0f..0591600 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 8b48097..b6fd23a 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 5a3cd4d..e8fd373 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); -- GitLab