From a1ff21cf1a1e838e6b5d0a45a2a86d50d7509f9a Mon Sep 17 00:00:00 2001 From: Andrew Fiddian-Green Date: Sat, 15 Feb 2025 19:12:55 +0000 Subject: [PATCH] GroupFunction calculations remove duplicate method (#4597) Signed-off-by: Andrew Fiddian-Green --- .../QuantityTypeArithmeticGroupFunction.java | 27 ++----------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityTypeArithmeticGroupFunction.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityTypeArithmeticGroupFunction.java index 210e2b4b5..b4937c82f 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityTypeArithmeticGroupFunction.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityTypeArithmeticGroupFunction.java @@ -29,8 +29,6 @@ import org.openhab.core.types.State; import org.openhab.core.types.UnDefType; import org.openhab.core.util.Statistics; -import tech.units.indriya.AbstractUnit; - /** * This interface is a container for dimension based functions that require {@link QuantityType}s for its calculations. * @@ -55,7 +53,7 @@ public interface QuantityTypeArithmeticGroupFunction extends GroupFunction { State state = calculate(items); if (stateClass.isInstance(state)) { if (state instanceof QuantityType quantity) { - state = toInvertibleUnit(quantity, baseItemUnit); + state = quantity.toInvertibleUnit(baseItemUnit); } return stateClass.cast(state); } else { @@ -68,27 +66,6 @@ public interface QuantityTypeArithmeticGroupFunction extends GroupFunction { return new State[0]; } - /** - * Convert the given {@link QuantityType} to an equivalent based on the given {@link Unit}. The conversion can - * be made to both inverted and non-inverted units, so invertible type conversions (e.g. Mirek <=> Kelvin) are - * supported. - *

- * Note: we can use {@link QuantityType.toInvertibleUnit()} if OH Core PR #4561 is merged. - * - * @param source the {@link QuantityType} to be converted. - * @param targetUnit the {@link Unit} to convert to. - * - * @return a new {@link QuantityType} based on 'targetUnit' or null. - */ - private @Nullable QuantityType toInvertibleUnit(QuantityType source, Unit targetUnit) { - if (!targetUnit.equals(source.getUnit()) && !targetUnit.isCompatible(AbstractUnit.ONE) - && source.getUnit().inverse().isCompatible(targetUnit)) { - QuantityType sourceInSystemUnit = source.toUnit(source.getUnit().getSystemUnit()); - return sourceInSystemUnit != null ? sourceInSystemUnit.inverse().toUnit(targetUnit) : null; - } - return source.toUnit(targetUnit); - } - /** * Convert the given item {@link State} to a {@link QuantityType} based on the {@link Unit} of the * {@link GroupItem} i.e. 'referenceUnit'. Returns null if the {@link State} is not a {@link QuantityType} or @@ -102,7 +79,7 @@ public interface QuantityTypeArithmeticGroupFunction extends GroupFunction { */ private @Nullable QuantityType toQuantityTypeOfUnit(@Nullable State state, Unit unit) { return state instanceof QuantityType quantity // - ? toInvertibleUnit(quantity, unit) + ? quantity.toInvertibleUnit(unit) : null; }