Skip to content

Commit

Permalink
GroupFunction calculations remove duplicate method (#4597)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
  • Loading branch information
andrewfg authored Feb 15, 2025
1 parent 3b3fd3a commit a1ff21c
Showing 1 changed file with 2 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
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.
*
Expand All @@ -55,7 +53,7 @@ public DimensionalGroupFunction(Unit<?> baseItemUnit) {
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 {
Expand All @@ -68,27 +66,6 @@ public State[] getParameters() {
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
* {@link GroupItem} i.e. 'referenceUnit'. Returns null if the {@link State} is not a {@link QuantityType} or
Expand All @@ -102,7 +79,7 @@ public State[] getParameters() {
*/
private @Nullable QuantityType<?> toQuantityTypeOfUnit(@Nullable State state, Unit<?> unit) {
return state instanceof QuantityType<?> quantity //
? toInvertibleUnit(quantity, unit)
? quantity.toInvertibleUnit(unit)
: null;
}

Expand Down

0 comments on commit a1ff21c

Please sign in to comment.