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

@@ -146,10 +146,10 @@ public class AmazonEchoControlHandlerFactory extends BaseThingHandlerFactory {
.getService(serviceReg.getReference());
serviceReg.unregister();
if (service != null) {
if (service instanceof AmazonEchoDiscovery) {
((AmazonEchoDiscovery) service).deactivate();
} else if (service instanceof SmartHomeDevicesDiscovery) {
((SmartHomeDevicesDiscovery) service).deactivate();
if (service instanceof AmazonEchoDiscovery discovery) {
discovery.deactivate();
} else if (service instanceof SmartHomeDevicesDiscovery discovery) {
discovery.deactivate();
} else {
logger.warn("Found unknown discovery-service instance: {}", service);
}

View File

@@ -49,8 +49,8 @@ public class ChannelHandlerAnnouncement extends ChannelHandler {
public boolean tryHandleCommand(Device device, Connection connection, String channelId, Command command)
throws IOException, URISyntaxException {
if (channelId.equals(CHANNEL_NAME)) {
if (command instanceof StringType) {
String commandValue = ((StringType) command).toFullString();
if (command instanceof StringType stringCommand) {
String commandValue = stringCommand.toFullString();
String body = commandValue;
String title = null;
String speak = commandValue;

View File

@@ -26,6 +26,7 @@ import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
*/
@NonNullByDefault
public interface IEchoThingHandler extends IAmazonThingHandler {
@Override
void startAnnouncement(Device device, String speak, String bodyText, @Nullable String title,
@Nullable Integer volume) throws IOException, URISyntaxException;
}

View File

@@ -143,8 +143,7 @@ public class SmartHomeDevicesDiscovery extends AbstractDiscoveryService {
String deviceName = null;
Map<String, Object> props = new HashMap<>();
if (smartHomeDevice instanceof SmartHomeDevice) {
SmartHomeDevice shd = (SmartHomeDevice) smartHomeDevice;
if (smartHomeDevice instanceof SmartHomeDevice shd) {
logger.trace("Found SmartHome device: {}", shd);
String entityId = shd.entityId;
@@ -165,7 +164,7 @@ public class SmartHomeDevicesDiscovery extends AbstractDiscoveryService {
// Connected through skill
continue;
}
if (!(smartHomeDeviceDiscoveryMode == 2) && "openHAB".equalsIgnoreCase(shd.manufacturerName)) {
if (smartHomeDeviceDiscoveryMode != 2 && "openHAB".equalsIgnoreCase(shd.manufacturerName)) {
// OpenHAB device
continue;
}
@@ -203,8 +202,7 @@ public class SmartHomeDevicesDiscovery extends AbstractDiscoveryService {
deviceName = shd.friendlyName;
}
props.put(DEVICE_PROPERTY_ID, id);
} else if (smartHomeDevice instanceof SmartHomeGroup) {
SmartHomeGroup shg = (SmartHomeGroup) smartHomeDevice;
} else if (smartHomeDevice instanceof SmartHomeGroup shg) {
logger.trace("Found SmartHome device: {}", shg);
String id = shg.findId();

View File

@@ -821,8 +821,7 @@ public class AccountHandler extends BaseBridgeHandler implements IWebSocketComma
// create new id map
Map<String, SmartHomeBaseDevice> newJsonIdSmartHomeDeviceMapping = new HashMap<>();
for (Object smartHomeDevice : smartHomeDevices) {
if (smartHomeDevice instanceof SmartHomeBaseDevice) {
SmartHomeBaseDevice smartHomeBaseDevice = (SmartHomeBaseDevice) smartHomeDevice;
if (smartHomeDevice instanceof SmartHomeBaseDevice smartHomeBaseDevice) {
String id = smartHomeBaseDevice.findId();
if (id != null) {
newJsonIdSmartHomeDeviceMapping.put(id, smartHomeBaseDevice);

View File

@@ -293,8 +293,8 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
}
// Notification commands
if (channelId.equals(CHANNEL_NOTIFICATION_VOLUME)) {
if (command instanceof PercentType) {
int volume = ((PercentType) command).intValue();
if (command instanceof PercentType percentCommand) {
int volume = percentCommand.intValue();
connection.notificationVolume(device, volume);
this.notificationVolumeLevel = volume;
waitForUpdate = -1;
@@ -318,21 +318,18 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
// Media progress commands
Long mediaPosition = null;
if (channelId.equals(CHANNEL_MEDIA_PROGRESS)) {
if (command instanceof PercentType) {
PercentType value = (PercentType) command;
int percent = value.intValue();
if (command instanceof PercentType percentCommand) {
int percent = percentCommand.intValue();
mediaPosition = Math.round((mediaLengthMs / 1000d) * (percent / 100d));
}
}
if (channelId.equals(CHANNEL_MEDIA_PROGRESS_TIME)) {
if (command instanceof DecimalType) {
DecimalType value = (DecimalType) command;
mediaPosition = value.longValue();
if (command instanceof DecimalType decimalCommand) {
mediaPosition = decimalCommand.longValue();
}
if (command instanceof QuantityType<?>) {
QuantityType<?> value = (QuantityType<?>) command;
if (command instanceof QuantityType<?> quantityCommand) {
@Nullable
QuantityType<?> seconds = value.toUnit(Units.SECOND);
QuantityType<?> seconds = quantityCommand.toUnit(Units.SECOND);
if (seconds != null) {
mediaPosition = seconds.longValue();
}
@@ -353,9 +350,8 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
// Volume commands
if (channelId.equals(CHANNEL_VOLUME)) {
Integer volume = null;
if (command instanceof PercentType) {
PercentType value = (PercentType) command;
volume = value.intValue();
if (command instanceof PercentType percentCommand) {
volume = percentCommand.intValue();
} else if (command == OnOffType.OFF) {
volume = 0;
} else if (command == OnOffType.ON) {
@@ -393,8 +389,7 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
// shuffle command
if (channelId.equals(CHANNEL_SHUFFLE)) {
if (command instanceof OnOffType) {
OnOffType value = (OnOffType) command;
if (command instanceof OnOffType value) {
connection.command(device, "{\"type\":\"ShuffleCommand\",\"shuffle\":\""
+ (value == OnOffType.ON ? "true" : "false") + "\"}");
@@ -429,8 +424,8 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
// bluetooth commands
if (channelId.equals(CHANNEL_BLUETOOTH_MAC)) {
needBluetoothRefresh = true;
if (command instanceof StringType) {
String address = ((StringType) command).toFullString();
if (command instanceof StringType stringCommand) {
String address = stringCommand.toFullString();
if (!address.isEmpty()) {
waitForUpdate = 4000;
}
@@ -566,9 +561,8 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
}
}
if (channelId.equals(CHANNEL_TEXT_TO_SPEECH_VOLUME)) {
if (command instanceof PercentType) {
PercentType value = (PercentType) command;
textToSpeechVolume = value.intValue();
if (command instanceof PercentType percentCommand) {
textToSpeechVolume = percentCommand.intValue();
} else if (command == OnOffType.OFF) {
textToSpeechVolume = 0;
} else if (command == OnOffType.ON) {
@@ -680,8 +674,7 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
if (command instanceof RefreshType) {
this.lastKnownEqualizer = null;
}
if (command instanceof DecimalType) {
DecimalType value = (DecimalType) command;
if (command instanceof DecimalType decimalCommand) {
if (this.lastKnownEqualizer == null) {
updateEqualizerState();
}
@@ -689,13 +682,13 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
if (lastKnownEqualizer != null) {
JsonEqualizer newEqualizerSetting = lastKnownEqualizer.createClone();
if (channelId.equals(CHANNEL_EQUALIZER_BASS)) {
newEqualizerSetting.bass = value.intValue();
newEqualizerSetting.bass = decimalCommand.intValue();
}
if (channelId.equals(CHANNEL_EQUALIZER_MIDRANGE)) {
newEqualizerSetting.mid = value.intValue();
newEqualizerSetting.mid = decimalCommand.intValue();
}
if (channelId.equals(CHANNEL_EQUALIZER_TREBLE)) {
newEqualizerSetting.treble = value.intValue();
newEqualizerSetting.treble = decimalCommand.intValue();
}
try {
connection.setEqualizer(device, newEqualizerSetting);

View File

@@ -191,8 +191,8 @@ public class SmartHomeDeviceHandler extends BaseThingHandler {
boolean stateFound = false;
Map<String, List<JsonObject>> mapInterfaceToStates = new HashMap<>();
SmartHomeDevice firstDevice = null;
for (SmartHomeDevice shd : getSupportedSmartHomeDevices(smartHomeBaseDevice, allDevices)) {
String applianceId = shd.applianceId;
for (SmartHomeDevice smartHomeDevice : getSupportedSmartHomeDevices(smartHomeBaseDevice, allDevices)) {
String applianceId = smartHomeDevice.applianceId;
if (applianceId == null) {
continue;
}
@@ -210,7 +210,7 @@ public class SmartHomeDeviceHandler extends BaseThingHandler {
}
}
if (firstDevice == null) {
firstDevice = shd;
firstDevice = smartHomeDevice;
}
for (JsonElement stateElement : states) {
String stateJson = stateElement.getAsString();
@@ -240,9 +240,9 @@ public class SmartHomeDeviceHandler extends BaseThingHandler {
}
}
if (result.needSingleUpdate && smartHomeBaseDevice instanceof SmartHomeDevice && accountHandler != null) {
SmartHomeDevice shd = (SmartHomeDevice) smartHomeBaseDevice;
accountHandler.forceDelayedSmartHomeStateUpdate(shd.findId());
if (result.needSingleUpdate && smartHomeBaseDevice instanceof SmartHomeDevice smartHomeDevice
&& accountHandler != null) {
accountHandler.forceDelayedSmartHomeStateUpdate(smartHomeDevice.findId());
}
}
@@ -257,8 +257,8 @@ public class SmartHomeDeviceHandler extends BaseThingHandler {
Bridge bridge = getBridge();
if (bridge != null) {
BridgeHandler bridgeHandler = bridge.getHandler();
if (bridgeHandler instanceof AccountHandler) {
return (AccountHandler) bridgeHandler;
if (bridgeHandler instanceof AccountHandler accountHandler) {
return accountHandler;
}
}
@@ -297,17 +297,17 @@ public class SmartHomeDeviceHandler extends BaseThingHandler {
if (handlerBase == null || !handlerBase.hasChannel(channelId)) {
continue;
}
for (SmartHomeDevice shd : devices) {
String entityId = shd.entityId;
for (SmartHomeDevice smartHomeDevice : devices) {
String entityId = smartHomeDevice.entityId;
if (entityId == null) {
continue;
}
accountHandler.forceDelayedSmartHomeStateUpdate(getId()); // block updates
if (handlerBase.handleCommand(connection, shd, entityId, shd.getCapabilities(), channelUID.getId(),
command)) {
if (handlerBase.handleCommand(connection, smartHomeDevice, entityId,
smartHomeDevice.getCapabilities(), channelUID.getId(), command)) {
accountHandler.forceDelayedSmartHomeStateUpdate(getId()); // force update again to restart
// update timer
logger.debug("Command {} sent to {}", command, shd.findId());
logger.debug("Command {} sent to {}", command, smartHomeDevice.findId());
}
}
}
@@ -318,9 +318,8 @@ public class SmartHomeDeviceHandler extends BaseThingHandler {
private static void getCapabilities(Map<String, List<SmartHomeCapability>> result, AccountHandler accountHandler,
SmartHomeBaseDevice device) {
if (device instanceof SmartHomeDevice) {
SmartHomeDevice shd = (SmartHomeDevice) device;
for (SmartHomeCapability capability : shd.getCapabilities()) {
if (device instanceof SmartHomeDevice smartHomeDevice) {
for (SmartHomeCapability capability : smartHomeDevice.getCapabilities()) {
String interfaceName = capability.interfaceName;
if (interfaceName != null) {
Objects.requireNonNull(result.computeIfAbsent(interfaceName, name -> new ArrayList<>()))
@@ -329,9 +328,9 @@ public class SmartHomeDeviceHandler extends BaseThingHandler {
}
}
if (device instanceof SmartHomeGroup) {
for (SmartHomeDevice shd : getSupportedSmartHomeDevices(device,
for (SmartHomeDevice smartHomeDevice : getSupportedSmartHomeDevices(device,
accountHandler.getLastKnownSmartHomeDevices())) {
getCapabilities(result, accountHandler, shd);
getCapabilities(result, accountHandler, smartHomeDevice);
}
}
}
@@ -342,29 +341,28 @@ public class SmartHomeDeviceHandler extends BaseThingHandler {
return Collections.emptySet();
}
Set<SmartHomeDevice> result = new HashSet<>();
if (baseDevice instanceof SmartHomeDevice) {
SmartHomeDevice shd = (SmartHomeDevice) baseDevice;
if (shd.getCapabilities().stream().map(capability -> capability.interfaceName)
if (baseDevice instanceof SmartHomeDevice smartHomeDevice) {
if (smartHomeDevice.getCapabilities().stream().map(capability -> capability.interfaceName)
.anyMatch(SUPPORTED_INTERFACES::contains)) {
result.add(shd);
result.add(smartHomeDevice);
}
} else {
SmartHomeGroup shg = (SmartHomeGroup) baseDevice;
SmartHomeGroup smartHomeGroup = (SmartHomeGroup) baseDevice;
for (SmartHomeBaseDevice device : allDevices) {
if (device instanceof SmartHomeDevice) {
SmartHomeDevice shd = (SmartHomeDevice) device;
JsonSmartHomeTags.JsonSmartHomeTag tags = shd.tags;
if (device instanceof SmartHomeDevice smartHomeDevice) {
JsonSmartHomeTags.JsonSmartHomeTag tags = smartHomeDevice.tags;
if (tags != null) {
JsonSmartHomeGroupIdentity.SmartHomeGroupIdentity tagNameToValueSetMap = tags.tagNameToValueSetMap;
JsonSmartHomeGroupIdentifiers.SmartHomeGroupIdentifier applianceGroupIdentifier = shg.applianceGroupIdentifier;
JsonSmartHomeGroupIdentifiers.SmartHomeGroupIdentifier applianceGroupIdentifier = smartHomeGroup.applianceGroupIdentifier;
if (tagNameToValueSetMap != null) {
List<String> groupIdentity = Objects.requireNonNullElse(tagNameToValueSetMap.groupIdentity,
List.of());
if (applianceGroupIdentifier != null && applianceGroupIdentifier.value != null
&& groupIdentity.contains(applianceGroupIdentifier.value)) {
if (shd.getCapabilities().stream().map(capability -> capability.interfaceName)
if (smartHomeDevice.getCapabilities().stream()
.map(capability -> capability.interfaceName)
.anyMatch(SUPPORTED_INTERFACES::contains)) {
result.add(shd);
result.add(smartHomeDevice);
}
}
}

View File

@@ -55,11 +55,10 @@ public class DynamicStateDescriptionSmartHome implements DynamicStateDescription
return null;
}
ThingHandler handler = thing.getHandler();
if (!(handler instanceof SmartHomeDeviceHandler)) {
return null;
if (handler instanceof SmartHomeDeviceHandler smartHomeHandler) {
return smartHomeHandler;
}
SmartHomeDeviceHandler smartHomeHandler = (SmartHomeDeviceHandler) handler;
return smartHomeHandler;
return null;
}
@Override