]> git.basschouten.com Git - openhab-addons.git/commitdiff
[bluetooth] Null annotations and SAT (#13967)
authorlsiepel <leosiepel@gmail.com>
Mon, 6 Mar 2023 08:38:57 +0000 (09:38 +0100)
committerGitHub <noreply@github.com>
Mon, 6 Mar 2023 08:38:57 +0000 (09:38 +0100)
* null annotation, checkstyle, dependency

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
21 files changed:
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothAddress.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCharacteristic.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothClass.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCompletionStatus.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothDescriptor.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothService.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothUtils.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/ConnectedBluetoothHandler.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/BluetoothDiscoveryDevice.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDeviceSnapshot.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryProcess.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothConnectionStatusNotification.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothNotification.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothScanNotification.java
bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/util/StringUtil.java [new file with mode: 0644]
bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/BluetoothAddressTest.java
bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/CharacteristicPropertiesTest.java
bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java
bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java
bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/util/RetryFutureTest.java

index 358f04bf831179c93d71af2a02649d20975c8b10..5fe5d41091e2c7e6263738591b1dd5928757e921 100644 (file)
  */
 package org.openhab.binding.bluetooth;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
 /**
  * The {@link BluetoothAddress} class defines a bluetooth address
  *
  * @author Chris Jackson - Initial contribution
  */
+@NonNullByDefault
 public class BluetoothAddress {
 
     public static final int BD_ADDRESS_LENGTH = 17;
@@ -28,7 +32,7 @@ public class BluetoothAddress {
      *
      * @param address the device address
      */
-    public BluetoothAddress(String address) {
+    public BluetoothAddress(@Nullable String address) {
         if (address == null || address.length() != BD_ADDRESS_LENGTH) {
             throw new IllegalArgumentException("BT Address cannot be null and must be in format XX:XX:XX:XX:XX:XX");
         }
@@ -58,12 +62,12 @@ public class BluetoothAddress {
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((address == null) ? 0 : address.hashCode());
+        result = prime * result + address.hashCode();
         return result;
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(@Nullable Object obj) {
         if (this == obj) {
             return true;
         }
@@ -74,14 +78,8 @@ public class BluetoothAddress {
             return false;
         }
         BluetoothAddress other = (BluetoothAddress) obj;
-        if (address == null) {
-            if (other.address != null) {
-                return false;
-            }
-        } else if (!address.equals(other.address)) {
-            return false;
-        }
-        return true;
+
+        return address.equals(other.address);
     }
 
     @Override
index 6c5b1fd38a115be6ec0c069b0c70b26dc3295257..a0620334014a69321ccca339e47b741bb83a92e8 100644 (file)
@@ -18,8 +18,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 
 /**
  * The {@link BluetoothCharacteristic} class defines the Bluetooth characteristic.
@@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
  * @author Kai Kreuzer - Cleaned up code
  * @author Peter Rosenberg - Improve properties support
  */
+@NonNullByDefault
 public class BluetoothCharacteristic {
     public static final int PROPERTY_BROADCAST = 0x01;
     public static final int PROPERTY_READ = 0x02;
@@ -55,8 +56,6 @@ public class BluetoothCharacteristic {
     public static final int WRITE_TYPE_NO_RESPONSE = 0x01;
     public static final int WRITE_TYPE_SIGNED = 0x04;
 
-    private final Logger logger = LoggerFactory.getLogger(BluetoothCharacteristic.class);
-
     /**
      * The {@link UUID} for this characteristic
      */
@@ -79,7 +78,7 @@ public class BluetoothCharacteristic {
     /**
      * The {@link BluetoothService} to which this characteristic belongs
      */
-    protected BluetoothService service;
+    protected @Nullable BluetoothService service;
 
     /**
      * Create a new BluetoothCharacteristic.
@@ -217,7 +216,7 @@ public class BluetoothCharacteristic {
      *
      * @return the {@link BluetoothService}
      */
-    public BluetoothService getService() {
+    public @Nullable BluetoothService getService() {
         return service;
     }
 
@@ -253,22 +252,23 @@ public class BluetoothCharacteristic {
      *
      * @return the {@link BluetoothDescriptor}
      */
-    public BluetoothDescriptor getDescriptor(UUID uuid) {
+    public @Nullable BluetoothDescriptor getDescriptor(UUID uuid) {
         return gattDescriptors.get(uuid);
     }
 
     @Override
     public int hashCode() {
+        BluetoothService btService = service;
         final int prime = 31;
         int result = 1;
         result = prime * result + instance;
-        result = prime * result + ((service == null) ? 0 : service.hashCode());
-        result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
+        result = prime * result + ((btService == null) ? 0 : btService.hashCode());
+        result = prime * result + uuid.hashCode();
         return result;
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(@Nullable Object obj) {
         if (this == obj) {
             return true;
         }
@@ -282,24 +282,19 @@ public class BluetoothCharacteristic {
         if (instance != other.instance) {
             return false;
         }
-        if (service == null) {
+        BluetoothService btService = service;
+        if (btService == null) {
             if (other.service != null) {
                 return false;
             }
-        } else if (!service.equals(other.service)) {
-            return false;
-        }
-        if (uuid == null) {
-            if (other.uuid != null) {
-                return false;
-            }
-        } else if (!uuid.equals(other.uuid)) {
+        } else if (!btService.equals(other.service)) {
             return false;
         }
-        return true;
+
+        return uuid.equals(other.uuid);
     }
 
-    public GattCharacteristic getGattCharacteristic() {
+    public @Nullable GattCharacteristic getGattCharacteristic() {
         return GattCharacteristic.getCharacteristic(uuid);
     }
 
@@ -410,7 +405,7 @@ public class BluetoothCharacteristic {
         REMOVABLE(0x2A3A),
         SERVICE_REQUIRED(0x2A3B);
 
-        private static Map<UUID, GattCharacteristic> uuidToServiceMapping;
+        private static @Nullable Map<UUID, GattCharacteristic> uuidToServiceMapping;
 
         private UUID uuid;
 
@@ -418,18 +413,16 @@ public class BluetoothCharacteristic {
             this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
         }
 
-        private static void initMapping() {
-            uuidToServiceMapping = new HashMap<>();
-            for (GattCharacteristic s : values()) {
-                uuidToServiceMapping.put(s.uuid, s);
-            }
-        }
-
-        public static GattCharacteristic getCharacteristic(UUID uuid) {
-            if (uuidToServiceMapping == null) {
-                initMapping();
+        public static @Nullable GattCharacteristic getCharacteristic(UUID uuid) {
+            Map<UUID, GattCharacteristic> localServiceMapping = uuidToServiceMapping;
+            if (localServiceMapping == null) {
+                localServiceMapping = new HashMap<>();
+                for (GattCharacteristic s : values()) {
+                    localServiceMapping.put(s.uuid, s);
+                }
+                uuidToServiceMapping = localServiceMapping;
             }
-            return uuidToServiceMapping.get(uuid);
+            return localServiceMapping.get(uuid);
         }
 
         /**
index c8c6123f7d771d7e8fde368dbc69f0a8992cdacd..57a0d7c25e76208500a2b47c4b463227c20045ea 100644 (file)
  */
 package org.openhab.binding.bluetooth;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * Represents a Bluetooth class, which describes the general characteristics and capabilities of a device.
  *
  * @author Chris Jackson - Initial Contribution
  *
  */
+@NonNullByDefault
 public class BluetoothClass {
     private final int clazz;
 
index 4c5931ab088895ef7941a4bb83678cbf60ec2af0..1e29a4e0980c8233fa2134eaf8b4c911fa5cb841 100644 (file)
  */
 package org.openhab.binding.bluetooth;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * An enumeration of transaction completion status values
  *
  * @author Chris Jackson - Initial contribution
  *
  */
+@NonNullByDefault
 public enum BluetoothCompletionStatus {
     SUCCESS,
     ERROR
index 92a3bb2e500b7b5898c433b328057714d0dce6b9..0f9ac4c6f194379c6fadad7540dc111f51dd7773 100644 (file)
@@ -16,6 +16,9 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
 /**
  * The {@link BluetoothDescriptor} class defines the Bluetooth descriptor.
  * <p>
@@ -26,6 +29,7 @@ import java.util.UUID;
  * @author Chris Jackson - Initial contribution
  * @author Kai Kreuzer - added constructor and fixed setValue method
  */
+@NonNullByDefault
 public class BluetoothDescriptor {
 
     protected final BluetoothCharacteristic characteristic;
@@ -81,7 +85,7 @@ public class BluetoothDescriptor {
         return handle;
     }
 
-    public GattDescriptor getDescriptor() {
+    public @Nullable GattDescriptor getDescriptor() {
         return GattDescriptor.getDescriptor(uuid);
     }
 
@@ -99,7 +103,7 @@ public class BluetoothDescriptor {
         NUMBER_OF_DIGITALS(0x2909),
         TRIGGER_SETTING(0x290A);
 
-        private static Map<UUID, GattDescriptor> uuidToServiceMapping;
+        private static @Nullable Map<UUID, GattDescriptor> uuidToServiceMapping;
 
         private final UUID uuid;
 
@@ -107,18 +111,16 @@ public class BluetoothDescriptor {
             this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
         }
 
-        private static void initMapping() {
-            uuidToServiceMapping = new HashMap<>();
-            for (GattDescriptor s : values()) {
-                uuidToServiceMapping.put(s.uuid, s);
-            }
-        }
-
-        public static GattDescriptor getDescriptor(UUID uuid) {
-            if (uuidToServiceMapping == null) {
-                initMapping();
+        public static @Nullable GattDescriptor getDescriptor(UUID uuid) {
+            Map<UUID, GattDescriptor> localServiceMapping = uuidToServiceMapping;
+            if (localServiceMapping == null) {
+                localServiceMapping = new HashMap<>();
+                for (GattDescriptor s : values()) {
+                    localServiceMapping.put(s.uuid, s);
+                }
+                uuidToServiceMapping = localServiceMapping;
             }
-            return uuidToServiceMapping.get(uuid);
+            return localServiceMapping.get(uuid);
         }
 
         /**
index d2642f13a9a3e1b08c6fd4125565f1454cb1eb3d..ad417e04b9bdb67695bf0f9d48bfce230d2b1418 100644 (file)
@@ -19,6 +19,9 @@ import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
 /**
  * The {@link BluetoothCharacteristic} class defines the BLE Service.
  * <p>
@@ -30,6 +33,7 @@ import java.util.concurrent.ConcurrentHashMap;
  * @author Chris Jackson - Initial contribution
  * @author Kai Kreuzer - Cleaned up code
  */
+@NonNullByDefault
 public class BluetoothService {
 
     // The service UUID
@@ -92,11 +96,11 @@ public class BluetoothService {
     }
 
     /**
-     * Get characteristic based on {@link UUID}
+     * Get characteristic based on {@link UUID}, null if it is not known
      *
      * @return the {@link BluetoothCharacteristic} with the requested {@link UUID}
      */
-    public BluetoothCharacteristic getCharacteristic(UUID uuid) {
+    public @Nullable BluetoothCharacteristic getCharacteristic(UUID uuid) {
         return supportedCharacteristics.get(uuid);
     }
 
@@ -185,7 +189,7 @@ public class BluetoothService {
      * @param handle the handle of the characteristic to return
      * @return return the {@link BluetoothCharacteristic} or null if not found
      */
-    public BluetoothCharacteristic getCharacteristicByHandle(int handle) {
+    public @Nullable BluetoothCharacteristic getCharacteristicByHandle(int handle) {
         synchronized (supportedCharacteristics) {
             for (BluetoothCharacteristic characteristic : supportedCharacteristics.values()) {
                 if (characteristic.getHandle() == handle) {
@@ -201,7 +205,7 @@ public class BluetoothService {
      *
      * @return the {@link GattService} relating to this service
      */
-    public GattService getService() {
+    public @Nullable GattService getService() {
         return GattService.getService(uuid);
     }
 
@@ -241,7 +245,7 @@ public class BluetoothService {
         USER_DATA(0x181C),
         WEIGHT_SCALE(0x181D);
 
-        private static Map<UUID, GattService> uuidToServiceMapping;
+        private static @Nullable Map<UUID, GattService> uuidToServiceMapping;
 
         private UUID uuid;
 
@@ -249,18 +253,16 @@ public class BluetoothService {
             this.uuid = BluetoothBindingConstants.createBluetoothUUID(key);
         }
 
-        private static void initMapping() {
-            uuidToServiceMapping = new HashMap<>();
-            for (GattService s : values()) {
-                uuidToServiceMapping.put(s.uuid, s);
-            }
-        }
-
-        public static GattService getService(UUID uuid) {
-            if (uuidToServiceMapping == null) {
-                initMapping();
+        public static @Nullable GattService getService(UUID uuid) {
+            Map<UUID, GattService> localServiceMapping = uuidToServiceMapping;
+            if (localServiceMapping == null) {
+                localServiceMapping = new HashMap<>();
+                for (GattService s : values()) {
+                    localServiceMapping.put(s.uuid, s);
+                }
+                uuidToServiceMapping = localServiceMapping;
             }
-            return uuidToServiceMapping.get(uuid);
+            return localServiceMapping.get(uuid);
         }
 
         /**
index e95294d4e34793fd8e7507faecc98bdb240fde87..23229affe03c14a64ce5657a7047405c5666480e 100644 (file)
@@ -14,6 +14,8 @@ package org.openhab.binding.bluetooth;
 
 import java.nio.charset.StandardCharsets;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -23,6 +25,7 @@ import org.slf4j.LoggerFactory;
  * @author Connor Petty - Initial Contribution
  *
  */
+@NonNullByDefault
 public class BluetoothUtils {
 
     public static final Logger logger = LoggerFactory.getLogger(BluetoothUtils.class);
@@ -43,20 +46,12 @@ public class BluetoothUtils {
      * @return
      */
     public static int[] toIntArray(byte[] value) {
-        if (value == null) {
-            return null;
-        }
         int[] ret = new int[value.length];
-        for (int i = 0; i < value.length; i++) {
-            ret[i] = value[i];
-        }
+        System.arraycopy(value, 0, ret, 0, value.length);
         return ret;
     }
 
     public static byte[] toByteArray(int[] value) {
-        if (value == null) {
-            return null;
-        }
         byte[] ret = new byte[value.length];
         for (int i = 0; i < value.length; i++) {
             ret[i] = (byte) (value[i] & 0xFF);
@@ -68,7 +63,7 @@ public class BluetoothUtils {
      * Return the stored value of this characteristic.
      *
      */
-    public static Integer getIntegerValue(byte[] value, int formatType, int offset) {
+    public static @Nullable Integer getIntegerValue(byte[] value, int formatType, int offset) {
         if ((offset + getTypeLen(formatType)) > value.length) {
             return null;
         }
@@ -103,7 +98,7 @@ public class BluetoothUtils {
      * Return the stored value of this characteristic. This doesn't read the remote data.
      *
      */
-    public static Float getFloatValue(byte[] value, int formatType, int offset) {
+    public static @Nullable Float getFloatValue(byte[] value, int formatType, int offset) {
         if ((offset + getTypeLen(formatType)) > value.length) {
             return null;
         }
@@ -124,8 +119,8 @@ public class BluetoothUtils {
      * Return the stored value of this characteristic. This doesn't read the remote data.
      *
      */
-    public static String getStringValue(byte[] value, int offset) {
-        if (value == null || offset > value.length) {
+    public static @Nullable String getStringValue(byte[] value, int offset) {
+        if (offset > value.length) {
             return null;
         }
         byte[] strBytes = new byte[value.length - offset];
@@ -145,7 +140,7 @@ public class BluetoothUtils {
      */
     public static boolean setValue(byte[] dest, int value, int formatType, int offset) {
         int len = offset + getTypeLen(formatType);
-        if (dest == null || len > dest.length) {
+        if (len > dest.length) {
             return false;
         }
         int val = value;
@@ -193,7 +188,7 @@ public class BluetoothUtils {
      */
     public static boolean setValue(byte[] dest, int mantissa, int exponent, int formatType, int offset) {
         int len = offset + getTypeLen(formatType);
-        if (dest == null || len > dest.length) {
+        if (len > dest.length) {
             return false;
         }
 
index e432f88ccfa07639421558237cdd2e9bb6dbf31d..4ca5dc45566e84ea0612484d455d4189bacf156c 100644 (file)
@@ -60,7 +60,6 @@ public class ConnectedBluetoothHandler extends BeaconBluetoothHandler {
 
     @Override
     public void initialize() {
-
         // super.initialize adds callbacks that might require the connectionTaskExecutor to be present, so we initialize
         // the connectionTaskExecutor first
         ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1,
index 62ebbc3d2ba4e86c933e28608c7abfe365e3c99a..3acf2bd440dbda8b74a817207c982c4b157225ef 100644 (file)
@@ -12,7 +12,6 @@
  */
 package org.openhab.binding.bluetooth.discovery;
 
-import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.bluetooth.BluetoothCompanyIdentifiers;
@@ -29,7 +28,7 @@ import org.openhab.binding.bluetooth.DelegateBluetoothDevice;
 @NonNullByDefault
 public class BluetoothDiscoveryDevice extends DelegateBluetoothDevice {
 
-    private BluetoothDevice delegate;
+    private @NonNullByDefault({}) BluetoothDevice delegate;
 
     protected @Nullable String model;
     protected @Nullable String serialNumber;
@@ -43,7 +42,7 @@ public class BluetoothDiscoveryDevice extends DelegateBluetoothDevice {
     }
 
     @Override
-    protected @NonNull BluetoothDevice getDelegate() {
+    protected @Nullable BluetoothDevice getDelegate() {
         return delegate;
     }
 
index f192d7c41fbeb7e18bd1e783bc0d5472110ef893..e813a306d73b8fba94f75f836c0a42cb6dba935b 100644 (file)
@@ -202,10 +202,7 @@ public class BluetoothDeviceSnapshot extends BluetoothDiscoveryDevice {
         if (!Objects.equals(firmwareRevision, other.firmwareRevision)) {
             return false;
         }
-        if (!Objects.equals(softwareRevision, other.softwareRevision)) {
-            return false;
-        }
-        return true;
+        return Objects.equals(softwareRevision, other.softwareRevision);
     }
 
     /**
index d048051ed67f06b4c4b8c159dc618978c773410d..323f63dd710116408790130c942c7106276d9186 100644 (file)
@@ -221,7 +221,10 @@ public class BluetoothDiscoveryProcess implements Supplier<DiscoveryResult> {
         }
         try {
             byte[] value = device.readCharacteristic(characteristic).get(1, TimeUnit.SECONDS);
-            consumer.accept(BluetoothUtils.getStringValue(value, 0));
+            String strValue = BluetoothUtils.getStringValue(value, 0);
+            if (strValue != null) {
+                consumer.accept(strValue);
+            }
         } catch (ExecutionException e) {
             logger.debug("Failed to aquire uuid {} from device {}: {}", uuid, device.getAddress(), e.getMessage());
         } catch (TimeoutException e) {
index 6402af6474855574f210b62035f40e4cfade7dc2..129873f0e75fc48153de431b2a94e8cd863a94d4 100644 (file)
@@ -190,7 +190,11 @@ public class BluetoothDiscoveryService extends AbstractDiscoveryService implemen
             // we remove any discoveries that have been published for this device
             BluetoothAdapter adapter = device.getAdapter();
             if (discoveryFutures.containsKey(adapter)) {
-                discoveryFutures.remove(adapter).future.thenAccept(result -> retractDiscoveryResult(adapter, result));
+                @Nullable
+                SnapshotFuture ssFuture = discoveryFutures.remove(adapter);
+                if (ssFuture != null) {
+                    ssFuture.future.thenAccept(result -> retractDiscoveryResult(adapter, result));
+                }
             }
             if (discoveryFutures.isEmpty()) {
                 return null;
@@ -253,6 +257,8 @@ public class BluetoothDiscoveryService extends AbstractDiscoveryService implemen
             if (discoveryFutures.containsKey(adapter)) {
                 // now we need to make sure that we remove the old discovered result if it is different from the new
                 // one.
+
+                @Nullable
                 SnapshotFuture oldSF = discoveryFutures.get(adapter);
                 future = oldSF.future.thenCombine(future, (oldResult, newResult) -> {
                     logger.trace("\n old: {}\n new: {}", oldResult, newResult);
index b1b64a63eef9f006aaf820d7f7ec3edd96b99c4b..c7ce0cadd2aa331907f51e418dcf0cc97bf074e1 100644 (file)
@@ -12,6 +12,7 @@
  */
 package org.openhab.binding.bluetooth.notification;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.bluetooth.BluetoothDevice.ConnectionState;
 
 /**
@@ -19,6 +20,7 @@ import org.openhab.binding.bluetooth.BluetoothDevice.ConnectionState;
  *
  * @author Chris Jackson - Initial contribution
  */
+@NonNullByDefault
 public class BluetoothConnectionStatusNotification extends BluetoothNotification {
     private ConnectionState connectionState;
 
index 2e1f6e5e0e61392df452f3e77eace897c3220dbb..0b418a1917d658243236e3a927e34bc390a4fcf7 100644 (file)
@@ -12,6 +12,8 @@
  */
 package org.openhab.binding.bluetooth.notification;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.bluetooth.BluetoothAddress;
 
 /**
@@ -19,13 +21,15 @@ import org.openhab.binding.bluetooth.BluetoothAddress;
  *
  * @author Chris Jackson - Initial contribution
  */
+@NonNullByDefault
 public abstract class BluetoothNotification {
-    protected BluetoothAddress address;
+
+    protected @Nullable BluetoothAddress address;
 
     /**
      * Returns the bluetooth address for this frame
      */
-    public BluetoothAddress getAddress() {
+    public @Nullable BluetoothAddress getAddress() {
         return address;
     }
 }
index 04ae0f2b20db227b5ab36fde12050d332086b15d..77ba53d13c0b3bf2763c985b5a189628ab48e108 100644 (file)
  */
 package org.openhab.binding.bluetooth.notification;
 
+import java.util.HashMap;
 import java.util.Map;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
 /**
  * The {@link BluetoothScanNotification} provides a notification of a received scan packet
  *
  * @author Chris Jackson - Initial contribution
  * @author Peter Rosenberg - Add support for ServiceData
  */
+@NonNullByDefault
 public class BluetoothScanNotification extends BluetoothNotification {
     /**
      * The receive signal strength for this beacon packet
@@ -29,19 +33,19 @@ public class BluetoothScanNotification extends BluetoothNotification {
     /**
      * The raw data
      */
-    private byte[] data = null;
+    private byte[] data = new byte[0];
 
     /**
      * The manufacturer specific data
      */
-    private byte[] manufacturerData = null;
+    private byte[] manufacturerData = new byte[0];
 
     /**
      * The service data.
      * Key: UUID of the service
      * Value: Data of the characteristic
      */
-    private Map<String, byte[]> serviceData = null;
+    private Map<String, byte[]> serviceData = new HashMap<String, byte[]>();
 
     /**
      * The beacon type
diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/util/StringUtil.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/util/StringUtil.java
new file mode 100644 (file)
index 0000000..6365665
--- /dev/null
@@ -0,0 +1,47 @@
+/**
+ * Copyright (c) 2010-2023 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.bluetooth.util;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * This is a n string utility class
+ *
+ * @author Leo Siepel - Initial contribution
+ *
+ */
+@NonNullByDefault
+public class StringUtil {
+
+    public static String randomString(int length, String charset) {
+        StringBuilder sb = new StringBuilder(length);
+        for (int i = 0; i < length; i++) {
+            int index = (int) (charset.length() * Math.random());
+            sb.append(charset.charAt(index));
+        }
+
+        return sb.toString();
+    }
+
+    public static String randomAlphabetic(int length) {
+        return StringUtil.randomString(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz");
+    }
+
+    public static String randomHex(int length) {
+        return StringUtil.randomString(length, "0123456789ABCDEF");
+    }
+
+    public static String randomAlphanummeric(int length) {
+        return StringUtil.randomString(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvxyz");
+    }
+}
index 04bfd6a6c8f6d6db22251c3c107e3fd1af456f77..a00f3b24cd114caa468659489ba441dea36ab3e9 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.bluetooth;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -21,6 +22,7 @@ import org.junit.jupiter.api.Test;
  *
  * @author Kai Kreuzer - Initial contribution
  */
+@NonNullByDefault
 public class BluetoothAddressTest {
 
     @Test
index 089425bbc079c8151161d5190dbd3725637d068e..85dd0bb1e8c68e5846f7469c447674bd65080fb5 100644 (file)
@@ -16,6 +16,7 @@ import static org.junit.jupiter.api.Assertions.*;
 
 import java.util.UUID;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -23,6 +24,7 @@ import org.junit.jupiter.api.Test;
  *
  * @author Peter Rosenberg - Initial contribution
  */
+@NonNullByDefault
 public class CharacteristicPropertiesTest {
     private BluetoothCharacteristic characteristic = new BluetoothCharacteristic(UUID.randomUUID(), 0);
 
index ce1a0427ebaabe713f192fa66ea0f354307290f5..0303ba6818fb8dd56f45d15926ccd19b08ee2247 100644 (file)
@@ -12,8 +12,8 @@
  */
 package org.openhab.binding.bluetooth;
 
-import org.apache.commons.lang3.RandomStringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.openhab.binding.bluetooth.util.StringUtil;
 import org.openhab.core.thing.ThingUID;
 
 /**
@@ -27,14 +27,14 @@ public class TestUtils {
     public static BluetoothAddress randomAddress() {
         StringBuilder builder = new StringBuilder();
         for (int i = 0; i < 5; i++) {
-            builder.append(RandomStringUtils.random(2, "0123456789ABCDEF"));
+            builder.append(StringUtil.randomHex(2));
             builder.append(":");
         }
-        builder.append(RandomStringUtils.random(2, "0123456789ABCDEF"));
+        builder.append(StringUtil.randomHex(2));
         return new BluetoothAddress(builder.toString());
     }
 
     public static ThingUID randomThingUID() {
-        return new ThingUID(BluetoothBindingConstants.BINDING_ID, RandomStringUtils.randomAlphabetic(6));
+        return new ThingUID(BluetoothBindingConstants.BINDING_ID, StringUtil.randomAlphabetic(6));
     }
 }
index 4204b744c81870c0ee7f265b0ff28e5d445e4f6e..9b362883cb6eaa1ebeeeebe4b2b6c312fa3e59a7 100644 (file)
@@ -23,8 +23,6 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.BiConsumer;
 
-import org.apache.commons.lang3.RandomStringUtils;
-import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.junit.jupiter.api.BeforeEach;
@@ -49,13 +47,12 @@ import org.openhab.binding.bluetooth.TestUtils;
 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
 import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
+import org.openhab.binding.bluetooth.util.StringUtil;
 import org.openhab.core.config.discovery.DiscoveryListener;
 import org.openhab.core.config.discovery.DiscoveryResult;
 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
 import org.openhab.core.thing.ThingTypeUID;
 import org.openhab.core.thing.ThingUID;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Tests {@link BluetoothDiscoveryService}.
@@ -69,8 +66,6 @@ public class BluetoothDiscoveryServiceTest {
 
     private static final int TIMEOUT = 2000;
 
-    private final Logger logger = LoggerFactory.getLogger(BluetoothDiscoveryServiceTest.class);
-
     private @NonNullByDefault({}) BluetoothDiscoveryService discoveryService;
 
     private @Spy @NonNullByDefault({}) MockDiscoveryParticipant participant1 = new MockDiscoveryParticipant();
@@ -201,7 +196,7 @@ public class BluetoothDiscoveryServiceTest {
 
         MockBluetoothAdapter mockAdapter1 = new MockBluetoothAdapter();
         MockBluetoothDevice mockDevice = mockAdapter1.getDevice(address);
-        String deviceName = RandomStringUtils.randomAlphanumeric(10);
+        String deviceName = StringUtil.randomAlphanummeric(10);
         mockDevice.setDeviceName(deviceName);
 
         BluetoothDevice device = Mockito.spy(mockDevice);
@@ -228,7 +223,7 @@ public class BluetoothDiscoveryServiceTest {
         MockBluetoothAdapter mockAdapter2 = new MockBluetoothAdapter();
         MockBluetoothDevice mockDevice1 = mockAdapter1.getDevice(address);
         MockBluetoothDevice mockDevice2 = mockAdapter2.getDevice(address);
-        String deviceName = RandomStringUtils.randomAlphanumeric(10);
+        String deviceName = StringUtil.randomAlphanummeric(10);
         mockDevice1.setDeviceName(deviceName);
         mockDevice2.setDeviceName(deviceName);
 
@@ -266,7 +261,7 @@ public class BluetoothDiscoveryServiceTest {
     public void nonConnectionParticipantTest() {
         MockBluetoothAdapter mockAdapter1 = new MockBluetoothAdapter();
         MockBluetoothDevice mockDevice = mockAdapter1.getDevice(TestUtils.randomAddress());
-        String deviceName = RandomStringUtils.randomAlphanumeric(10);
+        String deviceName = StringUtil.randomAlphanummeric(10);
         mockDevice.setDeviceName(deviceName);
 
         BluetoothDevice device = Mockito.spy(mockDevice);
@@ -423,7 +418,7 @@ public class BluetoothDiscoveryServiceTest {
         MockBluetoothAdapter mockAdapter2 = new MockBluetoothAdapter();
         MockBluetoothDevice mockDevice1 = mockAdapter1.getDevice(address);
         MockBluetoothDevice mockDevice2 = mockAdapter2.getDevice(address);
-        String deviceName = RandomStringUtils.randomAlphanumeric(10);
+        String deviceName = StringUtil.randomAlphanummeric(10);
 
         MockDiscoveryParticipant participant2 = new MockDiscoveryParticipant() {
             @Override
@@ -539,8 +534,7 @@ public class BluetoothDiscoveryServiceTest {
         private ThingTypeUID typeUID;
 
         public MockDiscoveryParticipant() {
-            this.typeUID = new ThingTypeUID(BluetoothBindingConstants.BINDING_ID,
-                    RandomStringUtils.randomAlphabetic(6));
+            this.typeUID = new ThingTypeUID(BluetoothBindingConstants.BINDING_ID, StringUtil.randomAlphabetic(6));
         }
 
         @Override
@@ -550,16 +544,20 @@ public class BluetoothDiscoveryServiceTest {
 
         @Override
         public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
-            String repProp = RandomStringUtils.randomAlphabetic(6);
-            return DiscoveryResultBuilder.create(getThingUID(device)).withLabel(RandomStringUtils.randomAlphabetic(6))
-                    .withProperty(repProp, RandomStringUtils.randomAlphabetic(6)).withRepresentationProperty(repProp)
+            String repProp = StringUtil.randomAlphabetic(6);
+            ThingUID thingUID = getThingUID(device);
+            if (thingUID == null) {
+                return null;
+            }
+            return DiscoveryResultBuilder.create(thingUID).withLabel(StringUtil.randomAlphabetic(6))
+                    .withProperty(repProp, StringUtil.randomAlphabetic(6)).withRepresentationProperty(repProp)
                     .withBridge(device.getAdapter().getUID()).build();
         }
 
         @Override
-        public @NonNull ThingUID getThingUID(BluetoothDiscoveryDevice device) {
+        public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
             String deviceName = device.getName();
-            String id = deviceName != null ? deviceName : RandomStringUtils.randomAlphabetic(6);
+            String id = deviceName != null ? deviceName : StringUtil.randomAlphabetic(6);
             return new ThingUID(typeUID, device.getAdapter().getUID(), id);
         }
     }
index 895abf473107543af66b46bc0aae70c4682f019c..f29c961fde067b6330e2beefad6dca2fd29cafda 100644 (file)
@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -33,10 +34,11 @@ import org.openhab.core.common.NamedThreadFactory;
  * @author Connor Petty - Initial contribution
  *
  */
+@NonNullByDefault
 class RetryFutureTest {
 
     private static final int TIMEOUT_MS = 1000;
-    private ScheduledExecutorService scheduler;
+    private @NonNullByDefault({}) ScheduledExecutorService scheduler;
 
     @BeforeEach
     public void init() {
@@ -165,6 +167,6 @@ class RetryFutureTest {
     }
 
     private static class DummyException extends Exception {
-
+        private static final long serialVersionUID = 1L;
     }
 }