From f376606e921a7c93369b878e2d5c9660ca173322 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Thu, 28 Dec 2023 13:19:01 +0100 Subject: [PATCH] Simplify boolean expressions (#3971) Signed-off-by: Wouter Born --- .../config/core/ConfigDescriptionParameterGroupBuilder.java | 3 +-- .../org/openhab/core/config/core/ConfigurableServiceUtil.java | 2 +- .../config/core/xml/ConfigDescriptionParameterConverter.java | 2 +- .../openhab/core/config/discovery/addon/BaseAddonFinder.java | 2 +- .../internal/pooling/ModbusSlaveConnectionFactoryImpl.java | 4 ++-- .../org/openhab/core/model/script/lib/NumberExtensions.java | 2 +- .../core/persistence/filter/PersistenceEqualsFilter.java | 2 +- .../core/persistence/filter/PersistenceIncludeFilter.java | 2 +- .../core/persistence/filter/PersistenceThresholdFilter.java | 2 +- .../core/internal/types/StateDescriptionFragmentImpl.java | 3 +-- .../src/main/java/org/openhab/core/tools/UpgradeTool.java | 2 +- 11 files changed, 12 insertions(+), 14 deletions(-) diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigDescriptionParameterGroupBuilder.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigDescriptionParameterGroupBuilder.java index 920531a99..948d95a6e 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigDescriptionParameterGroupBuilder.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigDescriptionParameterGroupBuilder.java @@ -94,7 +94,6 @@ public class ConfigDescriptionParameterGroupBuilder { * @return the desired result */ public ConfigDescriptionParameterGroup build() throws IllegalArgumentException { - return new ConfigDescriptionParameterGroup(name, context, advanced != null ? advanced : false, label, - description); + return new ConfigDescriptionParameterGroup(name, context, advanced != null && advanced, label, description); } } diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigurableServiceUtil.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigurableServiceUtil.java index afd7b059d..0815f0573 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigurableServiceUtil.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/ConfigurableServiceUtil.java @@ -91,6 +91,6 @@ public class ConfigurableServiceUtil { private static boolean resolveBoolean(Function propertyResolver, String key) { Boolean value = (Boolean) propertyResolver.apply(key); - return value == null ? false : value.booleanValue(); + return value != null && value.booleanValue(); } } diff --git a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/xml/ConfigDescriptionParameterConverter.java b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/xml/ConfigDescriptionParameterConverter.java index 7574db2d7..448c510cf 100644 --- a/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/xml/ConfigDescriptionParameterConverter.java +++ b/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/xml/ConfigDescriptionParameterConverter.java @@ -88,7 +88,7 @@ public class ConfigDescriptionParameterConverter extends GenericUnmarshaller propertyPatternMap, String propertyName, @Nullable String propertyValue) { Pattern pattern = propertyPatternMap.get(propertyName); - return pattern == null ? true : propertyValue == null ? false : pattern.matcher(propertyValue).matches(); + return pattern == null || (propertyValue != null && pattern.matcher(propertyValue).matches()); } protected volatile List addonCandidates = List.of(); diff --git a/bundles/org.openhab.core.io.transport.modbus/src/main/java/org/openhab/core/io/transport/modbus/internal/pooling/ModbusSlaveConnectionFactoryImpl.java b/bundles/org.openhab.core.io.transport.modbus/src/main/java/org/openhab/core/io/transport/modbus/internal/pooling/ModbusSlaveConnectionFactoryImpl.java index 2ffca7017..e28d381fc 100644 --- a/bundles/org.openhab.core.io.transport.modbus/src/main/java/org/openhab/core/io/transport/modbus/internal/pooling/ModbusSlaveConnectionFactoryImpl.java +++ b/bundles/org.openhab.core.io.transport.modbus/src/main/java/org/openhab/core/io/transport/modbus/internal/pooling/ModbusSlaveConnectionFactoryImpl.java @@ -105,8 +105,8 @@ public class ModbusSlaveConnectionFactoryImpl long reconnectAfterMillis = configuration.getReconnectAfterMillis(); long connectionAgeMillis = System.currentTimeMillis() - localLastConnected; long disconnectIfConnectedBeforeMillis = disconnectIfConnectedBefore.getOrDefault(localEndpoint, -1L); - boolean disconnectSinceTooOldConnection = disconnectIfConnectedBeforeMillis < 0L ? false - : localLastConnected <= disconnectIfConnectedBeforeMillis; + boolean disconnectSinceTooOldConnection = disconnectIfConnectedBeforeMillis >= 0L + && localLastConnected <= disconnectIfConnectedBeforeMillis; boolean shouldBeDisconnected = (reconnectAfterMillis == 0 || (reconnectAfterMillis > 0 && connectionAgeMillis > reconnectAfterMillis) || disconnectSinceTooOldConnection); diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/lib/NumberExtensions.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/lib/NumberExtensions.java index 4b05df4e8..6826955a4 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/lib/NumberExtensions.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/lib/NumberExtensions.java @@ -164,7 +164,7 @@ public class NumberExtensions { BigDecimal leftValue = numberToBigDecimal(left); BigDecimal rightValue = numberToBigDecimal(right); if (leftValue == null) { - return (rightValue != null) ? false : true; + return rightValue == null; } else if (rightValue == null) { return true; } else { diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceEqualsFilter.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceEqualsFilter.java index 45ccbc69c..e374d5546 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceEqualsFilter.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceEqualsFilter.java @@ -33,7 +33,7 @@ public class PersistenceEqualsFilter extends PersistenceFilter { public PersistenceEqualsFilter(String name, Collection values, @Nullable Boolean inverted) { super(name); this.values = values; - this.inverted = (inverted == null) ? false : inverted; + this.inverted = inverted != null && inverted; } public Collection getValues() { diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceIncludeFilter.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceIncludeFilter.java index 2ca7fc364..8203e0d3d 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceIncludeFilter.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceIncludeFilter.java @@ -45,7 +45,7 @@ public class PersistenceIncludeFilter extends PersistenceFilter { this.lower = lower; this.upper = upper; this.unit = (unit == null) ? "" : unit; - this.inverted = (inverted == null) ? false : inverted; + this.inverted = inverted != null && inverted; } public BigDecimal getLower() { diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceThresholdFilter.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceThresholdFilter.java index b841c0548..ad088c944 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceThresholdFilter.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/filter/PersistenceThresholdFilter.java @@ -53,7 +53,7 @@ public class PersistenceThresholdFilter extends PersistenceFilter { super(name); this.value = value; this.unit = (unit == null) ? "" : unit; - this.relative = (relative == null) ? false : relative; + this.relative = relative != null && relative; } public BigDecimal getValue() { diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/types/StateDescriptionFragmentImpl.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/types/StateDescriptionFragmentImpl.java index 18bbbdc70..6d534bff2 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/types/StateDescriptionFragmentImpl.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/internal/types/StateDescriptionFragmentImpl.java @@ -169,8 +169,7 @@ public class StateDescriptionFragmentImpl implements StateDescriptionFragment { return null; } final Boolean ro = readOnly; - return new StateDescriptionImpl(minimum, maximum, step, pattern, ro == null ? false : ro.booleanValue(), - options); + return new StateDescriptionImpl(minimum, maximum, step, pattern, ro != null && ro.booleanValue(), options); } /** diff --git a/tools/upgradetool/src/main/java/org/openhab/core/tools/UpgradeTool.java b/tools/upgradetool/src/main/java/org/openhab/core/tools/UpgradeTool.java index dd8f20c28..4c41c44be 100644 --- a/tools/upgradetool/src/main/java/org/openhab/core/tools/UpgradeTool.java +++ b/tools/upgradetool/src/main/java/org/openhab/core/tools/UpgradeTool.java @@ -71,7 +71,7 @@ public class UpgradeTool { "Please either set the environment variable ${OPENHAB_USERDATA} or provide a directory through the --dir option."); System.exit(0); } else { - boolean force = commandLine.hasOption(OPT_FORCE) ? true : false; + boolean force = commandLine.hasOption(OPT_FORCE); Upgrader upgrader = new Upgrader(baseDir, force); if (!commandLine.hasOption(OPT_COMMAND)