Commit f1d9d6c7 authored by Nico Mack's avatar Nico Mack

Added clear method to Variables

Extended functionality of ApplicationContext Helper Class
parent d519a37d
......@@ -57,6 +57,12 @@ public class BooleanVariable extends Variable<Boolean> {
this.setValue(this.valueFromObject(object));
}
@Override
public void clear() {
this.setValue(Boolean.FALSE);
}
/** {@inheritDoc} */
@Override
public BooleanVariable clone() {
......
......@@ -96,6 +96,7 @@ public class NumericalVariable extends Variable<Double> {
public NumericalVariable(String name, String unit, Double value) {
super(EquationSystemBundle.NUMERIC_TYPE, name, unit);
this.initialValue = value;
this.value = (value != null) ? value : Double.valueOf(0);
initToDefaults();
}
......@@ -119,7 +120,7 @@ public class NumericalVariable extends Variable<Double> {
minValue = -Double.MAX_VALUE;
maxValue = Double.MAX_VALUE;
initialValue = Double.NaN;
// initialValue = Double.NaN;
valueRange = getValueRange(minValue, maxValue);
epsilon = DEFAULT_EPSILON;
scale = 1;
......@@ -303,6 +304,13 @@ public class NumericalVariable extends Variable<Double> {
this.setValue(this.valueFromObject(object));
}
// ---------------------------------------------------------------------------
@Override
public void clear() {
this.setValue((!Double.isNaN(this.initialValue)) ? this.initialValue : Double.valueOf(0));
}
// ---------------------------------------------------------------------------
/**
*
......
......@@ -32,6 +32,8 @@ import java.util.regex.Pattern;
*/
public class SpatialVariable extends Variable<Point> {
private Point initialValue;
private static final Logger LOGGER = LoggerFactory.getLogger(SpatialVariable.class.getSimpleName());
private static final Pattern SPATIAL_STRING_PATTERN = Pattern.compile("([0-9]+\\.?[0-9]*)\\s*,\\s*([0-9]+\\.?[0-9]*)(\\s*,\\s*([0-9]+\\.?[0-9]*))?", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
......@@ -56,7 +58,8 @@ public class SpatialVariable extends Variable<Point> {
public SpatialVariable(String name, String unit, Point value) {
super(EquationSystemBundle.SPATIAL_TYPE, name, unit);
this.value = value;
this.initialValue = value;
clear();
}
/**
......@@ -100,6 +103,12 @@ public class SpatialVariable extends Variable<Point> {
this.setValue(this.valueFromObject(object));
}
@Override
public void clear() {
this.setValue((this.initialValue != null) ? this.initialValue : new Point());
}
/** {@inheritDoc} */
@Override
public Variable<Point> clone() {
......
......@@ -40,6 +40,8 @@ import java.util.Vector;
@NonNullByDefault
public class TextVariable extends Variable<String> {
private String initialValue;
private static final Logger logger = LoggerFactory.getLogger(TextVariable.class.getSimpleName());
// ---------------------------------------------------------------------------
......@@ -69,7 +71,8 @@ public class TextVariable extends Variable<String> {
public TextVariable(String name, String value) {
super(EquationSystemBundle.TEXT_TYPE, name, EquationSystemBundle.EMPTY_STRING);
this.value = (value != null) ? value : EquationSystemBundle.EMPTY_STRING;
initialValue = value;
this.clear();
}
/**
......@@ -125,6 +128,11 @@ public class TextVariable extends Variable<String> {
this.setValue(this.valueFromObject(object));
}
@Override
public void clear() {
this.setValue((this.initialValue != null) ? this.initialValue : EquationSystemBundle.EMPTY_STRING);
}
/** {@inheritDoc} */
@Override
public TextVariable clone() {
......
......@@ -219,6 +219,11 @@ public abstract class Variable<V> implements Cloneable {
*/
public abstract void setValueFromObject(Object object);
/**
* Clears the value of this variable.
*/
public abstract void clear();
/**
* Method used to set the equate held by the variable.
*
......
......@@ -60,6 +60,36 @@ public class ApplicationContext {
// ---------------------------------------------------------------------------
public static boolean updateVariable(String identifier, Object value) {
boolean assigned = false;
if (context.containsKey(identifier)) {
Variable<?> variable = context.get(identifier);
if (variable != null) {
variable.setValueFromObject(value);
assigned = true;
}
}
return assigned;
}
// ---------------------------------------------------------------------------
public static boolean clearVariable(String identifier) {
boolean cleared = false;
if (context.containsKey(identifier)) {
Variable<?> variable = context.get(identifier);
if (variable != null) {
variable.clear();
cleared = true;
}
}
return cleared;
}
// ---------------------------------------------------------------------------
public static void atomicUpdate(List<Variable<?>> variables, List<Object> values) {
Preconditions.checkArgument(!variables.isEmpty(), "No variables to update!"); //$NON-NLS-1$
Preconditions.checkArgument(variables.size() == values.size(), "Sizes of variables and values lists must match!"); //$NON-NLS-1$
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment