remove duplicate code (#18270)

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
pull/18271/head
Andrew Fiddian-Green 2025-02-15 22:55:48 +00:00 committed by GitHub
parent 40a6334ecd
commit 94313ba102
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 1 additions and 25 deletions

View File

@ -54,8 +54,6 @@ import org.openhab.transform.basicprofiles.internal.config.StateFilterProfileCon
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import tech.units.indriya.AbstractUnit;
/** /**
* Accepts updates to state as long as conditions are met. Support for sending fixed state if conditions are *not* * Accepts updates to state as long as conditions are met. Support for sending fixed state if conditions are *not*
* met. * met.
@ -776,32 +774,10 @@ public class StateFilterProfile implements StateProfile {
*/ */
protected @Nullable QuantityType<?> toSystemUnitQuantityType(State state) { protected @Nullable QuantityType<?> toSystemUnitQuantityType(State state) {
return state instanceof QuantityType<?> quantityType && hasSystemUnit() // return state instanceof QuantityType<?> quantityType && hasSystemUnit() //
? toInvertibleUnit(quantityType, Objects.requireNonNull(systemUnit)) ? quantityType.toInvertibleUnit(Objects.requireNonNull(systemUnit))
: null; : null;
} }
/**
* Convert the given {@link QuantityType} to an equivalent based on the target {@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 'systemUnit' or null.
*/
protected @Nullable QuantityType<?> toInvertibleUnit(QuantityType<?> source, Unit<?> targetUnit) {
Unit<?> sourceSystemUnit = source.getUnit().getSystemUnit();
if (!targetUnit.equals(sourceSystemUnit) && !targetUnit.isCompatible(AbstractUnit.ONE)
&& sourceSystemUnit.inverse().isCompatible(targetUnit)) {
QuantityType<?> sourceInItsSystemUnit = source.toUnit(sourceSystemUnit);
return sourceInItsSystemUnit != null ? sourceInItsSystemUnit.inverse().toUnit(targetUnit) : null;
}
return source.toUnit(targetUnit);
}
/** /**
* Check if the given {@link State} is allowed. Non -allowed states are those which are a {@link QuantityType} * Check if the given {@link State} is allowed. Non -allowed states are those which are a {@link QuantityType}
* and if there is a 'systemUnit' not compatible with that. * and if there is a 'systemUnit' not compatible with that.