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

@@ -180,46 +180,44 @@ public abstract class YeelightHandlerBase extends BaseThingHandler
}
switch (channelUID.getId()) {
case CHANNEL_BRIGHTNESS:
if (command instanceof PercentType) {
handlePercentMessage((PercentType) command);
} else if (command instanceof OnOffType) {
handleOnOffCommand((OnOffType) command);
} else if (command instanceof IncreaseDecreaseType) {
handleIncreaseDecreaseBrightnessCommand((IncreaseDecreaseType) command);
if (command instanceof PercentType percentCommand) {
handlePercentMessage(percentCommand);
} else if (command instanceof OnOffType onOffCommand) {
handleOnOffCommand(onOffCommand);
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
handleIncreaseDecreaseBrightnessCommand(increaseDecreaseCommand);
}
break;
case CHANNEL_COLOR:
if (command instanceof HSBType) {
HSBType hsbCommand = (HSBType) command;
if (command instanceof HSBType hsbCommand) {
if (hsbCommand.getBrightness().intValue() == 0) {
handleOnOffCommand(OnOffType.OFF);
} else {
handleHSBCommand(hsbCommand);
}
} else if (command instanceof PercentType) {
handlePercentMessage((PercentType) command);
} else if (command instanceof OnOffType) {
handleOnOffCommand((OnOffType) command);
} else if (command instanceof IncreaseDecreaseType) {
handleIncreaseDecreaseBrightnessCommand((IncreaseDecreaseType) command);
} else if (command instanceof PercentType percentCommand) {
handlePercentMessage(percentCommand);
} else if (command instanceof OnOffType onOffCommand) {
handleOnOffCommand(onOffCommand);
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
handleIncreaseDecreaseBrightnessCommand(increaseDecreaseCommand);
}
break;
case CHANNEL_COLOR_TEMPERATURE:
if (command instanceof PercentType) {
handleColorTemperatureCommand((PercentType) command);
} else if (command instanceof IncreaseDecreaseType) {
handleIncreaseDecreaseBrightnessCommand((IncreaseDecreaseType) command);
if (command instanceof PercentType percentCommand) {
handleColorTemperatureCommand(percentCommand);
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
handleIncreaseDecreaseBrightnessCommand(increaseDecreaseCommand);
}
break;
case CHANNEL_BACKGROUND_COLOR:
if (command instanceof HSBType) {
HSBType hsbCommand = (HSBType) command;
if (command instanceof HSBType hsbCommand) {
handleBackgroundHSBCommand(hsbCommand);
} else if (command instanceof PercentType) {
handleBackgroundBrightnessPercentMessage((PercentType) command);
} else if (command instanceof OnOffType) {
handleBackgroundOnOffCommand((OnOffType) command);
} else if (command instanceof PercentType percentCommand) {
handleBackgroundBrightnessPercentMessage(percentCommand);
} else if (command instanceof OnOffType onOffCommand) {
handleBackgroundOnOffCommand(onOffCommand);
}
break;
case CHANNEL_NIGHTLIGHT:

View File

@@ -51,9 +51,9 @@ public class CeilingDevice extends DeviceBase {
JsonArray status = result.get("result").getAsJsonArray();
// power:
if (status.get(0).toString().equals("\"off\"")) {
if ("\"off\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(true);
} else if (status.get(0).toString().equals("\"on\"")) {
} else if ("\"on\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(false);
}

View File

@@ -51,9 +51,9 @@ public class CtBulbDevice extends DeviceBase {
JsonArray status = result.get("result").getAsJsonArray();
// power:
if (status.get(0).toString().equals("\"off\"")) {
if ("\"off\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(true);
} else if (status.get(0).toString().equals("\"on\"")) {
} else if ("\"on\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(false);
}

View File

@@ -51,9 +51,9 @@ public class DesklampDevice extends DeviceBase {
JsonArray status = result.get("result").getAsJsonArray();
// power:
if (status.get(0).toString().equals("\"off\"")) {
if ("\"off\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(true);
} else if (status.get(0).toString().equals("\"on\"")) {
} else if ("\"on\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(false);
}

View File

@@ -80,7 +80,7 @@ public abstract class DeviceBase {
try {
if (message.has("method")) {
String method = message.get("method").toString().replace("\"", "");
if (method.equals("props")) {// Property notify
if ("props".equals(method)) {// Property notify
String params = message.get("params").toString();
JsonObject propsObject = JsonParser.parseString(params).getAsJsonObject();
for (Entry<String, JsonElement> prop : propsObject.entrySet()) {
@@ -92,9 +92,9 @@ public abstract class DeviceBase {
switch (property) {
case POWER:
if (prop.getValue().toString().equals("\"off\"")) {
if ("\"off\"".equals(prop.getValue().toString())) {
mDeviceStatus.setPowerOff(true);
} else if (prop.getValue().toString().equals("\"on\"")) {
} else if ("\"on\"".equals(prop.getValue().toString())) {
mDeviceStatus.setPowerOff(false);
}
break;
@@ -222,7 +222,7 @@ public abstract class DeviceBase {
} else if (message.has("id") && message.has("result")) {
// no method, but result : ["ok"]
JsonArray result = message.get("result").getAsJsonArray();
if (result.get(0).toString().equals("\"ok\"")) {
if ("\"ok\"".equals(result.get(0).toString())) {
logger.info("######### this is control command response, don't need to notify status change!");
needNotify = false;
}

View File

@@ -49,9 +49,9 @@ public class MonoDevice extends DeviceBase {
JsonArray status = result.get("result").getAsJsonArray();
// power:
if (status.get(0).toString().equals("\"off\"")) {
if ("\"off\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(true);
} else if (status.get(0).toString().equals("\"on\"")) {
} else if ("\"on\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(false);
}

View File

@@ -52,9 +52,9 @@ public class PitayaDevice extends DeviceBase {
JsonArray status = result.get("result").getAsJsonArray();
// power:
if (status.get(0).toString().equals("\"off\"")) {
if ("\"off\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(true);
} else if (status.get(0).toString().equals("\"on\"")) {
} else if ("\"on\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(false);
}

View File

@@ -52,9 +52,9 @@ public class WonderDevice extends DeviceBase {
JsonArray status = result.get("result").getAsJsonArray();
// power:
if (status.get(0).toString().equals("\"off\"")) {
if ("\"off\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(true);
} else if (status.get(0).toString().equals("\"on\"")) {
} else if ("\"on\"".equals(status.get(0).toString())) {
mDeviceStatus.setPowerOff(false);
}

View File

@@ -251,37 +251,36 @@ public class DeviceManager {
device.decreaseCt(action.intDuration());
break;
case background_color:
if (device instanceof DeviceWithAmbientLight) {
if (device instanceof DeviceWithAmbientLight light) {
final String[] split = action.strValue().split(",");
((DeviceWithAmbientLight) device).setBackgroundColor(Integer.parseInt(split[0]),
Integer.parseInt(split[1]), action.intDuration());
}
break;
case background_brightness:
if (device instanceof DeviceWithAmbientLight) {
((DeviceWithAmbientLight) device).setBackgroundBrightness(action.intValue(),
light.setBackgroundColor(Integer.parseInt(split[0]), Integer.parseInt(split[1]),
action.intDuration());
}
break;
case background_brightness:
if (device instanceof DeviceWithAmbientLight light) {
light.setBackgroundBrightness(action.intValue(), action.intDuration());
}
break;
case background_on:
if (device instanceof DeviceWithAmbientLight) {
((DeviceWithAmbientLight) device).setBackgroundPower(true, action.intDuration());
if (device instanceof DeviceWithAmbientLight light) {
light.setBackgroundPower(true, action.intDuration());
}
break;
case background_off:
if (device instanceof DeviceWithAmbientLight) {
((DeviceWithAmbientLight) device).setBackgroundPower(false, action.intDuration());
if (device instanceof DeviceWithAmbientLight light) {
light.setBackgroundPower(false, action.intDuration());
}
break;
case nightlight_off:
if (device instanceof DeviceWithNightlight) {
((DeviceWithNightlight) device).toggleNightlightMode(false);
if (device instanceof DeviceWithNightlight nightlight) {
nightlight.toggleNightlightMode(false);
}
break;
case nightlight_on:
if (device instanceof DeviceWithNightlight) {
((DeviceWithNightlight) device).toggleNightlightMode(true);
if (device instanceof DeviceWithNightlight nightlight) {
nightlight.toggleNightlightMode(true);
}
break;
default:
@@ -327,7 +326,7 @@ public class DeviceManager {
}
public static String getDefaultName(DeviceBase device) {
if (device.getDeviceModel() != null && !device.getDeviceName().equals("")) {
if (device.getDeviceModel() != null && !"".equals(device.getDeviceName())) {
return device.getDeviceName();
}
switch (device.getDeviceType()) {