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

@@ -79,9 +79,7 @@ public class OpenAPIUtils {
}
try {
URI requestURI = new URI(HttpScheme.HTTP.asString(), (String) null, address, port, path, query,
(String) null);
return requestURI;
return new URI(HttpScheme.HTTP.asString(), (String) null, address, port, path, query, (String) null);
} catch (URISyntaxException var8) {
LOGGER.warn("URI could not be parsed with path {}", path);
throw new NanoleafException("Wrong URI format for API request");
@@ -115,8 +113,8 @@ public class OpenAPIUtils {
}
} catch (ExecutionException ee) {
Throwable cause = ee.getCause();
if (cause != null && cause instanceof HttpResponseException
&& ((HttpResponseException) cause).getResponse().getStatus() == HttpStatus.UNAUTHORIZED_401) {
if (cause instanceof HttpResponseException httpResponseException
&& httpResponseException.getResponse().getStatus() == HttpStatus.UNAUTHORIZED_401) {
LOGGER.debug("OpenAPI request unauthorized. Invalid authorization token.");
throw new NanoleafUnauthorizedException("Invalid authorization token");
} else {

View File

@@ -57,8 +57,7 @@ public class NanoleafCommandExtension extends AbstractConsoleCommandExtension {
thingRegistry.getAll().forEach(thing -> {
if (thing.getUID().getBindingId().equals(NanoleafBindingConstants.BINDING_ID)) {
ThingHandler handler = thing.getHandler();
if (handler instanceof NanoleafControllerHandler) {
NanoleafControllerHandler nanoleafControllerHandler = (NanoleafControllerHandler) handler;
if (handler instanceof NanoleafControllerHandler nanoleafControllerHandler) {
if (!handler.getThing().isEnabled()) {
console.println(
"The following Nanoleaf is NOT enabled as a Thing. Enable it first to view its layout.");
@@ -76,16 +75,15 @@ public class NanoleafCommandExtension extends AbstractConsoleCommandExtension {
Thing thing = thingRegistry.get(new ThingUID(uid));
if (thing != null) {
ThingHandler handler = thing.getHandler();
if (handler instanceof NanoleafControllerHandler) {
NanoleafControllerHandler nanoleafControllerHandler = (NanoleafControllerHandler) handler;
if (handler instanceof NanoleafControllerHandler nanoleafControllerHandler) {
String layout = nanoleafControllerHandler.getLayout();
console.println(layout);
} else {
console.println("Thing with UID '" + uid.toString()
+ "' is not an initialized Nanoleaf controller.");
console.println(
"Thing with UID '" + uid + "' is not an initialized Nanoleaf controller.");
}
} else {
console.println("Thing with UID '" + uid.toString() + "' does not exist.");
console.println("Thing with UID '" + uid + "' does not exist.");
}
} else {
printUsage(console);

View File

@@ -86,10 +86,8 @@ public class NanoleafMDNSDiscoveryParticipant implements MDNSDiscoveryParticipan
MODEL_ID_LIGHTPANELS.equals(modelId) ? API_MIN_FW_VER_LIGHTPANELS : API_MIN_FW_VER_CANVAS);
}
final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
.withProperties(properties).withLabel(service.getName()).withRepresentationProperty(CONFIG_ADDRESS)
.build();
return result;
return DiscoveryResultBuilder.create(uid).withThingType(getThingType(service)).withProperties(properties)
.withLabel(service.getName()).withRepresentationProperty(CONFIG_ADDRESS).build();
}
@Override

View File

@@ -137,8 +137,8 @@ public class NanoleafPanelHandler extends BaseThingHandler implements NanoleafPa
Bridge bridge = getBridge();
if (bridge != null) {
ThingHandler handler = bridge.getHandler();
if (handler instanceof NanoleafControllerHandler) {
((NanoleafControllerHandler) handler).getColorInformation().unregisterChangeListener(getPanelID());
if (handler instanceof NanoleafControllerHandler controllerHandler) {
controllerHandler.getColorInformation().unregisterChangeListener(getPanelID());
}
}
@@ -174,8 +174,8 @@ public class NanoleafPanelHandler extends BaseThingHandler implements NanoleafPa
Bridge bridge = getBridge();
if (bridge != null) {
ThingHandler handler = bridge.getHandler();
if (handler instanceof NanoleafControllerHandler) {
((NanoleafControllerHandler) handler).getColorInformation().registerChangeListener(getPanelID(), this);
if (handler instanceof NanoleafControllerHandler controllerHandler) {
controllerHandler.getColorInformation().registerChangeListener(getPanelID(), this);
}
}
}
@@ -185,8 +185,8 @@ public class NanoleafPanelHandler extends BaseThingHandler implements NanoleafPa
logger.debug("currentPanelColor: {}", currentPanelColor);
HSBType newPanelColor = new HSBType();
if (command instanceof HSBType) {
newPanelColor = (HSBType) command;
if (command instanceof HSBType hsbCommand) {
newPanelColor = hsbCommand;
} else if (command instanceof OnOffType) {
if (OnOffType.ON.equals(command)) {
newPanelColor = new HSBType(currentPanelColor.getHue(), currentPanelColor.getSaturation(),
@@ -195,9 +195,8 @@ public class NanoleafPanelHandler extends BaseThingHandler implements NanoleafPa
newPanelColor = new HSBType(currentPanelColor.getHue(), currentPanelColor.getSaturation(),
MIN_PANEL_BRIGHTNESS);
}
} else if (command instanceof PercentType) {
PercentType brightness = new PercentType(
Math.max(MIN_PANEL_BRIGHTNESS.intValue(), ((PercentType) command).intValue()));
} else if (command instanceof PercentType type) {
PercentType brightness = new PercentType(Math.max(MIN_PANEL_BRIGHTNESS.intValue(), type.intValue()));
newPanelColor = new HSBType(currentPanelColor.getHue(), currentPanelColor.getSaturation(), brightness);
} else if (command instanceof IncreaseDecreaseType) {
int brightness = currentPanelColor.getBrightness().intValue();
@@ -281,8 +280,8 @@ public class NanoleafPanelHandler extends BaseThingHandler implements NanoleafPa
Object panelId = getThing().getConfiguration().get(CONFIG_PANEL_ID);
if (panelId instanceof Integer) {
return (Integer) panelId;
} else if (panelId instanceof Number) {
return ((Number) panelId).intValue();
} else if (panelId instanceof Number numberValue) {
return numberValue.intValue();
} else {
// Fall back to parsing string representation of panel if it is not returning an integer
String stringPanelId = panelId.toString();
@@ -300,8 +299,8 @@ public class NanoleafPanelHandler extends BaseThingHandler implements NanoleafPa
Bridge bridge = getBridge();
if (bridge != null) {
ThingHandler handler = bridge.getHandler();
if (handler instanceof NanoleafControllerHandler) {
((NanoleafControllerHandler) handler).getColorInformation().setPanelColor(panelId, color);
if (handler instanceof NanoleafControllerHandler controllerHandler) {
controllerHandler.getColorInformation().setPanelColor(panelId, color);
} else {
logger.debug("Couldn't find handler for panel {}", panelId);
}

View File

@@ -26,5 +26,5 @@ public enum DrawingAlgorithm {
TRIANGLE,
HEXAGON,
CORNER,
LINE;
LINE
}

View File

@@ -77,10 +77,8 @@ public class DrawingSettings {
private static ImagePoint2D toPictureLayout(Point2D original, int imageHeight, ImagePoint2D min,
double rotationRadians) {
Point2D rotated = original.rotate(rotationRadians);
ImagePoint2D translated = new ImagePoint2D(
NanoleafBindingConstants.LAYOUT_BORDER_WIDTH + rotated.getX() - min.getX(),
return new ImagePoint2D(NanoleafBindingConstants.LAYOUT_BORDER_WIDTH + rotated.getX() - min.getX(),
imageHeight - NanoleafBindingConstants.LAYOUT_BORDER_WIDTH - rotated.getY() + min.getY());
return translated;
}
private static List<ImagePoint2D> toPictureLayout(List<Point2D> originals, int imageHeight, ImagePoint2D min,

View File

@@ -64,7 +64,7 @@ public class NanoleafLayout {
return new byte[] {};
}
ImagePoint2D size[] = findSize(positionDatums, rotationRadians);
ImagePoint2D[] size = findSize(positionDatums, rotationRadians);
final ImagePoint2D min = size[0];
final ImagePoint2D max = size[1];

View File

@@ -90,20 +90,20 @@ public class State {
}
public void setState(IntegerState value) {
if (value instanceof Brightness) {
this.setBrightness((Brightness) value);
} else if (value instanceof Hue) {
this.setHue((Hue) value);
} else if (value instanceof Sat) {
this.setSaturation((Sat) value);
} else if (value instanceof Ct) {
this.setColorTemperature((Ct) value);
if (value instanceof Brightness brightnessState) {
this.setBrightness(brightnessState);
} else if (value instanceof Hue hueState) {
this.setHue(hueState);
} else if (value instanceof Sat satState) {
this.setSaturation(satState);
} else if (value instanceof Ct ctState) {
this.setColorTemperature(ctState);
}
}
public void setState(BooleanState value) {
if (value instanceof On) {
this.setOn((On) value);
if (value instanceof On onState) {
this.setOn(onState);
}
}
}

View File

@@ -70,7 +70,7 @@ public class NanoleafLayoutTest {
}
private class TestPanelState implements PanelState {
private final HSBType testColors[] = { HSBType.fromRGB(160, 120, 40), HSBType.fromRGB(80, 60, 20),
private final HSBType[] testColors = { HSBType.fromRGB(160, 120, 40), HSBType.fromRGB(80, 60, 20),
HSBType.fromRGB(120, 90, 30), HSBType.fromRGB(200, 150, 60) };
@Override