Commit c8e221d3 authored by Nico Mack's avatar Nico Mack

Added support for literals and absolute values

parent 9b7dfb28
...@@ -15,6 +15,8 @@ package lu.list.itis.dkd.tui.utility; ...@@ -15,6 +15,8 @@ package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.tui.cps.variable.Variable; import lu.list.itis.dkd.tui.cps.variable.Variable;
import com.jgoodies.common.base.Strings;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -40,6 +42,7 @@ public class VariableFormat { ...@@ -40,6 +42,7 @@ public class VariableFormat {
private List<Character> placeholders; private List<Character> placeholders;
private String template; private String template;
private boolean absoluteValue;
// *************************************************************************** // ***************************************************************************
// * Constants * // * Constants *
...@@ -49,6 +52,7 @@ public class VariableFormat { ...@@ -49,6 +52,7 @@ public class VariableFormat {
private static final char NAME = 'n'; private static final char NAME = 'n';
private static final char VALUE = 'v'; private static final char VALUE = 'v';
private static final char FORMATTED_VALUE = 'f'; private static final char FORMATTED_VALUE = 'f';
private static final char NORMALIZED_VALUE = 'r';
private static final char UNIT = 'u'; private static final char UNIT = 'u';
private static final char MIN_VALUE = 'm'; private static final char MIN_VALUE = 'm';
private static final char MAX_VALUE = 'x'; private static final char MAX_VALUE = 'x';
...@@ -62,6 +66,7 @@ public class VariableFormat { ...@@ -62,6 +66,7 @@ public class VariableFormat {
VARIABLE_PROPERTIES.put(NAME, "Name"); //$NON-NLS-1$ VARIABLE_PROPERTIES.put(NAME, "Name"); //$NON-NLS-1$
VARIABLE_PROPERTIES.put(VALUE, "Value"); //$NON-NLS-1$ VARIABLE_PROPERTIES.put(VALUE, "Value"); //$NON-NLS-1$
VARIABLE_PROPERTIES.put(FORMATTED_VALUE, "FormattedValue"); //$NON-NLS-1$ VARIABLE_PROPERTIES.put(FORMATTED_VALUE, "FormattedValue"); //$NON-NLS-1$
VARIABLE_PROPERTIES.put(NORMALIZED_VALUE, "NormalizedValue"); //$NON-NLS-1$
VARIABLE_PROPERTIES.put(UNIT, "Unit"); //$NON-NLS-1$ VARIABLE_PROPERTIES.put(UNIT, "Unit"); //$NON-NLS-1$
VARIABLE_PROPERTIES.put(MIN_VALUE, "MinValue"); //$NON-NLS-1$ VARIABLE_PROPERTIES.put(MIN_VALUE, "MinValue"); //$NON-NLS-1$
VARIABLE_PROPERTIES.put(MAX_VALUE, "MaxValue"); //$NON-NLS-1$ VARIABLE_PROPERTIES.put(MAX_VALUE, "MaxValue"); //$NON-NLS-1$
...@@ -69,14 +74,19 @@ public class VariableFormat { ...@@ -69,14 +74,19 @@ public class VariableFormat {
VARIABLE_PROPERTIES.put(TYPE, "Type"); //$NON-NLS-1$ VARIABLE_PROPERTIES.put(TYPE, "Type"); //$NON-NLS-1$
} }
private static final String PLACEHOLDER = "{}"; private static final List<Character> ABSOLUTABLES = new ArrayList<>();
private static final Pattern VARIABLE_FORMAT_PATTERN = Pattern.compile("([dnfvumxst])", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$ static {
// private static final Pattern VARIABLE_FORMAT_PATTERN = ABSOLUTABLES.add(VALUE);
// Pattern.compile("([dnfumxst]|v(\\d+(\\.\\d+)?)?)", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$ ABSOLUTABLES.add(FORMATTED_VALUE);
// private static final Pattern NUMERIC_FORMAT_PATTERN = Pattern.compile("\\d+(\\.\\d+)?)", ABSOLUTABLES.add(NORMALIZED_VALUE);
// Pattern.CASE_INSENSITIVE); //$NON-NLS-1$ }
private static final String PLACEHOLDER = "{}";
private static final Pattern VARIABLE_FORMAT_PATTERN = Pattern.compile("([dnfvumxst])|'(.*?)'", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Pattern ABSOLUTE_VALUE_PATTERN = Pattern.compile(".*?\\|[vfr]\\|.*?", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Pattern NEGATIVE_PATTERN = Pattern.compile("^(-?)([0-9.,]*)$", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Logger LOGGER = LoggerFactory.getLogger(VariableFormat.class.getSimpleName()); private static final Logger LOGGER = LoggerFactory.getLogger(VariableFormat.class.getSimpleName());
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
...@@ -87,6 +97,11 @@ public class VariableFormat { ...@@ -87,6 +97,11 @@ public class VariableFormat {
public VariableFormat(String formatString) { public VariableFormat(String formatString) {
this.template = this.parseFormatString(formatString); this.template = this.parseFormatString(formatString);
Matcher absoluteValueMatcher = ABSOLUTE_VALUE_PATTERN.matcher(formatString);
absoluteValue = absoluteValueMatcher.matches();
if (absoluteValue) {
this.template = this.template.replace("|", "");
}
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
...@@ -104,8 +119,12 @@ public class VariableFormat { ...@@ -104,8 +119,12 @@ public class VariableFormat {
while (formatMatcher.find()) { while (formatMatcher.find()) {
templateBuilder.append(format.substring(position, formatMatcher.start())); templateBuilder.append(format.substring(position, formatMatcher.start()));
placeholders.add(formatMatcher.group(1).charAt(0)); if (!Strings.isBlank(formatMatcher.group(1))) {
templateBuilder.append(PLACEHOLDER); placeholders.add(formatMatcher.group(1).charAt(0));
templateBuilder.append(PLACEHOLDER);
} else {
templateBuilder.append(formatMatcher.group(2));
}
position = formatMatcher.end(); position = formatMatcher.end();
} }
templateBuilder.append(format.substring(position)); templateBuilder.append(format.substring(position));
...@@ -158,7 +177,15 @@ public class VariableFormat { ...@@ -158,7 +177,15 @@ public class VariableFormat {
Object[] values = new Object[placeholders.size()]; Object[] values = new Object[placeholders.size()];
for (Character placeholder : placeholders) { for (Character placeholder : placeholders) {
values[index++] = getProperty(variable, VARIABLE_PROPERTIES.get(placeholder)); Object property = getProperty(variable, VARIABLE_PROPERTIES.get(placeholder));
if ((property != null) && absoluteValue && ABSOLUTABLES.contains(placeholder)) {
Matcher signMatcher = NEGATIVE_PATTERN.matcher(property.toString());
if (signMatcher.matches()) {
values[index++] = signMatcher.group(2);
}
} else {
values[index++] = property;
}
} }
return StringUtils.build(this.template, values); return StringUtils.build(this.template, values);
......
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