Skip to content

Commit

Permalink
[bluetooth] Null annotations and SAT (openhab#13967)
Browse files Browse the repository at this point in the history
* null annotation, checkstyle, dependency

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
  • Loading branch information
lsiepel authored and renescherer committed Mar 23, 2023
1 parent 5e456f0 commit 42d354b
Show file tree
Hide file tree
Showing 21 changed files with 187 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
Expand Down Expand Up @@ -58,12 +62,12 @@ public BluetoothAddress(String address) {
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;
}
Expand All @@ -74,14 +78,8 @@ public boolean equals(Object obj) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
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.
Expand All @@ -32,6 +32,7 @@
* @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;
Expand All @@ -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
*/
Expand All @@ -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.
Expand Down Expand Up @@ -217,7 +216,7 @@ public void setWriteType(int writeType) {
*
* @return the {@link BluetoothService}
*/
public BluetoothService getService() {
public @Nullable BluetoothService getService() {
return service;
}

Expand Down Expand Up @@ -253,22 +252,23 @@ public List<BluetoothDescriptor> getDescriptors() {
*
* @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;
}
Expand All @@ -282,24 +282,19 @@ public boolean equals(Object obj) {
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);
}

Expand Down Expand Up @@ -410,26 +405,24 @@ public enum GattCharacteristic {
REMOVABLE(0x2A3A),
SERVICE_REQUIRED(0x2A3B);

private static Map<UUID, GattCharacteristic> uuidToServiceMapping;
private static @Nullable Map<UUID, GattCharacteristic> uuidToServiceMapping;

private UUID uuid;

private GattCharacteristic(long key) {
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
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>
Expand All @@ -26,6 +29,7 @@
* @author Chris Jackson - Initial contribution
* @author Kai Kreuzer - added constructor and fixed setValue method
*/
@NonNullByDefault
public class BluetoothDescriptor {

protected final BluetoothCharacteristic characteristic;
Expand Down Expand Up @@ -81,7 +85,7 @@ public int getHandle() {
return handle;
}

public GattDescriptor getDescriptor() {
public @Nullable GattDescriptor getDescriptor() {
return GattDescriptor.getDescriptor(uuid);
}

Expand All @@ -99,26 +103,24 @@ public enum GattDescriptor {
NUMBER_OF_DIGITALS(0x2909),
TRIGGER_SETTING(0x290A);

private static Map<UUID, GattDescriptor> uuidToServiceMapping;
private static @Nullable Map<UUID, GattDescriptor> uuidToServiceMapping;

private final UUID uuid;

private GattDescriptor(long key) {
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
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>
Expand All @@ -30,6 +33,7 @@
* @author Chris Jackson - Initial contribution
* @author Kai Kreuzer - Cleaned up code
*/
@NonNullByDefault
public class BluetoothService {

// The service UUID
Expand Down Expand Up @@ -92,11 +96,11 @@ public BluetoothService(UUID uuid, boolean primaryService, int handleStart, int
}

/**
* 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);
}

Expand Down Expand Up @@ -185,7 +189,7 @@ public boolean addCharacteristic(BluetoothCharacteristic characteristic) {
* @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) {
Expand All @@ -201,7 +205,7 @@ public BluetoothCharacteristic getCharacteristicByHandle(int handle) {
*
* @return the {@link GattService} relating to this service
*/
public GattService getService() {
public @Nullable GattService getService() {
return GattService.getService(uuid);
}

Expand Down Expand Up @@ -241,26 +245,24 @@ public enum GattService {
USER_DATA(0x181C),
WEIGHT_SCALE(0x181D);

private static Map<UUID, GattService> uuidToServiceMapping;
private static @Nullable Map<UUID, GattService> uuidToServiceMapping;

private UUID uuid;

private GattService(long key) {
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);
}

/**
Expand Down
Loading

0 comments on commit 42d354b

Please sign in to comment.