Commit bc8b0bda authored by Nico Mack's avatar Nico Mack

Bug Fixes and Improvements

parent 99e5146f
......@@ -28,6 +28,8 @@ import lu.list.itis.dkd.tui.cps.InputEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
......@@ -40,7 +42,7 @@ import java.util.Vector;
@NonNullByDefault
public abstract class System implements InputDriver, InputChangeListener {
/** Field holding the {@link Vector} of nested {@link System} instances. */
protected Vector<System> nestedSystems;
protected List<System> nestedSystems;
protected boolean lockSystemForNesting = true;
private static final Logger logger = LoggerFactory.getLogger(System.class.getSimpleName());
......@@ -49,7 +51,7 @@ public abstract class System implements InputDriver, InputChangeListener {
* Constructor.
*/
public System() {
nestedSystems = new Vector<>();
nestedSystems = new ArrayList<>();
}
/**
......
......@@ -62,8 +62,9 @@ public class SqlExecutor extends Executor {
private static final String SQL_USER_PASSWORD = "sql.user.password"; //$NON-NLS-1$
private static final Pattern SQL_VAR_PATTERN = Pattern.compile("([\\$@])\\{([a-z0-9\\-_]+)\\}", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final String SQL_PLACEHOLDER = "?"; //$NON-NLS-1$
private static Logger LOGGER = LoggerFactory.getLogger(SqlExecutor.class.getSimpleName());
private static final Logger LOGGER = LoggerFactory.getLogger(SqlExecutor.class.getSimpleName());
private static final int MAX_CONNECTION_ATTEMPTS = 3;
private static HashMap<Class<?>, String> JavaToJdbcMapping = new HashMap<>();
......@@ -197,7 +198,7 @@ public class SqlExecutor extends Executor {
placeholders.add(variable);
if (SCALAR.equals(type)) {
interpolated.append("?"); //$NON-NLS-1$
interpolated.append(SQL_PLACEHOLDER);
if (positionalParameters)
interpolated.append(index++);
}
......@@ -206,21 +207,21 @@ public class SqlExecutor extends Executor {
if (value instanceof Collection) {
Collection<?> collection = (Collection<?>) value;
String separator = "";
if (collection.size() > 0) {
if (!collection.isEmpty()) {
for (int count = 0; count < collection.size(); count++) {
interpolated.append(separator);
interpolated.append("?");
interpolated.append(SQL_PLACEHOLDER);
if (positionalParameters)
interpolated.append(index++);
separator = ",";
}
} else {
interpolated.append("?"); //$NON-NLS-1$
interpolated.append(SQL_PLACEHOLDER);
if (positionalParameters)
interpolated.append(index++);
}
} else {
interpolated.append("?"); //$NON-NLS-1$
interpolated.append(SQL_PLACEHOLDER);
if (positionalParameters)
interpolated.append(index++);
}
......
......@@ -35,21 +35,39 @@ import java.util.Vector;
/**
* Class implementing numerical variables as handled by the {@link System}.
*
* @author Nico Mack [nico.mack@list.lu]
* @author Eric Tobias [eric.tobias@list.lu]
* @since 1.0
* @version 1.3.0
* @param <T>
* @version 2.5.0
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
@NonNullByDefault
public class NumericalVariable extends Variable<Double> {
protected double minValue;
protected double maxValue;
protected double scale;
protected double valueRange;
protected double normalizedValue;
private DecimalFormat format;
// ***************************************************************************
// * Constants *
// ***************************************************************************
public static final double EPSILON = 0.00001;
private static final Logger LOGGER = LoggerFactory.getLogger(NumericalVariable.class.getSimpleName());
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* Constructor resorting to the super constructor to initialise the name and unit and locally
* initialising the numerical value.
......@@ -61,6 +79,8 @@ public class NumericalVariable extends Variable<Double> {
* @param value
* The value held by the variable.
*/
// ---------------------------------------------------------------------------
public NumericalVariable(String name, String unit, Double value) {
super(Externalization.NUMERIC_TYPE, name, unit);
this.value = value;
......@@ -72,65 +92,144 @@ public class NumericalVariable extends Variable<Double> {
minValue = -Double.MAX_VALUE;
maxValue = Double.MAX_VALUE;
valueRange = getValueRange(minValue, maxValue);
scale = 1;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Primitives *
// ***************************************************************************
// ---------------------------------------------------------------------------
/**
* Constructor resorting to the super constructor to initialise the name and unit and locally
* initialising the numerical value.
* determines the value range from the specified minimum and maximum value bounds.
*
* @param rootElement
* The root element of the variable containing, as text on child elements, all the
* necessary information to initialize the variable.
* @param lowerValue
* specifies the lower value bound.
* @param upperValue
* specifies the upper value bound.
* @return the value range determined as the difference between the true upper bound and the
* true lower bound. This means that in case maxValue would be inferior to minValue,
* minValue and maxValue will be swapped. In case one of the specified values is still
* set to ± Double.MAX_VALUE the returned value will be Double.MAX_VALUE since the range
* can't be determined.
*/
// public NumericalVariable(Element rootElement) {
// super(Externalization.NUMERIC_TYPE, rootElement.getChildText("name"),
// rootElement.getChildText("unit")); //$NON-NLS-1$ //$NON-NLS-2$
// String valueContent = Strings.nullToEmpty(rootElement.getChildText("value")); //$NON-NLS-1$
// this.value = (valueContent.length() > 0) ? Double.parseDouble(valueContent) : 0.0;
//
// format = new DecimalFormat();
// format.setDecimalSeparatorAlwaysShown(false);
// format.setGroupingUsed(false);
//
// }
// ---------------------------------------------------------------------------
private double getValueRange(double lowerValue, double upperValue) {
double range = Double.MAX_VALUE;
if ((lowerValue > -Double.MAX_VALUE) && (upperValue < Double.MAX_VALUE)) {
double minimum = Math.min(lowerValue, upperValue);
double maximum = Math.max(lowerValue, upperValue);
range = maximum - minimum;
}
return range;
}
// ---------------------------------------------------------------------------
/**
* {@inheritDoc}<br>
* <br>
*
* The method constructs a string separating the value and the unit with a space.
* @param newValue
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder(format.format(value / scale));
// ---------------------------------------------------------------------------
private double normalizeValue(double newValue) {
double noramlized = 0;
if (this.valueRange == Double.MAX_VALUE) {
this.valueRange = this.getValueRange(this.minValue, this.maxValue);
}
if (this.valueRange != 0) {
double val = newValue + 0.0;
if (val < this.minValue)
val = this.minValue;
else if (val > this.maxValue)
val = this.maxValue;
noramlized = (val - this.minValue) / this.valueRange;
}
return noramlized;
}
// ---------------------------------------------------------------------------
private String stringify(double rawValue) {
StringBuilder builder = new StringBuilder(format.format(rawValue / scale));
if ((unit != null) && (unit.length() > 0)) {
builder.append(" ").append(unit); //$NON-NLS-1$
}
return builder.toString();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body *
// ***************************************************************************
// ---------------------------------------------------------------------------
public static boolean isEqual(double firstValue, double secondValue) {
return (Math.abs(firstValue - secondValue) < EPSILON);
}
// ---------------------------------------------------------------------------
/**
* @return a string showing the name of the variable, its value and its unit if defined.
*/
// ---------------------------------------------------------------------------
@Override
public String toString() {
return this.stringify(value / scale);
}
// ---------------------------------------------------------------------------
/**
* @param capped
* specifies whether or not to display the capped value, i.e. the constrained value being
* minValue <= x <= maxValue, or the raw Value. Specify <code>true</code> to display the
* capped value or <code>false</code> for the raw value
* @return a string showing the name of the variable, its value (either capped or not) and its
* unit if defined.
*/
// ---------------------------------------------------------------------------
public String toString(boolean capped) {
double displayValue = (capped) ? (this.normalizedValue * this.valueRange) : this.value;
return this.stringify(displayValue / scale);
}
// ---------------------------------------------------------------------------
public String getFormattedValue() {
return format.format(value / scale);
}
// ---------------------------------------------------------------------------
/**
* {@inheritDoc}<br>
* <br>
*
* The hash code is based on the <code>toString()</code> representation of this class.
*/
// ---------------------------------------------------------------------------
@Override
public int hashCode() {
return Objects.hashCode(name, unit, value);
}
// ---------------------------------------------------------------------------
/**
* {@inheritDoc}<br>
* <br>
*
* The equality test is base on equality of name, unit, and value.
*/
// ---------------------------------------------------------------------------
@Override
public boolean equals(@Nullable Object object) {
if (object == null)
......@@ -149,114 +248,147 @@ public class NumericalVariable extends Variable<Double> {
return false;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public void setValue(Double newValue) {
double val = (newValue != null) ? newValue + 0.0d : 0d; // Avoid -0.0 values
double val = (newValue != null) ? newValue : 0d;
if (val < this.minValue)
val = this.minValue;
else if (val > this.maxValue)
val = this.maxValue;
this.normalizedValue = this.normalizeValue(val);
super.setValue(val);
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public void setValueFromObject(Object object) {
this.setValue(this.valueFromObject(object));
}
// /** {@inheritDoc} */
// @Override
// public Double getValue() {
// return super.getValue();
// }
// ---------------------------------------------------------------------------
/**
*
* @param value
* specifies the new minimum value for this variable
*/
// ---------------------------------------------------------------------------
public void setMinValue(double value) {
this.minValue = value;
this.valueRange = getValueRange(this.minValue, this.maxValue);
}
// ---------------------------------------------------------------------------
/**
*
* @return the minimum value defined for this variable. If not previously set, the minimum value
* will be Double.MIN_VALUE
*/
// ---------------------------------------------------------------------------
public double getMinValue() {
return this.minValue;
}
// ---------------------------------------------------------------------------
/**
*
* @param value
* specifies the new maximum value for this variable
*/
// ---------------------------------------------------------------------------
public void setMaxValue(double value) {
this.maxValue = value;
this.valueRange = getValueRange(this.minValue, this.maxValue);
}
// ---------------------------------------------------------------------------
/**
*
* @return the maximum value defined for this variable. If not previously set, the minimum value
* will be Double.MAX_VALUE
*/
// ---------------------------------------------------------------------------
public double getMaxValue() {
return this.maxValue;
}
// ---------------------------------------------------------------------------
public double getValueRange() {
return this.valueRange;
}
// ---------------------------------------------------------------------------
/**
* Returns the normalized value of this variable if min and max value of variable have been
* defined, the plain value otherwise.
* Returns the normalized value of this variable.
*
* @return the normalized value, i.e 0 <= x <= 1 of the variable.
*/
// ---------------------------------------------------------------------------
public double getNormalizedValue() {
double range = 0;
return this.normalizedValue;
}
if ((minValue != -Double.MAX_VALUE) && (maxValue != Double.MAX_VALUE)) {
range = maxValue - minValue;
}
// ---------------------------------------------------------------------------
/**
*
* @param rawValue
* @return
*/
// ---------------------------------------------------------------------------
return (range > 0) ? (value - minValue) / range : value;
public double getNormalizedValueOf(double rawValue) {
return this.normalizeValue(rawValue);
}
// ---------------------------------------------------------------------------
/**
*
* @param scale
* specifies the display scale for this variable.
*/
// ---------------------------------------------------------------------------
public void setScale(double scale) {
this.scale = scale;
}
// ---------------------------------------------------------------------------
/**
*
* @return the scale to be used when displaying the value of this variable.
*/
// ---------------------------------------------------------------------------
public double getScale() {
return this.scale;
}
// ---------------------------------------------------------------------------
/**
*
* @param decimals
* specifies the number of decimals when displaying this variable.
*/
// ---------------------------------------------------------------------------
public void setNumberOfDecimals(int decimals) {
this.format.setMaximumFractionDigits(decimals);
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public NumericalVariable clone() {
NumericalVariable variable = new NumericalVariable(name, unit, value);
......@@ -268,7 +400,10 @@ public class NumericalVariable extends Variable<Double> {
return variable;
}
// ---------------------------------------------------------------------------
/** {@inheritDoc} */
// ---------------------------------------------------------------------------
@Override
public Double valueFromString(String stringValue) {
Double newValue = Double.valueOf(0);
......@@ -281,6 +416,8 @@ public class NumericalVariable extends Variable<Double> {
return newValue;
}
// ---------------------------------------------------------------------------
@Override
public Double valueFromObject(Object objectValue) {
Double converted = null;
......
/**
* Copyright Luxembourg Institute of Science and Technology, 2017. All rights reserved. If you wish
* to use this code for any purpose, please contact the author(s).
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package lu.list.itis.dkd.tui.utility;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Nico Mack [nico.mack@list.lu]
* @since 2.5
* @version 2.5.0
*/
// ***************************************************************************
// * Class Definition and Members *
// ***************************************************************************
public class Interpolator {
// ***************************************************************************
// * Constants *
// ***************************************************************************
private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\{([a-z0-9\\-_]+)(\\.(%[bhscxegat\\+\\.0-9]+))?\\}", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s)
// ***************************************************************************
// ---------------------------------------------------------------------------
private Interpolator() {}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Class Body
// ***************************************************************************
// ---------------------------------------------------------------------------
public static String interpolate(String template, Map<String, Object> parameters) {
Matcher interpolator;
String format;
String variable;
int position = 0;
StringBuilder interpolated;
interpolator = VARIABLE_PATTERN.matcher(template);
interpolated = new StringBuilder();
while (interpolator.find()) {
interpolated.append(template.substring(position, interpolator.start()));
variable = interpolator.group(1);
format = (interpolator.groupCount() == 3) ? interpolator.group(3) : ""; //$NON-NLS-1$
if ((variable != null) && (!variable.isEmpty())) {
if (parameters.containsKey(variable)) {
if (!format.isEmpty()) {
interpolated.append(String.format(format, parameters.get(variable)));
} else {
interpolated.append(parameters.get(variable));
}
}
} else {
interpolated.append(interpolated.substring(interpolator.start(), interpolator.end()));
}
position = interpolator.end();
}
interpolated.append(template.substring(position));
return interpolated.toString();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class
// ***************************************************************************
// ---------------------------------------------------------------------------
}
package lu.list.itis.dkd.tui.widget;
import lu.list.itis.dkd.tui.adapter.TangibleObject;
import lu.list.itis.dkd.tui.utility.AngleUtils;
import lu.list.itis.dkd.tui.utility.Point;
import lu.list.itis.dkd.tui.widget.builder.SelectorWidgetBuilder;
import lu.list.itis.dkd.tui.widget.corona.SelectableCorona;
......@@ -121,7 +122,7 @@ public class SelectorWidget extends ValueWidget {
double segment = TWO_PI / numberOfPositions;
int current = 0;
float angle = Point.moduloTwoPi(newAngle);
float angle = (float) AngleUtils.moduloTwoPi(newAngle);
// detect in which segment the current rotation is
for (int position = 0; position < numberOfPositions; position++) {
......@@ -178,7 +179,7 @@ public class SelectorWidget extends ValueWidget {
this.selectPosition(presetPosition);
Point centre = this.getPosition();
if (centre != null) {
presetAngleOffset = Point.moduloTwoPi(this.getCurrentAngle(currentPosition) - (centre.getAngle() - presetAngleOffset));
presetAngleOffset = (float) AngleUtils.moduloTwoPi(this.getCurrentAngle(currentPosition) - (centre.getAngle() - presetAngleOffset));
}
}
}
......@@ -208,17 +209,17 @@ public class SelectorWidget extends ValueWidget {
@Override
public void actionMove(TangibleObject tuioObject) {
double angle = Point.moduloTwoPi(tuioObject.getAngle() + presetAngleOffset);
double angle = AngleUtils.moduloTwoPi(tuioObject.getAngle() + presetAngleOffset);
TangibleObject clone<