diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothAddress.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothAddress.java index 358f04bf8..5fe5d4109 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothAddress.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothAddress.java @@ -12,11 +12,15 @@ */ 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 diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCharacteristic.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCharacteristic.java index 6c5b1fd38..a06203340 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCharacteristic.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCharacteristic.java @@ -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)) { + } else if (!btService.equals(other.service)) { return false; } - if (uuid == null) { - if (other.uuid != null) { - return false; - } - } else if (!uuid.equals(other.uuid)) { - 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 uuidToServiceMapping; + private static @Nullable Map 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 @Nullable GattCharacteristic getCharacteristic(UUID uuid) { + Map localServiceMapping = uuidToServiceMapping; + if (localServiceMapping == null) { + localServiceMapping = new HashMap<>(); + for (GattCharacteristic s : values()) { + localServiceMapping.put(s.uuid, s); + } + uuidToServiceMapping = localServiceMapping; } - } - - public static GattCharacteristic getCharacteristic(UUID uuid) { - if (uuidToServiceMapping == null) { - initMapping(); - } - return uuidToServiceMapping.get(uuid); + return localServiceMapping.get(uuid); } /** diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothClass.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothClass.java index c8c6123f7..57a0d7c25 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothClass.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothClass.java @@ -12,12 +12,15 @@ */ 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; diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCompletionStatus.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCompletionStatus.java index 4c5931ab0..1e29a4e09 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCompletionStatus.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothCompletionStatus.java @@ -12,12 +12,15 @@ */ 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 diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothDescriptor.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothDescriptor.java index 92a3bb2e5..0f9ac4c6f 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothDescriptor.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothDescriptor.java @@ -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. *

@@ -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 uuidToServiceMapping; + private static @Nullable Map 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 @Nullable GattDescriptor getDescriptor(UUID uuid) { + Map localServiceMapping = uuidToServiceMapping; + if (localServiceMapping == null) { + localServiceMapping = new HashMap<>(); + for (GattDescriptor s : values()) { + localServiceMapping.put(s.uuid, s); + } + uuidToServiceMapping = localServiceMapping; } - } - - public static GattDescriptor getDescriptor(UUID uuid) { - if (uuidToServiceMapping == null) { - initMapping(); - } - return uuidToServiceMapping.get(uuid); + return localServiceMapping.get(uuid); } /** diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothService.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothService.java index d2642f13a..ad417e04b 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothService.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothService.java @@ -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. *

@@ -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 uuidToServiceMapping; + private static @Nullable Map 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 @Nullable GattService getService(UUID uuid) { + Map localServiceMapping = uuidToServiceMapping; + if (localServiceMapping == null) { + localServiceMapping = new HashMap<>(); + for (GattService s : values()) { + localServiceMapping.put(s.uuid, s); + } + uuidToServiceMapping = localServiceMapping; } - } - - public static GattService getService(UUID uuid) { - if (uuidToServiceMapping == null) { - initMapping(); - } - return uuidToServiceMapping.get(uuid); + return localServiceMapping.get(uuid); } /** diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothUtils.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothUtils.java index e95294d4e..23229affe 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothUtils.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/BluetoothUtils.java @@ -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; } diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/ConnectedBluetoothHandler.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/ConnectedBluetoothHandler.java index e432f88cc..4ca5dc455 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/ConnectedBluetoothHandler.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/ConnectedBluetoothHandler.java @@ -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, diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/BluetoothDiscoveryDevice.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/BluetoothDiscoveryDevice.java index 62ebbc3d2..3acf2bd44 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/BluetoothDiscoveryDevice.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/BluetoothDiscoveryDevice.java @@ -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; } diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDeviceSnapshot.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDeviceSnapshot.java index f192d7c41..e813a306d 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDeviceSnapshot.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDeviceSnapshot.java @@ -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); } /** diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryProcess.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryProcess.java index d048051ed..323f63dd7 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryProcess.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryProcess.java @@ -221,7 +221,10 @@ public class BluetoothDiscoveryProcess implements Supplier { } 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) { diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java index 6402af647..129873f0e 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryService.java @@ -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); diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothConnectionStatusNotification.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothConnectionStatusNotification.java index b1b64a63e..c7ce0cadd 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothConnectionStatusNotification.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothConnectionStatusNotification.java @@ -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; diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothNotification.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothNotification.java index 2e1f6e5e0..0b418a191 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothNotification.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothNotification.java @@ -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; } } diff --git a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothScanNotification.java b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothScanNotification.java index 04ae0f2b2..77ba53d13 100644 --- a/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothScanNotification.java +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/notification/BluetoothScanNotification.java @@ -12,14 +12,18 @@ */ 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 serviceData = null; + private Map serviceData = new HashMap(); /** * 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 index 000000000..6365665f6 --- /dev/null +++ b/bundles/org.openhab.binding.bluetooth/src/main/java/org/openhab/binding/bluetooth/util/StringUtil.java @@ -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"); + } +} diff --git a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/BluetoothAddressTest.java b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/BluetoothAddressTest.java index 04bfd6a6c..a00f3b24c 100644 --- a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/BluetoothAddressTest.java +++ b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/BluetoothAddressTest.java @@ -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 diff --git a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/CharacteristicPropertiesTest.java b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/CharacteristicPropertiesTest.java index 089425bbc..85dd0bb1e 100644 --- a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/CharacteristicPropertiesTest.java +++ b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/CharacteristicPropertiesTest.java @@ -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); diff --git a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java index ce1a0427e..0303ba681 100644 --- a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java +++ b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/TestUtils.java @@ -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)); } } diff --git a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java index 4204b744c..9b362883c 100644 --- a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java +++ b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/discovery/internal/BluetoothDiscoveryServiceTest.java @@ -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); } } diff --git a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/util/RetryFutureTest.java b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/util/RetryFutureTest.java index 895abf473..f29c961fd 100644 --- a/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/util/RetryFutureTest.java +++ b/bundles/org.openhab.binding.bluetooth/src/test/java/org/openhab/binding/bluetooth/util/RetryFutureTest.java @@ -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; } }