diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigUtil.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigUtil.java index 2b9ea09cf15..d99c8314938 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigUtil.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigUtil.java @@ -177,10 +177,12 @@ public static Object normalizeType(Object value, @Nullable ConfigDescriptionPara if (configDescriptionParameter != null) { Normalizer normalizer = NormalizerFactory.getNormalizer(configDescriptionParameter); return normalizer.normalize(value); - } else if (value instanceof Boolean || value instanceof String || value instanceof BigDecimal) { - return value; + } else if (value instanceof Boolean) { + return NormalizerFactory.getNormalizer(Type.BOOLEAN).normalize(value); + } else if (value instanceof String) { + return NormalizerFactory.getNormalizer(Type.TEXT).normalize(value); } else if (value instanceof Number) { - return new BigDecimal(value.toString()); + return NormalizerFactory.getNormalizer(Type.DECIMAL).normalize(value); } else if (value instanceof Collection collection) { return normalizeCollection(collection); } diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/normalization/DecimalNormalizer.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/normalization/DecimalNormalizer.java index f77bad6825d..d8e4588b6a4 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/normalization/DecimalNormalizer.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/normalization/DecimalNormalizer.java @@ -35,13 +35,16 @@ public Object doNormalize(Object value) { return stripTrailingZeros(new BigDecimal(stringValue)); } if (value instanceof Byte byteValue) { - return new BigDecimal(byteValue).setScale(1); + return new BigDecimal(byteValue); + } + if (value instanceof Short shortValue) { + return new BigDecimal(shortValue); } if (value instanceof Integer integerValue) { - return new BigDecimal(integerValue).setScale(1); + return new BigDecimal(integerValue); } if (value instanceof Long longValue) { - return new BigDecimal(longValue).setScale(1); + return new BigDecimal(longValue); } if (value instanceof Float floatValue) { return new BigDecimal(floatValue.toString()); @@ -58,9 +61,12 @@ public Object doNormalize(Object value) { } private BigDecimal stripTrailingZeros(BigDecimal value) { - BigDecimal ret = new BigDecimal(value.stripTrailingZeros().toPlainString()); - if (ret.scale() == 0) { - ret = ret.setScale(1); + BigDecimal ret = value; + if (ret.scale() > 1) { + ret = ret.stripTrailingZeros(); + if (ret.scale() == 0) { + ret = ret.setScale(1); + } } return ret; } diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/normalization/NormalizerFactory.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/normalization/NormalizerFactory.java index c7a905d297b..404890442cc 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/normalization/NormalizerFactory.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/normalization/NormalizerFactory.java @@ -51,4 +51,14 @@ public static Normalizer getNormalizer(ConfigDescriptionParameter configDescript Normalizer ret = NORMALIZERS.get(configDescriptionParameter.getType()); return configDescriptionParameter.isMultiple() ? new ListNormalizer(ret) : ret; } + + /** + * Returns the {@link Normalizer} for the given ConfigDescriptionParameter type. + * + * @param type the type + * @return the corresponding {@link Normalizer} (not null) + */ + public static Normalizer getNormalizer(Type type) { + return NORMALIZERS.get(type); + } } diff --git a/bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/ConfigurationTest.java b/bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/ConfigurationTest.java index c7b00218f82..a165c4e8bb2 100644 --- a/bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/ConfigurationTest.java +++ b/bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/ConfigurationTest.java @@ -160,18 +160,42 @@ public void assertToStringHandlesNullValuesGracefully() { @Test public void assertNormalizationInSetProperties() { Map properties = new HashMap<>(); + properties.put("byteField", Byte.valueOf("1")); + properties.put("shortField", Short.valueOf("1")); properties.put("intField", 1); + properties.put("longField", Long.valueOf("1")); + properties.put("doubleField", Double.valueOf("1")); + properties.put("floatField", Float.valueOf("1")); + properties.put("bigDecimalField", BigDecimal.ONE); Configuration configuration = new Configuration(); configuration.setProperties(properties); + assertThat(configuration.get("byteField"), is(equalTo(BigDecimal.ONE))); + assertThat(configuration.get("shortField"), is(equalTo(BigDecimal.ONE))); assertThat(configuration.get("intField"), is(equalTo(BigDecimal.ONE))); + assertThat(configuration.get("longField"), is(equalTo(BigDecimal.ONE))); + assertThat(configuration.get("doubleField"), is(equalTo(new BigDecimal("1.0")))); + assertThat(configuration.get("floatField"), is(equalTo(new BigDecimal("1.0")))); + assertThat(configuration.get("bigDecimalField"), is(equalTo(BigDecimal.ONE))); } @Test public void assertNormalizationInPut() { Configuration configuration = new Configuration(); + configuration.put("byteField", Byte.valueOf("1")); + configuration.put("shortField", Short.valueOf("1")); configuration.put("intField", 1); + configuration.put("longField", Long.valueOf("1")); + configuration.put("doubleField", Double.valueOf("1")); + configuration.put("floatField", Float.valueOf("1")); + configuration.put("bigDecimalField", BigDecimal.ONE); + assertThat(configuration.get("byteField"), is(equalTo(BigDecimal.ONE))); + assertThat(configuration.get("shortField"), is(equalTo(BigDecimal.ONE))); assertThat(configuration.get("intField"), is(equalTo(BigDecimal.ONE))); + assertThat(configuration.get("longField"), is(equalTo(BigDecimal.ONE))); + assertThat(configuration.get("doubleField"), is(equalTo(new BigDecimal("1.0")))); + assertThat(configuration.get("floatField"), is(equalTo(new BigDecimal("1.0")))); + assertThat(configuration.get("bigDecimalField"), is(equalTo(BigDecimal.ONE))); } @Test diff --git a/bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/internal/normalization/NormalizerTest.java b/bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/internal/normalization/NormalizerTest.java index e68f56692e7..33516cd55b5 100644 --- a/bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/internal/normalization/NormalizerTest.java +++ b/bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/internal/normalization/NormalizerTest.java @@ -99,18 +99,19 @@ public void testDecimalNormalizer() { ConfigDescriptionParameterBuilder.create("test", ConfigDescriptionParameter.Type.DECIMAL).build()); assertThat(normalizer.normalize(null), is(nullValue())); - assertThat(normalizer.normalize(42), is(equalTo(new BigDecimal("42.0")))); - assertThat(normalizer.normalize(42L), is(equalTo(new BigDecimal("42.0")))); - assertThat(normalizer.normalize((byte) 42), is(equalTo(new BigDecimal("42.0")))); + assertThat(normalizer.normalize(42), is(equalTo(new BigDecimal("42")))); + assertThat(normalizer.normalize(42L), is(equalTo(new BigDecimal("42")))); + assertThat(normalizer.normalize((byte) 42), is(equalTo(new BigDecimal("42")))); assertThat(normalizer.normalize(42.0), is(equalTo(new BigDecimal("42.0")))); assertThat(normalizer.normalize(42.0f), is(equalTo(new BigDecimal("42.0")))); assertThat(normalizer.normalize(42.0d), is(equalTo(new BigDecimal("42.0")))); assertThat(normalizer.normalize(42.1), is(equalTo(new BigDecimal("42.1")))); assertThat(normalizer.normalize(42.88f), is(equalTo(new BigDecimal("42.88")))); assertThat(normalizer.normalize(42.88d), is(equalTo(new BigDecimal("42.88")))); - assertThat(normalizer.normalize("42"), is(equalTo(new BigDecimal("42.0")))); + assertThat(normalizer.normalize("42"), is(equalTo(new BigDecimal("42")))); assertThat(normalizer.normalize("42.0"), is(equalTo(new BigDecimal("42.0")))); assertThat(normalizer.normalize("42.1"), is(equalTo(new BigDecimal("42.1")))); + assertThat(normalizer.normalize("42.10"), is(equalTo(new BigDecimal("42.1")))); assertThat(normalizer.normalize("42.11"), is(equalTo(new BigDecimal("42.11")))); assertThat(normalizer.normalize("42.00"), is(equalTo(new BigDecimal("42.0")))); diff --git a/bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/GenericThingProvider.xtend b/bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/GenericThingProvider.xtend index b9475f558f1..3493606dda3 100644 --- a/bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/GenericThingProvider.xtend +++ b/bundles/org.openhab.core.model.thing/src/org/openhab/core/model/thing/internal/GenericThingProvider.xtend @@ -605,8 +605,8 @@ class GenericThingProvider extends AbstractProviderLazyNullness implement if (!loadedXmlThingTypes.contains(factory.bundleName) || modelRepository == null) { return } - val oldThings = thingsMap.get(modelName).clone val newThings = newArrayList() + val oldThings = thingsMap.put(modelName, newThings) val model = modelRepository.getModel(modelName) as ThingModel if (model !== null) { @@ -616,7 +616,6 @@ class GenericThingProvider extends AbstractProviderLazyNullness implement } newThings.forEach [ newThing | - thingsMap.get(modelName).add(newThing) val oldThing = oldThings.findFirst[it.UID == newThing.UID] if (oldThing !== null) { if (!ThingHelper.equals(oldThing, newThing)) {