Java 17 features (A-G) (#15516)

- add missing @override
- Java style array syntax
- remove redundant modifiers
- always move String constants to left side in comparisons
- simplify lambda expressions and return statements
- use replace instead of replaceAll w/o regex
- instanceof matching and multiline strings

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2023-09-05 22:30:16 +02:00
committed by GitHub
parent a0dc5c05f2
commit cf10b3e9c7
486 changed files with 2053 additions and 1955 deletions

View File

@@ -35,6 +35,7 @@ public class BlueGigaBluetoothCharacteristic extends BluetoothCharacteristic {
super(null, handle);
}
@Override
public void setProperties(int properties) {
this.properties = properties;
}

View File

@@ -356,33 +356,33 @@ public class BlueGigaBluetoothDevice extends BaseBluetoothDevice implements Blue
@Override
public void bluegigaEventReceived(BlueGigaResponse event) {
if (event instanceof BlueGigaScanResponseEvent) {
handleScanEvent((BlueGigaScanResponseEvent) event);
if (event instanceof BlueGigaScanResponseEvent responseEvent) {
handleScanEvent(responseEvent);
}
else if (event instanceof BlueGigaGroupFoundEvent) {
handleGroupFoundEvent((BlueGigaGroupFoundEvent) event);
else if (event instanceof BlueGigaGroupFoundEvent foundEvent) {
handleGroupFoundEvent(foundEvent);
}
else if (event instanceof BlueGigaFindInformationFoundEvent) {
else if (event instanceof BlueGigaFindInformationFoundEvent foundEvent) {
// A Characteristic has been discovered
handleFindInformationFoundEvent((BlueGigaFindInformationFoundEvent) event);
handleFindInformationFoundEvent(foundEvent);
}
else if (event instanceof BlueGigaProcedureCompletedEvent) {
handleProcedureCompletedEvent((BlueGigaProcedureCompletedEvent) event);
else if (event instanceof BlueGigaProcedureCompletedEvent completedEvent) {
handleProcedureCompletedEvent(completedEvent);
}
else if (event instanceof BlueGigaConnectionStatusEvent) {
handleConnectionStatusEvent((BlueGigaConnectionStatusEvent) event);
else if (event instanceof BlueGigaConnectionStatusEvent statusEvent) {
handleConnectionStatusEvent(statusEvent);
}
else if (event instanceof BlueGigaDisconnectedEvent) {
handleDisconnectedEvent((BlueGigaDisconnectedEvent) event);
else if (event instanceof BlueGigaDisconnectedEvent disconnectedEvent) {
handleDisconnectedEvent(disconnectedEvent);
}
else if (event instanceof BlueGigaAttributeValueEvent) {
handleAttributeValueEvent((BlueGigaAttributeValueEvent) event);
else if (event instanceof BlueGigaAttributeValueEvent valueEvent) {
handleAttributeValueEvent(valueEvent);
}
}

View File

@@ -779,9 +779,8 @@ public class BlueGigaBridgeHandler extends AbstractBluetoothBridgeHandler<BlueGi
@Override
public void bluegigaEventReceived(@Nullable BlueGigaResponse event) {
if (event instanceof BlueGigaScanResponseEvent) {
if (event instanceof BlueGigaScanResponseEvent scanEvent) {
if (initComplete) {
BlueGigaScanResponseEvent scanEvent = (BlueGigaScanResponseEvent) event;
// We use the scan event to add any devices we hear to the devices list
// The device gets created, and then manages itself for discovery etc.
@@ -795,13 +794,11 @@ public class BlueGigaBridgeHandler extends AbstractBluetoothBridgeHandler<BlueGi
return;
}
if (event instanceof BlueGigaConnectionStatusEvent) {
BlueGigaConnectionStatusEvent connectionEvent = (BlueGigaConnectionStatusEvent) event;
if (event instanceof BlueGigaConnectionStatusEvent connectionEvent) {
connections.put(connectionEvent.getConnection(), new BluetoothAddress(connectionEvent.getAddress()));
}
if (event instanceof BlueGigaDisconnectedEvent) {
BlueGigaDisconnectedEvent disconnectedEvent = (BlueGigaDisconnectedEvent) event;
if (event instanceof BlueGigaDisconnectedEvent disconnectedEvent) {
connections.remove(disconnectedEvent.getConnection());
}
}

View File

@@ -35,11 +35,11 @@ public class BlueGigaConfiguration extends BaseBluetoothBridgeHandlerConfigurati
@Override
public String toString() {
return String.format(
"[discovery=%b, port=%s, passiveScanIdleTime=%d, passiveScanInterval=%d, passiveScanWindow=%d"
+ ", activeScanInterval=%d, activeScanWindow=%d, connIntervalMin=%d, connIntervalMax=%d"
+ ", connLatency=%d, connTimeout=%d]",
backgroundDiscovery, port, passiveScanIdleTime, passiveScanInterval, passiveScanWindow,
return String.format("""
[discovery=%b, port=%s, passiveScanIdleTime=%d, passiveScanInterval=%d, passiveScanWindow=%d\
, activeScanInterval=%d, activeScanWindow=%d, connIntervalMin=%d, connIntervalMax=%d\
, connLatency=%d, connTimeout=%d]\
""", backgroundDiscovery, port, passiveScanIdleTime, passiveScanInterval, passiveScanWindow,
activeScanInterval, activeScanWindow, connIntervalMin, connIntervalMax, connLatency, connTimeout);
}
}

View File

@@ -194,8 +194,7 @@ class BlueGigaResponsePackets {
try {
ctor = bleClass.getConstructor(int[].class);
BlueGigaResponse bleFrame = (BlueGigaResponse) ctor.newInstance(data);
return bleFrame;
return (BlueGigaResponse) ctor.newInstance(data);
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
logger.debug("Error instantiating BLE class", e);

View File

@@ -260,8 +260,10 @@ public class BlueGigaSerialHandler {
private void checkIfAlive() {
if (!isAlive()) {
throw new IllegalStateException("Bluegiga handler is dead. Most likely because of IO errors. "
+ "Re-initialization of the BlueGigaSerialHandler is required.");
throw new IllegalStateException("""
Bluegiga handler is dead. Most likely because of IO errors. \
Re-initialization of the BlueGigaSerialHandler is required.\
""");
}
}

View File

@@ -154,7 +154,7 @@ public class BlueGigaTransactionManager implements BlueGigaSerialEventListener {
private void sendNextTransactionIfNoOngoing() {
synchronized (this) {
logger.trace("Send next transaction if no ongoing");
if (!ongoingTransactionId.isPresent()) {
if (ongoingTransactionId.isEmpty()) {
sendNextFrame();
}
}
@@ -243,9 +243,8 @@ public class BlueGigaTransactionManager implements BlueGigaSerialEventListener {
logger.trace("Expected frame: {}, received frame: {}", expected.getSimpleName(), bleResponse);
if (bleCommand instanceof BlueGigaDeviceCommand && bleResponse instanceof BlueGigaDeviceResponse) {
BlueGigaDeviceCommand devCommand = (BlueGigaDeviceCommand) bleCommand;
BlueGigaDeviceResponse devResponse = (BlueGigaDeviceResponse) bleResponse;
if (bleCommand instanceof BlueGigaDeviceCommand devCommand
&& bleResponse instanceof BlueGigaDeviceResponse devResponse) {
logger.trace("Expected connection id: {}, received connection id: {}", devCommand.getConnection(),
devResponse.getConnection());

View File

@@ -12,7 +12,6 @@
*/
package org.openhab.binding.bluetooth.bluegiga.internal.factory;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
@@ -48,8 +47,8 @@ import org.osgi.service.component.annotations.Reference;
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.bluegiga")
public class BlueGigaHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
.singleton(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA);
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
.of(BlueGigaAdapterConstants.THING_TYPE_BLUEGIGA);
private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
@@ -90,8 +89,8 @@ public class BlueGigaHandlerFactory extends BaseThingHandlerFactory {
@Override
protected synchronized void removeHandler(ThingHandler thingHandler) {
if (thingHandler instanceof BluetoothAdapter) {
UID uid = ((BluetoothAdapter) thingHandler).getUID();
if (thingHandler instanceof BluetoothAdapter bluetoothAdapter) {
UID uid = bluetoothAdapter.getUID();
ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
if (serviceReg != null) {
serviceReg.unregister();