GroupFunction calculations remove duplicate method (#4597)

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
Andrew Fiddian-Green
2025-02-15 20:12:55 +01:00
committed by GitHub
parent 3b3fd3aa53
commit a1ff21cf1a
@@ -29,8 +29,6 @@ import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType; import org.openhab.core.types.UnDefType;
import org.openhab.core.util.Statistics; 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. * 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); State state = calculate(items);
if (stateClass.isInstance(state)) { if (stateClass.isInstance(state)) {
if (state instanceof QuantityType<?> quantity) { if (state instanceof QuantityType<?> quantity) {
state = toInvertibleUnit(quantity, baseItemUnit); state = quantity.toInvertibleUnit(baseItemUnit);
} }
return stateClass.cast(state); return stateClass.cast(state);
} else { } else {
@@ -68,27 +66,6 @@ public interface QuantityTypeArithmeticGroupFunction extends GroupFunction {
return new State[0]; 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.
* <p>
* 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 * 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 * {@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) { private @Nullable QuantityType<?> toQuantityTypeOfUnit(@Nullable State state, Unit<?> unit) {
return state instanceof QuantityType<?> quantity // return state instanceof QuantityType<?> quantity //
? toInvertibleUnit(quantity, unit) ? quantity.toInvertibleUnit(unit)
: null; : null;
} }