Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Improving scale transformation service
Browse files Browse the repository at this point in the history
to ensure that they match in the same order than the scale definition file as discussed
in PR #486

Signed-off-by: Gaël L'hopital <glhopital@gmail.com>
  • Loading branch information
clinique committed Oct 18, 2017
1 parent e1133a1 commit d927162
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
@@ -0,0 +1,3 @@
]..15[=first
[10..17[=second
[15..[=last
Expand Up @@ -98,4 +98,16 @@ public void testTransformByScaleErrorInBounds() throws TransformationException {

}

@Test
public void testEvaluationOrder() throws TransformationException {
// Ensures that only first matching scale as presented in the file is taken in account
String evaluationorder = "scale/evaluationorder.scale";
// This value matches two lines of the scale file
String source = "12";

String transformedResponse = processor.transform(evaluationorder, source);
Assert.assertEquals("first", transformedResponse);

}

}
Expand Up @@ -10,9 +10,13 @@
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -37,28 +41,50 @@ public class ScaleTransformationService extends AbstractFileTransformationServic
/** RegEx to extract a scale definition */
private static final Pattern LIMITS_PATTERN = Pattern.compile("(\\[|\\])(.*)\\.\\.(.*)(\\[|\\])");

class OrderedProperties extends Properties {
private static final long serialVersionUID = 3860553217028220119L;
private final HashSet<Object> keys = new LinkedHashSet<Object>();

public OrderedProperties() {
}

public Iterable<Object> orderedKeys() {
return Collections.list(keys());
}

@Override
public Enumeration<Object> keys() {
return Collections.<Object> enumeration(keys);
}

@Override
public Object put(Object key, Object value) {
keys.add(key);
return super.put(key, value);
}
}

/**
* Performs transformation of the input <code>source</code>
*
*
* The method transforms the input <code>source</code> by matching searching
* the range where it fits i.e. [min..max]=value or ]min..max]=value
*
* @param properties
* the list of properties defining all the available ranges
* @param source
* the input to transform
*
*
*/
@Override
protected String internalTransform(Map<Range, String> data, String source) throws TransformationException {

try {
final BigDecimal value = new BigDecimal(source);

for (final Range range : data.keySet()) {
if (range.contains(value)) {
return data.get(range);
}
Optional<Range> match = data.keySet().stream().filter(range -> range.contains(value)).findFirst();
if (match.isPresent()) {
return data.get(match.get());
}

throw new TransformationException("No matching range for '" + source + "'");
Expand All @@ -70,14 +96,14 @@ protected String internalTransform(Map<Range, String> data, String source) throw
@Override
protected Map<Range, String> internalLoadTransform(String filename) throws TransformationException {
try (FileReader reader = new FileReader(filename)) {
final Properties properties = new Properties();
final Map<Range, String> data = new LinkedHashMap<>();
final OrderedProperties properties = new OrderedProperties();
properties.load(reader);
final Map<Range, String> data = new HashMap<>();

for (Entry<Object, Object> f : properties.entrySet()) {
final String key = (String) f.getKey();
final String value = properties.getProperty(key);
final Matcher matcher = LIMITS_PATTERN.matcher(key);
for (Object orderedKey : properties.orderedKeys()) {
final String entry = (String) orderedKey;
final String value = properties.getProperty(entry);
final Matcher matcher = LIMITS_PATTERN.matcher(entry);
if (matcher.matches() && (matcher.groupCount() == 4)) {

final boolean lowerInclusive = matcher.group(1).equals("]") ? false : true;
Expand All @@ -86,32 +112,23 @@ protected Map<Range, String> internalLoadTransform(String filename) throws Trans
final String lowLimit = matcher.group(2);
final String highLimit = matcher.group(3);

final BigDecimal lowValue;
final BigDecimal highValue;

try {
if (lowLimit.isEmpty()) {
lowValue = null;
} else {
lowValue = new BigDecimal(lowLimit);
}
if (highLimit.isEmpty()) {
highValue = null;
} else {
highValue = new BigDecimal(highLimit);
}
} catch (final NumberFormatException ex) {
throw new TransformationException("Error parsing bounds: " + lowLimit + ".." + highLimit);
}
final BigDecimal lowValue = lowLimit.isEmpty() ? null : new BigDecimal(lowLimit);
final BigDecimal highValue = highLimit.isEmpty() ? null : new BigDecimal(highLimit);
final Range range = Range.range(lowValue, lowerInclusive, highValue, upperInclusive);

final Range range = Range.range(lowValue, lowerInclusive, highValue, upperInclusive);
data.put(range, value);

data.put(range, value);
} catch (NumberFormatException ex) {
throw new TransformationException("Error parsing bounds: " + lowLimit + ".." + highLimit);
}

} else {
logger.warn("Scale transform entry does not comply with syntax : '{}', '{}'", key, value);
logger.warn("Scale transform file '{}' does not comply with syntax for entry : '{}', '{}'",
filename, entry, value);
}
}

return data;
} catch (final IOException ex) {
throw new TransformationException("An error occurred while opening file.", ex);
Expand Down

0 comments on commit d927162

Please sign in to comment.