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

Commit

Permalink
allow pure unit strings to be parsed (#6228)
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer authored and htreu committed Sep 19, 2018
1 parent ca8acb9 commit 5e24e27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.smarthome.core.library.dimension.Intensity;
import org.eclipse.smarthome.core.library.unit.ImperialUnits;
import org.eclipse.smarthome.core.library.unit.SIUnits;
import org.eclipse.smarthome.core.library.unit.SmartHomeUnits;
import org.junit.Test;

public class UnitUtilsTest {
Expand Down Expand Up @@ -58,8 +59,16 @@ public void whenValidDimensionIsGiven_shouldCreateQuantityClass() {

@Test
public void shouldParseUnitFromPattern() {
assertThat(UnitUtils.parseUnit("%.2f °F"), is(ImperialUnits.FAHRENHEIT));
assertThat(UnitUtils.parseUnit("%.2f °C"), is(SIUnits.CELSIUS));
assertThat(UnitUtils.parseUnit("myLabel km"), is(KILO(SIUnits.METRE)));
assertThat(UnitUtils.parseUnit("%.2f %%"), is(SmartHomeUnits.PERCENT));
assertThat(UnitUtils.parseUnit("myLabel %unit%"), is(nullValue()));
}

@Test
public void testParsePureUnit() {
assertThat(UnitUtils.parseUnit("m"), is(SIUnits.METRE));
assertThat(UnitUtils.parseUnit("%"), is(SmartHomeUnits.PERCENT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,25 @@ public class UnitUtils {
}

/**
* A utility method to parse a unit symbol from a given pattern (like stateDescription or widget label).
* The unit is always expected to be the last part of the pattern separated by " " (e.g. "%.2f °C" for °C).
* A utility method to parse a unit symbol either directly or from a given pattern (like stateDescription or widget
* label). In the latter case, the unit is expected to be the last part of the pattern separated by " " (e.g. "%.2f
* °C" for °C).
*
* @param stringWithUnit the string to extract the unit symbol from
* @return the unit symbol extracted from the string or {@code null} if no unit could be parsed
*
* @param pattern The pattern to extract the unit symbol from.
* @return the unit symbol extracted from the pattern or {@code null} if the pattern did not match the expected
* format.
*/
public static @Nullable Unit<?> parseUnit(String pattern) {
if (StringUtils.isBlank(pattern)) {
return null;
}

String unitSymbol = pattern;
int lastBlankIndex = pattern.lastIndexOf(" ");
if (lastBlankIndex < 0) {
return null;
if (lastBlankIndex >= 0) {
unitSymbol = pattern.substring(lastBlankIndex).trim();
}

String unitSymbol = pattern.substring(lastBlankIndex).trim();
if (StringUtils.isNotBlank(unitSymbol) && !unitSymbol.equals(UNIT_PLACEHOLDER)) {
if (UNIT_PERCENT_FORMAT_STRING.equals(unitSymbol)) {
return SmartHomeUnits.PERCENT;
Expand Down

0 comments on commit 5e24e27

Please sign in to comment.