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

Commit

Permalink
QuantityType: parse exponential values more stable (#5276)
Browse files Browse the repository at this point in the history
Signed-off-by: Henning Treu <henning.treu@telekom.de>
  • Loading branch information
Henning Treu authored and maggu2810 committed Mar 20, 2018
1 parent 9903848 commit 336878d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Expand Up @@ -71,6 +71,9 @@ public void testValidConstructors() throws Exception {
new QuantityType<>("3km/h");
new QuantityType<>("1084 hPa");
new QuantityType<>("0E-22 m");
new QuantityType<>("10E-3");
new QuantityType<>("10E+3");
new QuantityType<>("10E3");
QuantityType.valueOf("2m");
}

Expand Down
Expand Up @@ -25,7 +25,6 @@
import javax.measure.UnitConverter;
import javax.measure.quantity.Dimensionless;

import org.apache.commons.lang.ArrayUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
Expand Down Expand Up @@ -60,7 +59,10 @@ public class QuantityType<T extends Quantity<T>> extends Number
public static final QuantityType<Dimensionless> ONE = new QuantityType<>(1, AbstractUnit.ONE);

// Regular expression to split unit from value
private static final String UNIT_PATTERN = "(?<=\\d)\\s*(?=[a-zA-Z°µ%'])";
// split on any blank character, even none (\\s*) which occurs after a digit (?<=\\d) and before
// a "unit" character ?=[a-zA-Z°µ%'] which itself must not be preceded by plus/minus digit (?![\\+\\-]?\\d).
// The later would be an exponent from the scalar value.
private static final String UNIT_PATTERN = "(?<=\\d)\\s*(?=[a-zA-Z°µ%'](?![\\+\\-]?\\d))";

private final Quantity<T> quantity;

Expand All @@ -75,8 +77,7 @@ public QuantityType(String value) {
String[] constituents = value.split(UNIT_PATTERN);

// getQuantity needs a space between numeric value and unit
constituents = (String[]) ArrayUtils.add(constituents, constituents.length - 1, " ");
String formatted = String.join("", constituents);
String formatted = String.join(" ", constituents);
quantity = (Quantity<T>) Quantities.getQuantity(formatted);
}

Expand Down

0 comments on commit 336878d

Please sign in to comment.