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

@@ -88,8 +88,8 @@ public class SqueezeBoxAudioSink extends AudioSinkSync {
}
String url;
if (audioStream instanceof URLAudioStream) {
url = ((URLAudioStream) audioStream).getURL();
if (audioStream instanceof URLAudioStream urlAudioStream) {
url = urlAudioStream.getURL();
tryClose(audioStream);
} else {
try {

View File

@@ -149,7 +149,7 @@ public class SqueezeBoxHandlerFactory extends BaseThingHandlerFactory {
@Override
protected synchronized void removeHandler(ThingHandler thingHandler) {
if (thingHandler instanceof SqueezeBoxServerHandler) {
if (thingHandler instanceof SqueezeBoxServerHandler serverHandler) {
logger.trace("removing handler for bridge thing {}", thingHandler.getThing());
ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
@@ -162,7 +162,7 @@ public class SqueezeBoxHandlerFactory extends BaseThingHandlerFactory {
discoveryService.cancelRequestPlayerJob();
// Unregister the PlayerListener from the SqueezeBoxServerHandler
((SqueezeBoxServerHandler) thingHandler).unregisterSqueezeBoxPlayerListener(
serverHandler.unregisterSqueezeBoxPlayerListener(
(SqueezeBoxPlayerEventListener) bundleContext.getService(serviceReg.getReference()));
// Unregister the PlayerListener service
@@ -173,8 +173,8 @@ public class SqueezeBoxHandlerFactory extends BaseThingHandlerFactory {
}
}
if (thingHandler instanceof SqueezeBoxPlayerHandler) {
SqueezeBoxServerHandler bridge = ((SqueezeBoxPlayerHandler) thingHandler).getSqueezeBoxServerHandler();
if (thingHandler instanceof SqueezeBoxPlayerHandler playerHandler) {
SqueezeBoxServerHandler bridge = playerHandler.getSqueezeBoxServerHandler();
if (bridge != null) {
// Unregister the player's audio sink
logger.trace("Unregistering the audio sync service for player thing {}",
@@ -186,7 +186,7 @@ public class SqueezeBoxHandlerFactory extends BaseThingHandlerFactory {
}
logger.trace("removing handler for player thing {}", thingHandler.getThing());
bridge.removePlayerCache(((SqueezeBoxPlayerHandler) thingHandler).getMac());
bridge.removePlayerCache(playerHandler.getMac());
}
}
}

View File

@@ -15,7 +15,6 @@ package org.openhab.binding.squeezebox.internal.discovery;
import static org.openhab.binding.squeezebox.internal.SqueezeBoxBindingConstants.SQUEEZEBOXSERVER_THING_TYPE;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -51,7 +50,7 @@ public class SqueezeBoxServerDiscoveryParticipant implements UpnpDiscoveryPartic
@Override
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
return Collections.singleton(SQUEEZEBOXSERVER_THING_TYPE);
return Set.of(SQUEEZEBOXSERVER_THING_TYPE);
}
@Override

View File

@@ -105,12 +105,12 @@ public final class SqueezeBoxNotificationListener implements SqueezeBoxPlayerEve
}
logger.trace("Mode is {} for player {}", mode, mac);
if (mode.equals("play")) {
if ("play".equals(mode)) {
this.started.set(true);
} else if (this.started.get() && mode.equals("stop")) {
} else if (this.started.get() && "stop".equals(mode)) {
this.stopped.set(true);
}
if (mode.equals("pause")) {
if ("pause".equals(mode)) {
this.paused.set(true);
}
}

View File

@@ -73,8 +73,7 @@ import org.slf4j.LoggerFactory;
public class SqueezeBoxPlayerHandler extends BaseThingHandler implements SqueezeBoxPlayerEventListener {
private final Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerHandler.class);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
.singleton(SQUEEZEBOXPLAYER_THING_TYPE);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(SQUEEZEBOXPLAYER_THING_TYPE);
/**
* We need to remember some states to change offsets in volume, time index,
@@ -244,8 +243,8 @@ public class SqueezeBoxPlayerHandler extends BaseThingHandler implements Squeeze
}
break;
case CHANNEL_VOLUME:
if (command instanceof PercentType) {
squeezeBoxServerHandler.setVolume(mac, ((PercentType) command).intValue());
if (command instanceof PercentType percentCommand) {
squeezeBoxServerHandler.setVolume(mac, percentCommand.intValue());
} else if (command.equals(IncreaseDecreaseType.INCREASE)) {
squeezeBoxServerHandler.volumeUp(mac, currentVolume());
} else if (command.equals(IncreaseDecreaseType.DECREASE)) {
@@ -317,8 +316,8 @@ public class SqueezeBoxPlayerHandler extends BaseThingHandler implements Squeeze
}
break;
case CHANNEL_SLEEP:
if (command instanceof DecimalType) {
Duration sleepDuration = Duration.ofMinutes(((DecimalType) command).longValue());
if (command instanceof DecimalType decimalCommand) {
Duration sleepDuration = Duration.ofMinutes(decimalCommand.longValue());
if (sleepDuration.isNegative() || sleepDuration.compareTo(Duration.ofDays(1)) > 0) {
logger.debug("Sleep timer of {} minutes must be >= 0 and <= 1 day", sleepDuration.toMinutes());
return;
@@ -647,7 +646,7 @@ public class SqueezeBoxPlayerHandler extends BaseThingHandler implements Squeeze
private int cachedStateAsInt(String key) {
State state = stateMap.get(key);
return state instanceof DecimalType ? ((DecimalType) state).intValue() : 0;
return state instanceof DecimalType decimalValue ? decimalValue.intValue() : 0;
}
/**

View File

@@ -84,8 +84,7 @@ import com.google.gson.JsonSyntaxException;
public class SqueezeBoxServerHandler extends BaseBridgeHandler {
private final Logger logger = LoggerFactory.getLogger(SqueezeBoxServerHandler.class);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
.singleton(SQUEEZEBOXSERVER_THING_TYPE);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(SQUEEZEBOXSERVER_THING_TYPE);
// time in seconds to try to reconnect
private static final int RECONNECT_TIME = 60;
@@ -262,7 +261,7 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
int newVolume = volume;
newVolume = Math.min(100, newVolume);
newVolume = Math.max(0, newVolume);
sendCommand(mac + " mixer volume " + String.valueOf(newVolume));
sendCommand(mac + " mixer volume " + newVolume);
}
public void showString(String mac, String line) {
@@ -270,7 +269,7 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
}
public void showString(String mac, String line, int duration) {
sendCommand(mac + " show line1:" + line + " duration:" + String.valueOf(duration));
sendCommand(mac + " show line1:" + line + " duration:" + duration);
}
public void showStringHuge(String mac, String line) {
@@ -278,7 +277,7 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
}
public void showStringHuge(String mac, String line, int duration) {
sendCommand(mac + " show line1:" + line + " font:huge duration:" + String.valueOf(duration));
sendCommand(mac + " show line1:" + line + " font:huge duration:" + duration);
}
public void showStrings(String mac, String line1, String line2) {
@@ -286,7 +285,7 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
}
public void showStrings(String mac, String line1, String line2, int duration) {
sendCommand(mac + " show line1:" + line1 + " line2:" + line2 + " duration:" + String.valueOf(duration));
sendCommand(mac + " show line1:" + line1 + " line2:" + line2 + " duration:" + duration);
}
public void playFavorite(String mac, String favorite) {
@@ -300,7 +299,7 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
}
public void sleep(String mac, Duration sleepDuration) {
sendCommand(mac + " sleep " + String.valueOf(sleepDuration.toSeconds()));
sendCommand(mac + " sleep " + sleepDuration.toSeconds());
}
/**
@@ -856,18 +855,18 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
}
String action = messageParts[2];
String mode;
if (action.equals("newsong")) {
if ("newsong".equals(action)) {
mode = "play";
// Execute in separate thread to avoid delaying listener
scheduler.execute(() -> updateCustomButtons(mac));
// Set the track duration to 0
updatePlayer(listener -> listener.durationEvent(mac, 0));
} else if (action.equals("pause")) {
} else if ("pause".equals(action)) {
if (messageParts.length < 4) {
return;
}
mode = messageParts[3].equals("0") ? "play" : "pause";
} else if (action.equals("stop")) {
mode = "0".equals(messageParts[3]) ? "play" : "pause";
} else if ("stop".equals(action)) {
mode = "stop";
} else if ("play".equals(action) && "playlist".equals(messageParts[1])) {
if (messageParts.length >= 4) {
@@ -884,7 +883,7 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
}
private void handleSourceChangeMessage(String mac, String rawSource) {
String source = URLDecoder.decode(rawSource);
String source = URLDecoder.decode(rawSource, StandardCharsets.UTF_8);
updatePlayer(listener -> listener.sourceChangeEvent(mac, source));
}
@@ -893,13 +892,13 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
return;
}
// server prefsets
if (messageParts[2].equals("server")) {
if ("server".equals(messageParts[2])) {
String function = messageParts[3];
String value = messageParts[4];
if (function.equals("power")) {
final boolean power = value.equals("1");
if ("power".equals(function)) {
final boolean power = "1".equals(value);
updatePlayer(listener -> listener.powerChangeEvent(mac, power));
} else if (function.equals("volume")) {
} else if ("volume".equals(function)) {
final int volume = (int) Double.parseDouble(value);
updatePlayer(listener -> listener.absoluteVolumeChangeEvent(mac, volume));
}
@@ -967,7 +966,7 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
String quote = includeQuotes.booleanValue() ? "\"" : "";
StringBuilder sb = new StringBuilder();
for (Favorite favorite : favorites) {
sb.append(favorite.shortId).append("=").append(quote).append(favorite.name.replaceAll(",", ""))
sb.append(favorite.shortId).append("=").append(quote).append(favorite.name.replace(",", ""))
.append(quote).append(",");
}
@@ -1063,8 +1062,9 @@ public class SqueezeBoxServerHandler extends BaseBridgeHandler {
List<Thing> things = bridge.getThings();
for (Thing thing : things) {
ThingHandler handler = thing.getHandler();
if (handler instanceof SqueezeBoxPlayerEventListener && !squeezeBoxPlayerListeners.contains(handler)) {
event.updateListener((SqueezeBoxPlayerEventListener) handler);
if (handler instanceof SqueezeBoxPlayerEventListener playerEventListener
&& !squeezeBoxPlayerListeners.contains(handler)) {
event.updateListener(playerEventListener);
}
}
}