Java 17 features (T-Z) (#15576)

- 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
- remove null check before instanceof

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2023-09-21 07:58:53 +02:00
committed by GitHub
parent bf1aa3deb2
commit 1b122a53b9
277 changed files with 1402 additions and 1298 deletions

View File

@@ -120,8 +120,8 @@ public class TellstickHandlerFactory extends BaseThingHandlerFactory {
@Override
protected void removeHandler(ThingHandler thingHandler) {
if (thingHandler instanceof TelldusBridgeHandler) {
unregisterDeviceDiscoveryService((TelldusBridgeHandler) thingHandler);
if (thingHandler instanceof TelldusBridgeHandler telldusBridgeHandler) {
unregisterDeviceDiscoveryService(telldusBridgeHandler);
}
}
}

View File

@@ -156,10 +156,10 @@ public class TelldusCoreDeviceController implements DeviceChangeListener, Sensor
turnOn(device);
} else if (command == OnOffType.OFF) {
turnOff(device);
} else if (command instanceof PercentType) {
dim(device, (PercentType) command);
} else if (command instanceof IncreaseDecreaseType) {
increaseDecrease(device, ((IncreaseDecreaseType) command));
} else if (command instanceof PercentType percentCommand) {
dim(device, percentCommand);
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
increaseDecrease(device, increaseDecreaseCommand);
}
} else if (device instanceof SwitchableDevice) {
if (command == OnOffType.ON) {
@@ -198,29 +198,29 @@ public class TelldusCoreDeviceController implements DeviceChangeListener, Sensor
double value = command.doubleValue();
// 0 means OFF and 100 means ON
if (value == 0 && dev instanceof SwitchableDevice) {
((SwitchableDevice) dev).off();
} else if (value == 100 && dev instanceof SwitchableDevice) {
((SwitchableDevice) dev).on();
} else if (dev instanceof DimmableDevice) {
if (value == 0 && dev instanceof SwitchableDevice device) {
device.off();
} else if (value == 100 && dev instanceof SwitchableDevice device) {
device.on();
} else if (dev instanceof DimmableDevice device) {
long tdVal = Math.round((value / 100) * 255);
((DimmableDevice) dev).dim((int) tdVal);
device.dim((int) tdVal);
} else {
throw new TelldusBindingException("Cannot send DIM to " + dev);
}
}
private void turnOff(Device dev) throws TellstickException {
if (dev instanceof SwitchableDevice) {
((SwitchableDevice) dev).off();
if (dev instanceof SwitchableDevice device) {
device.off();
} else {
throw new TelldusBindingException("Cannot send OFF to " + dev);
}
}
private void turnOn(Device dev) throws TellstickException {
if (dev instanceof SwitchableDevice) {
((SwitchableDevice) dev).on();
if (dev instanceof SwitchableDevice device) {
device.on();
} else {
throw new TelldusBindingException("Cannot send ON to " + dev);
}

View File

@@ -135,16 +135,16 @@ public class TellstickDiscoveryService extends AbstractDiscoveryService implemen
} else if (device instanceof SwitchableDevice) {
thingUID = new ThingUID(TellstickBindingConstants.SWITCH_THING_TYPE, bridge.getUID(),
device.getUUId());
} else if (device instanceof TellstickNetDevice) {
if ((((TellstickNetDevice) device).getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
} else if (device instanceof TellstickNetDevice netDevice) {
if ((netDevice.getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
thingUID = new ThingUID(TellstickBindingConstants.DIMMER_THING_TYPE, bridge.getUID(),
device.getUUId());
} else {
thingUID = new ThingUID(TellstickBindingConstants.SWITCH_THING_TYPE, bridge.getUID(),
device.getUUId());
}
} else if (device instanceof TellstickLocalDeviceDTO) {
if ((((TellstickLocalDeviceDTO) device).getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
} else if (device instanceof TellstickLocalDeviceDTO localDevice) {
if ((localDevice.getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
thingUID = new ThingUID(TellstickBindingConstants.DIMMER_THING_TYPE, bridge.getUID(),
device.getUUId());
} else {
@@ -162,8 +162,7 @@ public class TellstickDiscoveryService extends AbstractDiscoveryService implemen
private ThingTypeUID findSensorType(Device device) {
logger.debug("Device: {}", device);
ThingTypeUID sensorThingId;
if (device instanceof TellstickSensor) {
TellstickSensor sensor = (TellstickSensor) device;
if (device instanceof TellstickSensor sensor) {
logger.debug("Sensor: {}", device);
if (sensor.getData(DataType.WINDAVERAGE) != null || sensor.getData(DataType.WINDGUST) != null
|| sensor.getData(DataType.WINDDIRECTION) != null) {
@@ -173,8 +172,7 @@ public class TellstickDiscoveryService extends AbstractDiscoveryService implemen
} else {
sensorThingId = TellstickBindingConstants.SENSOR_THING_TYPE;
}
} else if (device instanceof TellstickNetSensor) {
TellstickNetSensor sensor = (TellstickNetSensor) device;
} else if (device instanceof TellstickNetSensor sensor) {
if (sensor.isSensorOfType(LiveDataType.WINDAVERAGE) || sensor.isSensorOfType(LiveDataType.WINDDIRECTION)
|| sensor.isSensorOfType(LiveDataType.WINDGUST)) {
sensorThingId = TellstickBindingConstants.WINDSENSOR_THING_TYPE;

View File

@@ -241,29 +241,29 @@ public class TelldusDevicesHandler extends BaseThingHandler implements DeviceSta
}
private boolean isSensor() {
return (getThing().getThingTypeUID().equals(TellstickBindingConstants.SENSOR_THING_TYPE)
return getThing().getThingTypeUID().equals(TellstickBindingConstants.SENSOR_THING_TYPE)
|| getThing().getThingTypeUID().equals(TellstickBindingConstants.RAINSENSOR_THING_TYPE)
|| getThing().getThingTypeUID().equals(TellstickBindingConstants.WINDSENSOR_THING_TYPE)
|| getThing().getThingTypeUID().equals(TellstickBindingConstants.POWERSENSOR_THING_TYPE));
|| getThing().getThingTypeUID().equals(TellstickBindingConstants.POWERSENSOR_THING_TYPE);
}
private void updateSensorStates(Device dev) {
if (dev instanceof TellstickSensor) {
if (dev instanceof TellstickSensor sensor) {
updateStatus(ThingStatus.ONLINE);
for (DataType type : ((TellstickSensor) dev).getData().keySet()) {
updateSensorDataState(type, ((TellstickSensor) dev).getData(type));
for (DataType type : sensor.getData().keySet()) {
updateSensorDataState(type, sensor.getData(type));
}
} else if (dev instanceof TellstickNetSensor) {
if (((TellstickNetSensor) dev).getOnline()) {
} else if (dev instanceof TellstickNetSensor netSensor) {
if (netSensor.getOnline()) {
updateStatus(ThingStatus.ONLINE);
} else {
updateStatus(ThingStatus.OFFLINE);
}
for (DataTypeValue type : ((TellstickNetSensor) dev).getData()) {
for (DataTypeValue type : netSensor.getData()) {
updateSensorDataState(type);
}
} else if (dev instanceof TellstickLocalSensorDTO) {
for (LocalDataTypeValueDTO type : ((TellstickLocalSensorDTO) dev).getData()) {
} else if (dev instanceof TellstickLocalSensorDTO localSensor) {
for (LocalDataTypeValueDTO type : localSensor.getData()) {
updateSensorDataState(type);
}
}
@@ -283,14 +283,11 @@ public class TelldusDevicesHandler extends BaseThingHandler implements DeviceSta
if (device.getUUId().equals(deviceId)) {
if (event instanceof TellstickDeviceEvent) {
updateDeviceState(device);
} else if (event instanceof TellstickNetSensorEvent) {
TellstickNetSensorEvent sensorevent = (TellstickNetSensorEvent) event;
} else if (event instanceof TellstickNetSensorEvent sensorevent) {
updateSensorDataState(sensorevent.getDataTypeValue());
} else if (event instanceof TellstickLocalSensorEventDTO) {
TellstickLocalSensorEventDTO sensorevent = (TellstickLocalSensorEventDTO) event;
} else if (event instanceof TellstickLocalSensorEventDTO sensorevent) {
updateSensorDataState(sensorevent.getDataTypeValue());
} else if (event instanceof TellstickSensorEvent) {
TellstickSensorEvent sensorevent = (TellstickSensorEvent) event;
} else if (event instanceof TellstickSensorEvent sensorevent) {
updateSensorDataState(sensorevent.getDataType(), sensorevent.getData());
} else {
logger.debug("Unhandled Device {}.", device.getDeviceType());
@@ -356,7 +353,7 @@ public class TelldusDevicesHandler extends BaseThingHandler implements DeviceSta
new QuantityType<>(new BigDecimal(dataType.getValue()), WIND_SPEED_UNIT_MS));
break;
case WATT:
if (dataType.getUnit() != null && dataType.getUnit().equals("A")) {
if ("A".equals(dataType.getUnit())) {
updateState(ampereChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), ELECTRIC_UNIT));
} else {
updateState(wattChannel, new QuantityType<>(new BigDecimal(dataType.getValue()), POWER_UNIT));

View File

@@ -129,10 +129,10 @@ public class TelldusLiveDeviceController implements DeviceChangeListener, Sensor
turnOn(device);
} else if (command == OnOffType.OFF) {
turnOff(device);
} else if (command instanceof PercentType) {
dim(device, (PercentType) command);
} else if (command instanceof IncreaseDecreaseType) {
increaseDecrease(device, ((IncreaseDecreaseType) command));
} else if (command instanceof PercentType percentCommand) {
dim(device, percentCommand);
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
increaseDecrease(device, increaseDecreaseCommand);
}
} else if (device instanceof SwitchableDevice) {
if (command == OnOffType.ON) {
@@ -172,22 +172,21 @@ public class TelldusLiveDeviceController implements DeviceChangeListener, Sensor
turnOff(dev);
} else if (value == 100 && dev instanceof TellstickNetDevice) {
turnOn(dev);
} else if (dev instanceof TellstickNetDevice
&& (((TellstickNetDevice) dev).getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
} else if (dev instanceof TellstickNetDevice device && (device.getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
long tdVal = Math.round((value / 100) * 255);
TelldusLiveResponse response = callRestMethod(String.format(HTTP_TELLDUS_DEVICE_DIM, dev.getId(), tdVal),
TelldusLiveResponse.class);
handleResponse((TellstickNetDevice) dev, response);
handleResponse(device, response);
} else {
throw new TelldusBindingException("Cannot send DIM to " + dev);
}
}
private void turnOff(Device dev) throws TellstickException {
if (dev instanceof TellstickNetDevice) {
if (dev instanceof TellstickNetDevice device) {
TelldusLiveResponse response = callRestMethod(String.format(HTTP_TELLDUS_DEVICE_TURNOFF, dev.getId()),
TelldusLiveResponse.class);
handleResponse((TellstickNetDevice) dev, response);
handleResponse(device, response);
} else {
throw new TelldusBindingException("Cannot send OFF to " + dev);
}
@@ -197,21 +196,21 @@ public class TelldusLiveDeviceController implements DeviceChangeListener, Sensor
if (response == null || (response.status == null && response.error == null)) {
throw new TelldusBindingException("No response " + response);
} else if (response.error != null) {
if (response.error.equals("The client for this device is currently offline")) {
if ("The client for this device is currently offline".equals(response.error)) {
device.setOnline(false);
device.setUpdated(true);
}
throw new TelldusBindingException("Error " + response.error);
} else if (!response.status.trim().equals("success")) {
} else if (!"success".equals(response.status.trim())) {
throw new TelldusBindingException("Response " + response.status);
}
}
private void turnOn(Device dev) throws TellstickException {
if (dev instanceof TellstickNetDevice) {
if (dev instanceof TellstickNetDevice device) {
TelldusLiveResponse response = callRestMethod(String.format(HTTP_TELLDUS_DEVICE_TURNON, dev.getId()),
TelldusLiveResponse.class);
handleResponse((TellstickNetDevice) dev, response);
handleResponse(device, response);
} else {
throw new TelldusBindingException("Cannot send ON to " + dev);
}

View File

@@ -90,10 +90,10 @@ public class TelldusLocalDeviceController implements DeviceChangeListener, Senso
turnOn(device);
} else if (command == OnOffType.OFF) {
turnOff(device);
} else if (command instanceof PercentType) {
dim(device, (PercentType) command);
} else if (command instanceof IncreaseDecreaseType) {
increaseDecrease(device, ((IncreaseDecreaseType) command));
} else if (command instanceof PercentType percentCommand) {
dim(device, percentCommand);
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
increaseDecrease(device, increaseDecreaseCommand);
}
} else if (device instanceof SwitchableDevice) {
if (command == OnOffType.ON) {
@@ -137,22 +137,22 @@ public class TelldusLocalDeviceController implements DeviceChangeListener, Senso
turnOff(dev);
} else if (value == 100 && dev instanceof TellstickLocalDeviceDTO) {
turnOn(dev);
} else if (dev instanceof TellstickLocalDeviceDTO
&& (((TellstickLocalDeviceDTO) dev).getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
} else if (dev instanceof TellstickLocalDeviceDTO device
&& (device.getMethods() & JNA.CLibrary.TELLSTICK_DIM) > 0) {
long tdVal = Math.round((value / 100) * 255);
TelldusLocalResponseDTO response = callRestMethod(
String.format(HTTP_LOCAL_API_DEVICE_DIM, dev.getId(), tdVal), TelldusLocalResponseDTO.class);
handleResponse((TellstickLocalDeviceDTO) dev, response);
handleResponse(device, response);
} else {
throw new TelldusBindingException("Cannot send DIM to " + dev);
}
}
private void turnOff(Device dev) throws TellstickException, InterruptedException {
if (dev instanceof TellstickLocalDeviceDTO) {
if (dev instanceof TellstickLocalDeviceDTO device) {
TelldusLocalResponseDTO response = callRestMethod(String.format(HTTP_LOCAL_API_DEVICE_TURNOFF, dev.getId()),
TelldusLocalResponseDTO.class);
handleResponse((TellstickLocalDeviceDTO) dev, response);
handleResponse(device, response);
} else {
throw new TelldusBindingException("Cannot send OFF to " + dev);
}
@@ -165,16 +165,16 @@ public class TelldusLocalDeviceController implements DeviceChangeListener, Senso
} else if (response.getError() != null) {
device.setUpdated(true);
throw new TelldusBindingException("Error " + response.getError());
} else if (!response.getStatus().trim().equals("success")) {
} else if (!"success".equals(response.getStatus().trim())) {
throw new TelldusBindingException("Response " + response.getStatus());
}
}
private void turnOn(Device dev) throws TellstickException, InterruptedException {
if (dev instanceof TellstickLocalDeviceDTO) {
if (dev instanceof TellstickLocalDeviceDTO device) {
TelldusLocalResponseDTO response = callRestMethod(String.format(HTTP_LOCAL_DEVICE_TURNON, dev.getId()),
TelldusLocalResponseDTO.class);
handleResponse((TellstickLocalDeviceDTO) dev, response);
handleResponse(device, response);
} else {
throw new TelldusBindingException("Cannot send ON to " + dev);
}