Use `OnOffType.from` to reduce code (#3954)

You can create an `OnOffType` using a boolean nowadays which reduces the amount of code.

Signed-off-by: Wouter Born <github@maindrain.net>
pull/3955/head
Wouter Born 2023-12-23 15:33:07 +01:00 committed by GitHub
parent d1bbaba4d4
commit 11e51abb44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 9 additions and 9 deletions

View File

@ -93,7 +93,7 @@ public class TestPersistenceService implements QueryablePersistenceService {
@Override
public State getState() {
return hours < 5 || hours > 10 ? OnOffType.ON : OnOffType.OFF;
return OnOffType.from(hours < 5 || hours > 10);
}
@Override

View File

@ -77,8 +77,8 @@ public class SystemHysteresisStateProfile implements StateProfile {
final Object paramValue = context.getConfiguration().get(INVERTED_PARAM);
final boolean inverted = paramValue == null ? false : Boolean.valueOf(paramValue.toString());
this.low = inverted ? OnOffType.ON : OnOffType.OFF;
this.high = inverted ? OnOffType.OFF : OnOffType.ON;
this.low = OnOffType.from(inverted);
this.high = OnOffType.from(!inverted);
}
private @Nullable QuantityType<?> getParam(ProfileContext context, String param) {

View File

@ -84,8 +84,8 @@ public class SystemRangeStateProfile implements StateProfile {
final Object paramValue = context.getConfiguration().get(INVERTED_PARAM);
final boolean inverted = paramValue == null ? false : Boolean.valueOf(paramValue.toString());
this.inRange = inverted ? OnOffType.OFF : OnOffType.ON;
this.notInRange = inverted ? OnOffType.ON : OnOffType.OFF;
this.inRange = OnOffType.from(!inverted);
this.notInRange = OnOffType.from(inverted);
}
private @Nullable QuantityType<?> getParam(ProfileContext context, String param) {

View File

@ -187,7 +187,7 @@ public class DecimalType extends Number implements PrimitiveType, State, Command
@Override
public <T extends State> @Nullable T as(@Nullable Class<T> target) {
if (target == OnOffType.class) {
return target.cast(equals(ZERO) ? OnOffType.OFF : OnOffType.ON);
return target.cast(OnOffType.from(!equals(ZERO)));
} else if (target == PercentType.class) {
return target.cast(new PercentType(toBigDecimal().multiply(BIG_DECIMAL_HUNDRED)));
} else if (target == UpDownType.class) {

View File

@ -273,7 +273,7 @@ public class HSBType extends PercentType implements ComplexType, State, Command
public <T extends State> @Nullable T as(@Nullable Class<T> target) {
if (target == OnOffType.class) {
// if brightness is not completely off, we consider the state to be on
return target.cast(PercentType.ZERO.equals(getBrightness()) ? OnOffType.OFF : OnOffType.ON);
return target.cast(OnOffType.from(!PercentType.ZERO.equals(getBrightness())));
} else if (target == DecimalType.class) {
return target.cast(
new DecimalType(getBrightness().toBigDecimal().divide(BIG_DECIMAL_HUNDRED, 8, RoundingMode.UP)));

View File

@ -109,7 +109,7 @@ public class PercentType extends DecimalType {
@Override
public <T extends State> @Nullable T as(@Nullable Class<T> target) {
if (target == OnOffType.class) {
return target.cast(equals(ZERO) ? OnOffType.OFF : OnOffType.ON);
return target.cast(OnOffType.from(!equals(ZERO)));
} else if (target == DecimalType.class) {
return target.cast(new DecimalType(toBigDecimal().divide(BIG_DECIMAL_HUNDRED, 8, RoundingMode.UP)));
} else if (target == UpDownType.class) {

View File

@ -440,7 +440,7 @@ public class QuantityType<T extends Quantity<T>> extends Number
if (intValue() == 0) {
return target.cast(OnOffType.OFF);
} else if (Units.PERCENT.equals(getUnit())) {
return target.cast(toBigDecimal().compareTo(BigDecimal.ZERO) > 0 ? OnOffType.ON : OnOffType.OFF);
return target.cast(OnOffType.from(toBigDecimal().compareTo(BigDecimal.ZERO) > 0));
} else if (toBigDecimal().compareTo(BigDecimal.ONE) == 0) {
return target.cast(OnOffType.ON);
} else {