Java 17 features (N-S) (#15565)

- 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-13 08:03:31 +02:00
committed by GitHub
parent 641b482551
commit ab58f4ffb4
471 changed files with 1624 additions and 1868 deletions

View File

@@ -123,6 +123,7 @@ public class SmartthingsHandlerFactory extends BaseThingHandlerFactory
* @throws TimeoutException
* @throws ExecutionException
*/
@Override
public void sendDeviceCommand(String path, int timeout, String data)
throws InterruptedException, TimeoutException, ExecutionException {
ContentResponse response = httpClient

View File

@@ -53,11 +53,11 @@ public class SmartthingsColor100Converter extends SmartthingsConverter {
// The command should be of HSBType. The hue component needs to be divided by 3.6 to convert 0-360 degrees to
// 0-100 percent
// The easiest way to do this is to create a new HSBType with the hue component changed.
if (command instanceof HSBType) {
HSBType hsb = (HSBType) command;
double hue = Math.round((hsb.getHue().doubleValue() / 3.60)); // add .5 to round
if (command instanceof HSBType hsbCommand) {
double hue = Math.round((hsbCommand.getHue().doubleValue() / 3.60)); // add .5 to round
long hueInt = (long) hue;
HSBType hsb100 = new HSBType(new DecimalType(hueInt), hsb.getSaturation(), hsb.getBrightness());
HSBType hsb100 = new HSBType(new DecimalType(hueInt), hsbCommand.getSaturation(),
hsbCommand.getBrightness());
// now use the default converter to convert to a JSON string
jsonMsg = defaultConvertToSmartthings(channelUid, hsb100);
} else {
@@ -95,7 +95,7 @@ public class SmartthingsColor100Converter extends SmartthingsConverter {
}
// Get the RGB colors
int rgb[] = new int[3];
int[] rgb = new int[3];
for (int i = 0, pos = 1; i < 3; i++, pos += 2) {
String c = value.substring(pos, pos + 2);
rgb[i] = Integer.parseInt(c, 16);

View File

@@ -47,8 +47,7 @@ public class SmartthingsColorConverter extends SmartthingsConverter {
@Override
public String convertToSmartthings(ChannelUID channelUid, Command command) {
String jsonMsg = defaultConvertToSmartthings(channelUid, command);
return jsonMsg;
return defaultConvertToSmartthings(channelUid, command);
}
/*
@@ -78,14 +77,13 @@ public class SmartthingsColorConverter extends SmartthingsConverter {
}
// Get the RGB colors
int rgb[] = new int[3];
int[] rgb = new int[3];
for (int i = 0, pos = 1; i < 3; i++, pos += 2) {
String c = value.substring(pos, pos + 2);
rgb[i] = Integer.parseInt(c, 16);
}
// Convert to state
State state = HSBType.fromRGB(rgb[0], rgb[1], rgb[2]);
return state;
return HSBType.fromRGB(rgb[0], rgb[1], rgb[2]);
}
}

View File

@@ -76,13 +76,11 @@ public abstract class SmartthingsConverter {
protected String defaultConvertToSmartthings(ChannelUID channelUid, Command command) {
String value;
if (command instanceof DateTimeType) {
DateTimeType dt = (DateTimeType) command;
value = dt.format("%m/%d/%Y %H.%M.%S");
} else if (command instanceof HSBType) {
HSBType hsb = (HSBType) command;
value = String.format("[%d, %d, %d ]", hsb.getHue().intValue(), hsb.getSaturation().intValue(),
hsb.getBrightness().intValue());
if (command instanceof DateTimeType dateTimeCommand) {
value = dateTimeCommand.format("%m/%d/%Y %H.%M.%S");
} else if (command instanceof HSBType hsbCommand) {
value = String.format("[%d, %d, %d ]", hsbCommand.getHue().intValue(),
hsbCommand.getSaturation().intValue(), hsbCommand.getBrightness().intValue());
} else if (command instanceof DecimalType) {
value = command.toString();
} else if (command instanceof IncreaseDecreaseType) { // Need to surround with double quotes
@@ -121,11 +119,9 @@ public abstract class SmartthingsConverter {
value = command.toString().toLowerCase();
}
String jsonMsg = String.format(
return String.format(
"{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"capabilityAttribute\": \"%s\", \"value\": %s}",
thingTypeId, smartthingsName, channelUid.getId(), value);
return jsonMsg;
}
protected String surroundWithQuotes(String param) {
@@ -159,16 +155,16 @@ public abstract class SmartthingsConverter {
return UnDefType.UNDEF;
case "Dimmer":
// The value coming in should be a number
if (deviceValue instanceof String) {
return new PercentType((String) deviceValue);
if (deviceValue instanceof String stringCommand) {
return new PercentType(stringCommand);
} else {
logger.warn("Failed to convert {} with a value of {} from class {} to an appropriate type.",
deviceType, deviceValue, deviceValue.getClass().getName());
return UnDefType.UNDEF;
}
case "Number":
if (deviceValue instanceof String) {
return new DecimalType(Double.parseDouble((String) deviceValue));
if (deviceValue instanceof String stringCommand2) {
return new DecimalType(Double.parseDouble(stringCommand2));
} else if (deviceValue instanceof Double) {
return new DecimalType((Double) deviceValue);
} else if (deviceValue instanceof Long) {
@@ -196,8 +192,8 @@ public abstract class SmartthingsConverter {
// But if the result is from sensor change via a subscription to a threeAxis device the results will
// be a String of the format "value":"-873,-70,484"
// which GSON returns as a LinkedTreeMap
if (deviceValue instanceof String) {
return new StringType((String) deviceValue);
if (deviceValue instanceof String stringCommand3) {
return new StringType(stringCommand3);
} else if (deviceValue instanceof Map<?, ?>) {
Map<String, String> map = (Map<String, String>) deviceValue;
String s = String.format("%.0f,%.0f,%.0f", map.get("x"), map.get("y"), map.get("z"));

View File

@@ -41,13 +41,11 @@ public class SmartthingsDefaultConverter extends SmartthingsConverter {
@Override
public String convertToSmartthings(ChannelUID channelUid, Command command) {
String jsonMsg = defaultConvertToSmartthings(channelUid, command);
return jsonMsg;
return defaultConvertToSmartthings(channelUid, command);
}
@Override
public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
State state = defaultConvertToOpenHab(acceptedChannelType, dataFromSmartthings);
return state;
return defaultConvertToOpenHab(acceptedChannelType, dataFromSmartthings);
}
}

View File

@@ -47,11 +47,10 @@ public class SmartthingsHue100Converter extends SmartthingsConverter {
public String convertToSmartthings(ChannelUID channelUid, Command command) {
String jsonMsg;
if (command instanceof HSBType) {
HSBType hsb = (HSBType) command;
double hue = hsb.getHue().doubleValue() / 3.60;
String value = String.format("[%.0f, %d, %d ]", hue, hsb.getSaturation().intValue(),
hsb.getBrightness().intValue());
if (command instanceof HSBType hsbCommand) {
double hue = hsbCommand.getHue().doubleValue() / 3.60;
String value = String.format("[%.0f, %d, %d ]", hue, hsbCommand.getSaturation().intValue(),
hsbCommand.getBrightness().intValue());
jsonMsg = String.format(
"{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"capabilityAttribute\": \"%s\", \"value\": %s}",
thingTypeId, smartthingsName, channelUid.getId(), value);
@@ -74,20 +73,20 @@ public class SmartthingsHue100Converter extends SmartthingsConverter {
}
if (acceptedChannelType != null && "Number".contentEquals(acceptedChannelType)) {
if (deviceValue instanceof String) {
double d = Double.parseDouble((String) deviceValue);
if (deviceValue instanceof String stringCommand) {
double d = Double.parseDouble(stringCommand);
d *= 3.6;
return new DecimalType(d);
} else if (deviceValue instanceof Long) {
double d = ((Long) deviceValue).longValue();
d *= 3.6;
return new DecimalType(d);
} else if (deviceValue instanceof BigDecimal) {
double d = ((BigDecimal) deviceValue).doubleValue();
} else if (deviceValue instanceof BigDecimal decimalValue) {
double d = decimalValue.doubleValue();
d *= 3.6;
return new DecimalType(d);
} else if (deviceValue instanceof Number) {
double d = ((Number) deviceValue).doubleValue();
} else if (deviceValue instanceof Number numberValue) {
double d = numberValue.doubleValue();
d *= 3.6;
return new DecimalType(d);
} else {

View File

@@ -37,19 +37,15 @@ public class SmartthingsOpenCloseControlConverter extends SmartthingsConverter {
@Override
public String convertToSmartthings(ChannelUID channelUid, Command command) {
String smartthingsValue = (command.toString().toLowerCase().equals("open")) ? "open" : "close";
String smartthingsValue = ("open".equals(command.toString().toLowerCase())) ? "open" : "close";
smartthingsValue = surroundWithQuotes(smartthingsValue);
String jsonMsg = String.format("{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"value\": %s}",
thingTypeId, smartthingsName, smartthingsValue);
return jsonMsg;
return String.format("{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"value\": %s}", thingTypeId,
smartthingsName, smartthingsValue);
}
@Override
public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
State state = defaultConvertToOpenHab(acceptedChannelType, dataFromSmartthings);
return state;
return defaultConvertToOpenHab(acceptedChannelType, dataFromSmartthings);
}
}

View File

@@ -128,8 +128,7 @@ public class SmartthingsThingHandler extends ConfigStatusThingHandler {
* @return channel id
*/
private String getSmartthingsAttributeFromChannel(ChannelUID channelUID) {
String id = channelUID.getId();
return id;
return channelUID.getId();
}
/**
@@ -211,8 +210,7 @@ public class SmartthingsThingHandler extends ConfigStatusThingHandler {
try {
Constructor<?> constr = Class.forName(converterClassName.toString()).getDeclaredConstructor(Thing.class);
constr.setAccessible(true);
SmartthingsConverter cvtr = (SmartthingsConverter) constr.newInstance(thing);
return cvtr;
return (SmartthingsConverter) constr.newInstance(thing);
} catch (ClassNotFoundException e) {
// Most of the time there is no channel specific converter, the default converter is all that is needed.
logger.trace("No Custom converter exists for {} ({})", converterName, converterClassName);