]> git.basschouten.com Git - openhab-addons.git/commitdiff
Fix or suppress SAT CompareObjectsWithEquals findings (#10631)
authorWouter Born <github@maindrain.net>
Wed, 5 May 2021 19:06:04 +0000 (21:06 +0200)
committerGitHub <noreply@github.com>
Wed, 5 May 2021 19:06:04 +0000 (21:06 +0200)
* Fix or suppress SAT CompareObjectsWithEquals findings

Signed-off-by: Wouter Born <github@maindrain.net>
14 files changed:
bundles/org.openhab.binding.atlona/src/main/java/org/openhab/binding/atlona/internal/StatefulHandlerCallback.java
bundles/org.openhab.binding.bluetooth.bluegiga/src/main/java/org/openhab/binding/bluetooth/bluegiga/BlueGigaBluetoothDevice.java
bundles/org.openhab.binding.bluetooth.roaming/src/main/java/org/openhab/binding/bluetooth/roaming/internal/RoamingBluetoothDevice.java
bundles/org.openhab.binding.bosesoundtouch/src/main/java/org/openhab/binding/bosesoundtouch/internal/ContentItem.java
bundles/org.openhab.binding.fmiweather/src/main/java/org/openhab/binding/fmiweather/internal/AbstractWeatherHandler.java
bundles/org.openhab.binding.irtrans/src/main/java/org/openhab/binding/irtrans/internal/handler/EthernetBridgeHandler.java
bundles/org.openhab.binding.lcn/src/main/java/org/openhab/binding/lcn/internal/connection/AbstractStateMachine.java
bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/message/PowermaxCommManager.java
bundles/org.openhab.binding.russound/src/main/java/org/openhab/binding/russound/internal/rio/StatefulHandlerCallback.java
bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/util/ShellyChannelCache.java
bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/discovery/SmartthingsDiscoveryService.java
bundles/org.openhab.binding.tr064/src/main/java/org/openhab/binding/tr064/internal/phonebook/PhonebookProfile.java
bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/AbstractHomekitAccessoryImpl.java
bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitLockImpl.java

index 04fd2c559cd824726c53c1656784afb5e8cecf7b..29d882245e1d6f778da571f2b8ae165fa1aa0f38 100644 (file)
@@ -13,6 +13,7 @@
 package org.openhab.binding.atlona.internal;
 
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -93,13 +94,8 @@ public class StatefulHandlerCallback implements AtlonaHandlerCallback {
 
         final State oldState = this.state.get(channelId);
 
-        // If both null OR the same value (enums), nothing changed
-        if (oldState == state) {
-            return;
-        }
-
         // If they are equal - nothing changed
-        if (oldState != null && oldState.equals(state)) {
+        if (Objects.equals(oldState, state)) {
             return;
         }
 
index 3fce5a0e451db41f998a5e69c9df9308a3e4dbe5..38ea9907139dd36f9fcdfec424ed483818f64c36 100644 (file)
@@ -185,7 +185,7 @@ public class BlueGigaBluetoothDevice extends BaseBluetoothDevice implements Blue
 
     @Override
     public boolean discoverServices() {
-        if (currentProcedure != PROCEDURE_NONE) {
+        if (!PROCEDURE_NONE.equals(currentProcedure)) {
             return false;
         }
 
@@ -218,7 +218,7 @@ public class BlueGigaBluetoothDevice extends BaseBluetoothDevice implements Blue
                     new BluetoothException("Unable to find CCC for characteristic [" + characteristic.getUuid() + "]"));
         }
 
-        if (currentProcedure != PROCEDURE_NONE) {
+        if (!PROCEDURE_NONE.equals(currentProcedure)) {
             return CompletableFuture.failedFuture(new BluetoothException("Another procedure is already in progress"));
         }
 
@@ -264,7 +264,7 @@ public class BlueGigaBluetoothDevice extends BaseBluetoothDevice implements Blue
                     new BluetoothException("Unable to find CCC for characteristic [" + characteristic.getUuid() + "]"));
         }
 
-        if (currentProcedure != PROCEDURE_NONE) {
+        if (!PROCEDURE_NONE.equals(currentProcedure)) {
             return CompletableFuture.failedFuture(new BluetoothException("Another procedure is already in progress"));
         }
 
@@ -311,7 +311,7 @@ public class BlueGigaBluetoothDevice extends BaseBluetoothDevice implements Blue
             return CompletableFuture.failedFuture(new BluetoothException("Not connected"));
         }
 
-        if (currentProcedure != PROCEDURE_NONE) {
+        if (!PROCEDURE_NONE.equals(currentProcedure)) {
             return CompletableFuture.failedFuture(new BluetoothException("Another procedure is already in progress"));
         }
 
@@ -336,7 +336,7 @@ public class BlueGigaBluetoothDevice extends BaseBluetoothDevice implements Blue
             return CompletableFuture.failedFuture(new BluetoothException("Not connected"));
         }
 
-        if (currentProcedure != PROCEDURE_NONE) {
+        if (!PROCEDURE_NONE.equals(currentProcedure)) {
             return CompletableFuture.failedFuture(new BluetoothException("Another procedure is already in progress"));
         }
 
@@ -564,7 +564,7 @@ public class BlueGigaBluetoothDevice extends BaseBluetoothDevice implements Blue
             return;
         }
 
-        if (currentProcedure == PROCEDURE_NONE) {
+        if (!PROCEDURE_NONE.equals(currentProcedure)) {
             logger.debug("BlueGiga procedure completed but procedure is null with connection {}, address {}",
                     connection, address);
             return;
index 79cbcfd937c87dd1071ad10c52dc672b7c03dfdb..4ad7a0b255a8dcea76231e1263f8f31b728333c7 100644 (file)
@@ -68,6 +68,7 @@ public class RoamingBluetoothDevice extends DelegateBluetoothDevice {
     }
 
     @Override
+    @SuppressWarnings("PMD.CompareObjectsWithEquals")
     protected @Nullable BluetoothDevice getDelegate() {
         BluetoothDevice newDelegate = null;
         int newRssi = Integer.MIN_VALUE;
index 84c5a40e355f4ef81c4c16ea5350429ceea0f71c..85fee270fa0ecb3664c8ea82a8853929d81060f2 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.bosesoundtouch.internal;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.commons.lang3.StringEscapeUtils;
 import org.openhab.core.types.StateOption;
@@ -90,19 +91,19 @@ public class ContentItem {
     public boolean equals(Object obj) {
         if (obj instanceof ContentItem) {
             ContentItem other = (ContentItem) obj;
-            if (!isEqual(other.source, this.source)) {
+            if (!Objects.equals(other.source, this.source)) {
                 return false;
             }
-            if (!isEqual(other.sourceAccount, this.sourceAccount)) {
+            if (!Objects.equals(other.sourceAccount, this.sourceAccount)) {
                 return false;
             }
             if (other.presetable != this.presetable) {
                 return false;
             }
-            if (!isEqual(other.location, this.location)) {
+            if (!Objects.equals(other.location, this.location)) {
                 return false;
             }
-            if (!isEqual(other.itemName, this.itemName)) {
+            if (!Objects.equals(other.itemName, this.itemName)) {
                 return false;
             }
             return true;
@@ -265,14 +266,4 @@ public class ContentItem {
         // }
         return itemName;
     }
-
-    private boolean isEqual(String s1, String s2) {
-        if (s1 == s2) {
-            return true;
-        }
-        if (s1 == null || s2 == null) {
-            return false;
-        }
-        return s1.equals(s2);
-    }
 }
index 42f0cfeda98abde8e2c45ad7ad92ceecfab51e96..c819a90bcde13f9598de67ca31923367a2a252ae 100644 (file)
@@ -80,6 +80,7 @@ public abstract class AbstractWeatherHandler extends BaseThingHandler {
     }
 
     @Override
+    @SuppressWarnings("PMD.CompareObjectsWithEquals")
     public void handleCommand(ChannelUID channelUID, Command command) {
         if (RefreshType.REFRESH == command) {
             ScheduledFuture<?> prevFuture = updateChannelsFutureRef.get();
@@ -94,6 +95,7 @@ public abstract class AbstractWeatherHandler extends BaseThingHandler {
                     logger.trace("REFRESH received. Delaying by {} ms to avoid throttle excessive REFRESH",
                             delayRemainingMillis);
                 }
+                // Compare by reference to check if the future changed
                 if (prevFuture == newFuture) {
                     logger.trace("REFRESH received. Previous refresh ongoing, will wait for it to complete in {} ms",
                             lastRefreshMillis + REFRESH_THROTTLE_MILLIS - System.currentTimeMillis());
index 4992ebc34b27325118cf29310319f343a329dc31..2ffe31d922c738fe47e20cda4c02089cb0559640 100644 (file)
@@ -603,7 +603,7 @@ public class EthernetBridgeHandler extends BaseBridgeHandler implements Transcei
                 SelectionKey selKey = it.next();
                 it.remove();
                 if (selKey.isValid()) {
-                    if (selKey.isAcceptable() && selKey == listenerKey) {
+                    if (selKey.isAcceptable() && selKey.equals(listenerKey)) {
                         try {
                             SocketChannel newChannel = listenerChannel.accept();
                             newChannel.configureBlocking(false);
index 3a60c507e66187f40daf0a35dd909b0d31bad023..225468a8003bd029c8a3a8ac0db3845ec54e1acc 100644 (file)
@@ -58,6 +58,7 @@ public abstract class AbstractStateMachine<T extends AbstractStateMachine<T, U>,
         newState.startWorking();
     }
 
+    @SuppressWarnings("PMD.CompareObjectsWithEquals")
     protected boolean isStateActive(AbstractState<?, ?> otherState) {
         return state == otherState; // compare by identity
     }
index 36d73935debb58904ca2648ba8c591b596bfbcad..64c6aeb6adfa36bb15a7e3f9e377892f6d48f8f8 100644 (file)
@@ -621,6 +621,7 @@ public class PowermaxCommManager implements PowermaxMessageEventListener {
      *
      * @return true if the message was sent or the sending is delayed; false in other cases
      */
+    @SuppressWarnings("PMD.CompareObjectsWithEquals")
     private synchronized boolean sendMessage(PowermaxBaseMessage msg, boolean immediate, int waitTime,
             boolean doNotLog) {
         if ((waitTime > 0) && (msg != null)) {
index e68159458b0dbb07175ef15eb02d5ce620826aad..e6ae4b8cb53ce1f706002c8d736fd7741e699834 100644 (file)
@@ -13,6 +13,7 @@
 package org.openhab.binding.russound.internal.rio;
 
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
@@ -94,13 +95,8 @@ public class StatefulHandlerCallback implements RioHandlerCallback {
 
         final State oldState = state.get(channelId);
 
-        // If both null OR the same value (enums), nothing changed
-        if (oldState == newState) {
-            return;
-        }
-
         // If they are equal - nothing changed
-        if (oldState != null && oldState.equals(newState)) {
+        if (Objects.equals(oldState, newState)) {
             return;
         }
 
index 8f1e1d639d384ce2e0d75be1000420dd133c726f..d5c0b62409815b1a2ff46f06c53689e519088471 100644 (file)
@@ -77,7 +77,7 @@ public class ShellyChannelCache {
                 current = channelData.get(channelId);
             }
             if (!enabled || forceUpdate || (current == null) || !current.equals(newValue)) {
-                if ((current != null) && current.getClass().isEnum() && (current == newValue)) {
+                if ((current != null) && current.getClass().isEnum() && (current.equals(newValue))) {
                     return false; // special case for OnOffType
                 }
                 // For channels that support multiple types (like brightness) a suffix is added
index e051b0f551731e21b80b0dfd96d7f296572eb527..4fcc572aa01383f25042e873f942f5cb7bb5b0ea 100644 (file)
@@ -80,7 +80,7 @@ public class SmartthingsDiscoveryService extends AbstractDiscoveryService implem
 
     protected void unsetSmartthingsHubCommand(SmartthingsHubCommand hubCommand) {
         // Make sure it is this handleFactory that should be unset
-        if (hubCommand == smartthingsHubCommand) {
+        if (Objects.equals(hubCommand, smartthingsHubCommand)) {
             this.smartthingsHubCommand = null;
         }
     }
index 944c87adaaac201f52405086946b5aca34fc0c7e..83cc61a2adbbf2c83128b19a44cc11e4aa98a829 100644 (file)
@@ -131,6 +131,7 @@ public class PhonebookProfile implements StateProfile {
     }
 
     @Override
+    @SuppressWarnings("PMD.CompareObjectsWithEquals")
     public void onStateUpdateFromHandler(State state) {
         if (state instanceof UnDefType) {
             // we cannot adjust UNDEF or NULL values, thus we simply apply them without reporting an error or warning
@@ -139,6 +140,7 @@ public class PhonebookProfile implements StateProfile {
         if (state instanceof StringType) {
             Optional<String> match = resolveNumber(state.toString());
             State newState = match.map(name -> (State) new StringType(name)).orElse(state);
+            // Compare by reference to check if the name is mapped to the same state
             if (newState == state) {
                 logger.debug("Number '{}' not found in phonebook '{}' from provider '{}'", state, phonebookName,
                         thingUID);
index cad68e734e589bcc8e149428a27db6812023bd72..40c401fe2310a4479c7ca6b90b80d453ba5f86de 100644 (file)
@@ -113,6 +113,7 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
         return accessory;
     }
 
+    @Override
     public Collection<Service> getServices() {
         return this.services;
     }
@@ -182,7 +183,7 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
      * return configuration attached to the root accessory, e.g. groupItem.
      * Note: result will be casted to the type of the default value.
      * The type for number is BigDecimal.
-     * 
+     *
      * @param key configuration key
      * @param defaultValue default value
      * @param <T> expected type
@@ -197,7 +198,7 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
      * return configuration of the characteristic item, e.g. currentTemperature.
      * Note: result will be casted to the type of the default value.
      * The type for number is BigDecimal.
-     * 
+     *
      * @param characteristicType characteristic type
      * @param key configuration key
      * @param defaultValue default value
@@ -215,7 +216,7 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
      * update mapping with values from item configuration.
      * it checks for all keys from the mapping whether there is configuration at item with the same key and if yes,
      * replace the value.
-     * 
+     *
      * @param characteristicType characteristicType to identify item
      * @param map mapping to update
      * @param customEnumList list to store custom state enumeration
@@ -247,7 +248,7 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
     /**
      * takes item state as value and retrieves the key for that value from mapping.
      * e.g. used to map StringItem value to HomeKit Enum
-     * 
+     *
      * @param characteristicType characteristicType to identify item
      * @param mapping mapping
      * @param defaultValue default value if nothing found in mapping
@@ -283,7 +284,7 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
 
     @NonNullByDefault
     private <T extends Quantity<T>> double convertAndRound(double value, Unit<T> from, Unit<T> to) {
-        double rawValue = from == to ? value : from.getConverterTo(to).convert(value);
+        double rawValue = from.equals(to) ? value : from.getConverterTo(to).convert(value);
         return new BigDecimal(rawValue).setScale(1, RoundingMode.HALF_UP).doubleValue();
     }
 
@@ -302,7 +303,7 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
 
     /**
      * create boolean reader with ON state mapped to trueOnOffValue or trueOpenClosedValue depending of item type
-     * 
+     *
      * @param characteristicType characteristic id
      * @param trueOnOffValue ON value for switch
      * @param trueOpenClosedValue ON value for contact
@@ -320,7 +321,7 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
 
     /**
      * create boolean reader with default ON/OFF mapping considering inverted flag
-     * 
+     *
      * @param characteristicType characteristic id
      * @return boolean reader
      * @throws IncompleteAccessoryException
index de9379334a30170cf6a745a55dc33382025adedb..468cb9bb2944eee9cd5aae6c23dfc22659ec5101 100644 (file)
@@ -60,7 +60,7 @@ public class HomekitLockImpl extends AbstractHomekitAccessoryImpl implements Loc
             if (state instanceof DecimalType) {
                 lockState = LockCurrentStateEnum.fromCode(((DecimalType) state).intValue());
             } else if (state instanceof OnOffType) {
-                lockState = state == securedState ? LockCurrentStateEnum.SECURED : LockCurrentStateEnum.UNSECURED;
+                lockState = state.equals(securedState) ? LockCurrentStateEnum.SECURED : LockCurrentStateEnum.UNSECURED;
             }
         }
         return CompletableFuture.completedFuture(lockState);