Skip to content

Commit 2f34209

Browse files
authored
Merge pull request #29 from JaredDavis22/varsvisibility
change getAll() to vars()
2 parents 0ff8092 + b5e22bd commit 2f34209

3 files changed

Lines changed: 36 additions & 45 deletions

File tree

src/main/java/org/scijava/parsington/eval/AbstractEvaluator.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
package org.scijava.parsington.eval;
3232

33-
import java.util.Collections;
3433
import java.util.HashMap;
3534
import java.util.Map;
3635

@@ -73,6 +72,11 @@ public void setStrict(final boolean strict) {
7372
this.strict = strict;
7473
}
7574

75+
@Override
76+
public Map<String, Object> vars() {
77+
return vars;
78+
}
79+
7680
@Override
7781
public Object get(final String name) {
7882
// NB: Here, we look up the key twice: once with containsKey, then
@@ -89,29 +93,4 @@ public Object get(final String name) {
8993
return new Unresolved(name);
9094
}
9195

92-
@Override
93-
public Map<String, Object> getAll() {
94-
return Collections.unmodifiableMap(vars);
95-
}
96-
97-
@Override
98-
public void set(final String name, final Object value) {
99-
vars.put(name, value);
100-
}
101-
102-
@Override
103-
public void setAll(final Map<? extends String, ? extends Object> map) {
104-
vars.putAll(map);
105-
}
106-
107-
@Override
108-
public Object remove(final String name) {
109-
return vars.remove(name);
110-
}
111-
112-
@Override
113-
public void clear() {
114-
vars.clear();
115-
}
116-
11796
}

src/main/java/org/scijava/parsington/eval/Evaluator.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ public interface Evaluator {
112112
*/
113113
Object evaluate(SyntaxTree syntaxTree);
114114

115+
/**
116+
* Gets all variables as a mutable {@link Map}. Not thread-safe.
117+
* <p>
118+
* This map is a view, not a copy&mdash;i.e., changes to this map alter the
119+
* evaluator's actual variable values.
120+
* </p>
121+
*
122+
* @return The map from variable names to variable values.
123+
*/
124+
Map<String, Object> vars();
125+
115126
/**
116127
* Gets the value of a token. For variables, returns the value of the
117128
* variable, throwing an exception if the variable is not set. For literals,
@@ -153,7 +164,9 @@ default Variable var(final Object token) {
153164
* @param name The name of the variable whose value you want to set.
154165
* @param value The value to assign to the variable.
155166
*/
156-
void set(String name, Object value);
167+
default void set(String name, Object value) {
168+
vars().put(name, value);
169+
}
157170

158171
/**
159172
* Gets the value of a variable.
@@ -167,13 +180,6 @@ default Object get(final Variable v) {
167180
return get(v.getToken());
168181
}
169182

170-
/**
171-
* Gets a map of all variable names and values.
172-
*
173-
* @return A map from variable names to variable values.
174-
*/
175-
Map<String, Object> getAll();
176-
177183
/**
178184
* Sets the value of a variable.
179185
*
@@ -189,7 +195,9 @@ default void set(final Variable v, final Object value) {
189195
*
190196
* @param map A map from variable names to variable values.
191197
*/
192-
void setAll(Map<? extends String, ? extends Object> map);
198+
default void setAll(Map<? extends String, ? extends Object> map) {
199+
vars().putAll(map);
200+
}
193201

194202
/**
195203
* Removes the named variable.
@@ -198,12 +206,15 @@ default void set(final Variable v, final Object value) {
198206
* @return The previous variables value associated with name,
199207
* or null if the name did not exist.
200208
*/
201-
Object remove(String name);
209+
default Object remove(String name) {
210+
return vars().remove(name);
211+
}
202212

203213
/**
204214
* Clears all the variables.
205-
*
206215
*/
207-
void clear();
216+
default void clear() {
217+
vars().clear();
218+
}
208219

209220
}

src/test/java/org/scijava/parsington/eval/AbstractEvaluatorTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,26 @@ public void testClear() {
8383
final Evaluator e = createEvaluator();
8484
e.set("a", 1);
8585
e.set("b", 2);
86+
assertEquals(2, e.vars().size());
8687
e.clear();
87-
assertEquals(new HashMap<>(), e.getAll());
88+
assertEquals(0, e.vars().size());
8889
assertThrows(IllegalArgumentException.class, () -> e.get("a"));
8990
assertThrows(IllegalArgumentException.class, () -> e.get("b"));
9091
}
9192

92-
/** Tests {@link Evaluator#getAll()} and {@link Evaluator#setAll(Map)}. */
93+
/** Tests {@link Evaluator#vars()} and {@link Evaluator#setAll(Map)}. */
9394
@Test
94-
public void testGetAllSetAll() {
95+
public void testVarsSetAll() {
9596
final Evaluator e = createEvaluator();
96-
assertEquals(new HashMap<>(), e.getAll());
97+
assertEquals(new HashMap<>(), e.vars());
9798

9899
final Map<String, Object> vars = new HashMap<>();
99100
vars.put("a", 1);
100101
vars.put("b", "two");
101102
vars.put("c", 3.0);
102103
e.setAll(vars);
103104

104-
assertEquals(vars, e.getAll());
105+
assertEquals(vars, e.vars());
105106

106107
// Verify individual get still works after setAll.
107108
assertEquals(1, e.get("a"));
@@ -110,9 +111,9 @@ public void testGetAllSetAll() {
110111

111112
// Verify variables created at evaluation are accessible and correct.
112113
e.evaluate("d=a+c");
113-
assertTrue(e.getAll().containsKey("d"));
114+
assertTrue(e.vars().containsKey("d"));
114115
final Object dVal = e.get("d");
115116
assertEquals(4.0, dVal);
116-
assertEquals(dVal, e.getAll().get("d"));
117+
assertEquals(dVal, e.vars().get("d"));
117118
}
118119
}

0 commit comments

Comments
 (0)