Java 17 features (H-M) (#15520)

- 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-08 14:36:59 +02:00
committed by GitHub
parent 3751fd0646
commit edaf17b345
519 changed files with 2703 additions and 2660 deletions

View File

@@ -45,11 +45,9 @@ public class Util {
String contentString = contentProvider == null ? "null"
: StreamSupport.stream(contentProvider.spliterator(), false)
.map(b -> StandardCharsets.UTF_8.decode(b).toString()).collect(Collectors.joining(", "));
String logString = "Method = {" + request.getMethod() + "}, Headers = {"
return "Method = {" + request.getMethod() + "}, Headers = {"
+ request.getHeaders().stream().map(HttpField::toString).collect(Collectors.joining(", "))
+ "}, Content = {" + contentString + "}";
return logString;
}
/**

View File

@@ -60,13 +60,11 @@ public class ColorItemConverter extends AbstractTransformingItemConverter {
return string;
}
if (command instanceof HSBType) {
HSBType newState = (HSBType) command;
if (command instanceof HSBType newState) {
state = newState;
return hsbToString(newState);
} else if (command instanceof PercentType && state instanceof HSBType) {
HSBType newState = new HSBType(((HSBType) state).getBrightness(), ((HSBType) state).getSaturation(),
(PercentType) command);
} else if (command instanceof PercentType percentCommand && state instanceof HSBType hsb) {
HSBType newState = new HSBType(hsb.getBrightness(), hsb.getSaturation(), percentCommand);
state = newState;
return hsbToString(newState);
}
@@ -78,32 +76,29 @@ public class ColorItemConverter extends AbstractTransformingItemConverter {
public State toState(String string) {
State newState = UnDefType.UNDEF;
if (string.equals(channelConfig.onValue)) {
if (state instanceof HSBType) {
newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(),
PercentType.HUNDRED);
if (state instanceof HSBType hsb) {
newState = new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.HUNDRED);
} else {
newState = HSBType.WHITE;
}
} else if (string.equals(channelConfig.offValue)) {
if (state instanceof HSBType) {
newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(), PercentType.ZERO);
if (state instanceof HSBType hsb) {
newState = new HSBType(hsb.getHue(), hsb.getSaturation(), PercentType.ZERO);
} else {
newState = HSBType.BLACK;
}
} else if (string.equals(channelConfig.increaseValue) && state instanceof HSBType) {
BigDecimal newBrightness = ((HSBType) state).getBrightness().toBigDecimal().add(channelConfig.step);
} else if (string.equals(channelConfig.increaseValue) && state instanceof HSBType hsb) {
BigDecimal newBrightness = hsb.getBrightness().toBigDecimal().add(channelConfig.step);
if (HUNDRED.compareTo(newBrightness) < 0) {
newBrightness = HUNDRED;
}
newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(),
new PercentType(newBrightness));
} else if (string.equals(channelConfig.decreaseValue) && state instanceof HSBType) {
BigDecimal newBrightness = ((HSBType) state).getBrightness().toBigDecimal().subtract(channelConfig.step);
newState = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(newBrightness));
} else if (string.equals(channelConfig.decreaseValue) && state instanceof HSBType hsb) {
BigDecimal newBrightness = hsb.getBrightness().toBigDecimal().subtract(channelConfig.step);
if (BigDecimal.ZERO.compareTo(newBrightness) > 0) {
newBrightness = BigDecimal.ZERO;
}
newState = new HSBType(((HSBType) state).getHue(), ((HSBType) state).getSaturation(),
new PercentType(newBrightness));
newState = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(newBrightness));
} else {
Matcher matcher = TRIPLE_MATCHER.matcher(string);
if (matcher.matches()) {

View File

@@ -55,8 +55,8 @@ public class DimmerItemConverter extends AbstractTransformingItemConverter {
return string;
}
if (command instanceof PercentType) {
return ((PercentType) command).toString();
if (command instanceof PercentType percentCommand) {
return percentCommand.toString();
}
throw new IllegalArgumentException("Command type '" + command.toString() + "' not supported");
@@ -70,14 +70,14 @@ public class DimmerItemConverter extends AbstractTransformingItemConverter {
newState = PercentType.HUNDRED;
} else if (string.equals(channelConfig.offValue)) {
newState = PercentType.ZERO;
} else if (string.equals(channelConfig.increaseValue) && state instanceof PercentType) {
BigDecimal newBrightness = ((PercentType) state).toBigDecimal().add(channelConfig.step);
} else if (string.equals(channelConfig.increaseValue) && state instanceof PercentType brightnessState) {
BigDecimal newBrightness = brightnessState.toBigDecimal().add(channelConfig.step);
if (HUNDRED.compareTo(newBrightness) < 0) {
newBrightness = HUNDRED;
}
newState = new PercentType(newBrightness);
} else if (string.equals(channelConfig.decreaseValue) && state instanceof PercentType) {
BigDecimal newBrightness = ((PercentType) state).toBigDecimal().subtract(channelConfig.step);
} else if (string.equals(channelConfig.decreaseValue) && state instanceof PercentType brightnessState) {
BigDecimal newBrightness = brightnessState.toBigDecimal().subtract(channelConfig.step);
if (BigDecimal.ZERO.compareTo(newBrightness) > 0) {
newBrightness = BigDecimal.ZERO;
}

View File

@@ -40,6 +40,7 @@ public class GenericItemConverter extends AbstractTransformingItemConverter {
this.toState = toState;
}
@Override
protected State toState(String value) {
try {
return toState.apply(value);
@@ -53,6 +54,7 @@ public class GenericItemConverter extends AbstractTransformingItemConverter {
return null;
}
@Override
protected String toString(Command command) {
return command.toString();
}

View File

@@ -51,7 +51,7 @@ public class RollershutterItemConverter extends AbstractTransformingItemConverte
return string;
}
if (command instanceof PercentType) {
if (command instanceof PercentType brightnessState) {
final String downValue = channelConfig.downValue;
final String upValue = channelConfig.upValue;
if (command.equals(PercentType.HUNDRED) && downValue != null) {
@@ -59,7 +59,7 @@ public class RollershutterItemConverter extends AbstractTransformingItemConverte
} else if (command.equals(PercentType.ZERO) && upValue != null) {
return upValue;
} else {
return ((PercentType) command).toString();
return brightnessState.toString();
}
}

View File

@@ -83,9 +83,8 @@ public class HttpResponseListener extends BufferingResponseListener {
}
private String responseToLogString(Response response) {
String logString = "Code = {" + response.getStatus() + "}, Headers = {"
return "Code = {" + response.getStatus() + "}, Headers = {"
+ response.getHeaders().stream().map(HttpField::toString).collect(Collectors.joining(", "))
+ "}, Content = {" + getContentAsString() + "}";
return logString;
}
}