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:
@@ -52,8 +52,7 @@ public class UpnpDynamicCommandDescriptionProvider implements DynamicCommandDesc
|
||||
@Override
|
||||
public @Nullable CommandDescription getCommandDescription(Channel channel,
|
||||
@Nullable CommandDescription originalCommandDescription, @Nullable Locale locale) {
|
||||
CommandDescription description = descriptions.get(channel.getUID());
|
||||
return description;
|
||||
return descriptions.get(channel.getUID());
|
||||
}
|
||||
|
||||
@Deactivate
|
||||
|
||||
@@ -52,8 +52,7 @@ public class UpnpDynamicStateDescriptionProvider implements DynamicStateDescript
|
||||
@Override
|
||||
public @Nullable StateDescription getStateDescription(Channel channel,
|
||||
@Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
|
||||
StateDescription description = descriptions.get(channel.getUID());
|
||||
return description;
|
||||
return descriptions.get(channel.getUID());
|
||||
}
|
||||
|
||||
@Deactivate
|
||||
|
||||
@@ -333,7 +333,7 @@ public abstract class UpnpHandler extends BaseThingHandler implements UpnpIOPart
|
||||
* Invoke ConnectionComplete on UPnP Connection Manager.
|
||||
*/
|
||||
protected void connectionComplete() {
|
||||
Map<String, String> inputs = Collections.singletonMap(CONNECTION_ID, Integer.toString(connectionId));
|
||||
Map<String, String> inputs = Map.of(CONNECTION_ID, Integer.toString(connectionId));
|
||||
|
||||
invokeAction(CONNECTION_MANAGER, "ConnectionComplete", inputs);
|
||||
}
|
||||
@@ -367,7 +367,7 @@ public abstract class UpnpHandler extends BaseThingHandler implements UpnpIOPart
|
||||
isRcsIdSet = new CompletableFuture<Boolean>();
|
||||
|
||||
// ConnectionID will default to 0 if not set through prepareForConnection method
|
||||
Map<String, String> inputs = Collections.singletonMap(CONNECTION_ID, Integer.toString(connectionId));
|
||||
Map<String, String> inputs = Map.of(CONNECTION_ID, Integer.toString(connectionId));
|
||||
|
||||
invokeAction(CONNECTION_MANAGER, "GetCurrentConnectionInfo", inputs);
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@@ -317,7 +316,7 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
// received
|
||||
}
|
||||
|
||||
Map<String, String> inputs = Collections.singletonMap(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
Map<String, String> inputs = Map.of(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
|
||||
invokeAction(AV_TRANSPORT, "Stop", inputs);
|
||||
}
|
||||
@@ -353,7 +352,7 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
* Invoke Pause on UPnP AV Transport.
|
||||
*/
|
||||
protected void pause() {
|
||||
Map<String, String> inputs = Collections.singletonMap(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
Map<String, String> inputs = Map.of(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
|
||||
invokeAction(AV_TRANSPORT, "Pause", inputs);
|
||||
}
|
||||
@@ -362,7 +361,7 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
* Invoke Next on UPnP AV Transport.
|
||||
*/
|
||||
protected void next() {
|
||||
Map<String, String> inputs = Collections.singletonMap(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
Map<String, String> inputs = Map.of(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
|
||||
invokeAction(AV_TRANSPORT, "Next", inputs);
|
||||
}
|
||||
@@ -371,7 +370,7 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
* Invoke Previous on UPnP AV Transport.
|
||||
*/
|
||||
protected void previous() {
|
||||
Map<String, String> inputs = Collections.singletonMap(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
Map<String, String> inputs = Map.of(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
|
||||
invokeAction(AV_TRANSPORT, "Previous", inputs);
|
||||
}
|
||||
@@ -456,7 +455,7 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
* Result is received in {@link #onValueReceived}.
|
||||
*/
|
||||
protected void getTransportState() {
|
||||
Map<String, String> inputs = Collections.singletonMap(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
Map<String, String> inputs = Map.of(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
|
||||
invokeAction(AV_TRANSPORT, "GetTransportInfo", inputs);
|
||||
}
|
||||
@@ -466,7 +465,7 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
* Result is received in {@link #onValueReceived}.
|
||||
*/
|
||||
protected void getPositionInfo() {
|
||||
Map<String, String> inputs = Collections.singletonMap(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
Map<String, String> inputs = Map.of(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
|
||||
invokeAction(AV_TRANSPORT, "GetPositionInfo", inputs);
|
||||
}
|
||||
@@ -476,7 +475,7 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
* Result is received in {@link #onValueReceived}.
|
||||
*/
|
||||
protected void getMediaInfo() {
|
||||
Map<String, String> inputs = Collections.singletonMap(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
Map<String, String> inputs = Map.of(INSTANCE_ID, Integer.toString(avTransportId));
|
||||
|
||||
invokeAction(AV_TRANSPORT, "smarthome:audio stream http://icecast.vrtcdn.be/stubru_tijdloze-high.mp3", inputs);
|
||||
}
|
||||
@@ -696,24 +695,24 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
private void handleCommandVolume(Command command, String id) {
|
||||
if (command instanceof RefreshType) {
|
||||
getVolume("volume".equals(id) ? UPNP_MASTER : id.replace("volume", ""));
|
||||
} else if (command instanceof PercentType) {
|
||||
setVolume("volume".equals(id) ? UPNP_MASTER : id.replace("volume", ""), (PercentType) command);
|
||||
} else if (command instanceof PercentType percentCommand) {
|
||||
setVolume("volume".equals(id) ? UPNP_MASTER : id.replace("volume", ""), percentCommand);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleCommandMute(Command command, String id) {
|
||||
if (command instanceof RefreshType) {
|
||||
getMute("mute".equals(id) ? UPNP_MASTER : id.replace("mute", ""));
|
||||
} else if (command instanceof OnOffType) {
|
||||
setMute("mute".equals(id) ? UPNP_MASTER : id.replace("mute", ""), (OnOffType) command);
|
||||
} else if (command instanceof OnOffType onOffCommand) {
|
||||
setMute("mute".equals(id) ? UPNP_MASTER : id.replace("mute", ""), onOffCommand);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleCommandLoudness(Command command, String id) {
|
||||
if (command instanceof RefreshType) {
|
||||
getLoudness("loudness".equals(id) ? UPNP_MASTER : id.replace("loudness", ""));
|
||||
} else if (command instanceof OnOffType) {
|
||||
setLoudness("loudness".equals(id) ? UPNP_MASTER : id.replace("loudness", ""), (OnOffType) command);
|
||||
} else if (command instanceof OnOffType onOffCommand) {
|
||||
setLoudness("loudness".equals(id) ? UPNP_MASTER : id.replace("loudness", ""), onOffCommand);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -884,8 +883,8 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
private void handleCommandTrackPosition(ChannelUID channelUID, Command command) {
|
||||
if (command instanceof RefreshType) {
|
||||
updateState(channelUID, new QuantityType<>(trackPosition, Units.SECOND));
|
||||
} else if (command instanceof QuantityType<?>) {
|
||||
QuantityType<?> position = ((QuantityType<?>) command).toUnit(Units.SECOND);
|
||||
} else if (command instanceof QuantityType<?> quantityCommand) {
|
||||
QuantityType<?> position = quantityCommand.toUnit(Units.SECOND);
|
||||
if (position != null) {
|
||||
int pos = Integer.min(trackDuration, position.intValue());
|
||||
seek(String.format("%02d:%02d:%02d", pos / 3600, (pos % 3600) / 60, pos % 60));
|
||||
@@ -897,8 +896,8 @@ public class UpnpRendererHandler extends UpnpHandler {
|
||||
if (command instanceof RefreshType) {
|
||||
int relPosition = (trackDuration != 0) ? (trackPosition * 100) / trackDuration : 0;
|
||||
updateState(channelUID, new PercentType(relPosition));
|
||||
} else if (command instanceof PercentType) {
|
||||
int pos = ((PercentType) command).intValue() * trackDuration / 100;
|
||||
} else if (command instanceof PercentType percentCommand) {
|
||||
int pos = percentCommand.intValue() * trackDuration / 100;
|
||||
seek(String.format("%02d:%02d:%02d", pos / 3600, (pos % 3600) / 60, pos % 60));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,8 +321,8 @@ public class UpnpServerHandler extends UpnpHandler {
|
||||
private void handleCommandUpnpRenderer(ChannelUID channelUID, Command command) {
|
||||
UpnpRendererHandler renderer = null;
|
||||
UpnpRendererHandler previousRenderer = currentRendererHandler;
|
||||
if (command instanceof StringType) {
|
||||
renderer = (upnpRenderers.get(((StringType) command).toString()));
|
||||
if (command instanceof StringType stringCommand) {
|
||||
renderer = (upnpRenderers.get(stringCommand.toString()));
|
||||
currentRendererHandler = renderer;
|
||||
if (config.filter) {
|
||||
// only refresh title list if filtering by renderer capabilities
|
||||
|
||||
@@ -32,11 +32,11 @@ public class StringUtils {
|
||||
* ' - '
|
||||
*/
|
||||
public static String escapeXml(String xml) {
|
||||
xml = xml.replaceAll("&", "&");
|
||||
xml = xml.replaceAll("<", "<");
|
||||
xml = xml.replaceAll(">", ">");
|
||||
xml = xml.replaceAll("\"", """);
|
||||
xml = xml.replaceAll("'", "'");
|
||||
xml = xml.replace("&", "&");
|
||||
xml = xml.replace("<", "<");
|
||||
xml = xml.replace(">", ">");
|
||||
xml = xml.replace("\"", """);
|
||||
xml = xml.replace("'", "'");
|
||||
return xml;
|
||||
}
|
||||
|
||||
@@ -50,11 +50,11 @@ public class StringUtils {
|
||||
* ' - '
|
||||
*/
|
||||
public static String unEscapeXml(String xml) {
|
||||
xml = xml.replaceAll("&", "&");
|
||||
xml = xml.replaceAll("<", "<");
|
||||
xml = xml.replaceAll(">", ">");
|
||||
xml = xml.replaceAll(""", "\"");
|
||||
xml = xml.replaceAll("'", "'");
|
||||
xml = xml.replace("&", "&");
|
||||
xml = xml.replace("<", "<");
|
||||
xml = xml.replace(">", ">");
|
||||
xml = xml.replace(""", "\"");
|
||||
xml = xml.replace("'", "'");
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,9 +113,8 @@ public final class UpnpControlUtil {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<String> result = (Arrays.asList(files)).stream().map(p -> p.getName().replace(extension, ""))
|
||||
return (Arrays.asList(files)).stream().map(p -> p.getName().replace(extension, ""))
|
||||
.collect(Collectors.toList());
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void delete(String name, @Nullable String path, String extension) {
|
||||
|
||||
@@ -145,9 +145,8 @@ public class UpnpHandlerTest {
|
||||
((Runnable) invocation.getArguments()[0]).run();
|
||||
return null;
|
||||
}).when(executor).scheduleWithFixedDelay(any(Runnable.class), eq(0L), anyLong(), any(TimeUnit.class));
|
||||
doAnswer(invocation -> {
|
||||
return SCHEDULER.schedule((Runnable) invocation.getArguments()[0], 500, TimeUnit.MILLISECONDS);
|
||||
}).when(executor).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
|
||||
doAnswer(invocation -> SCHEDULER.schedule((Runnable) invocation.getArguments()[0], 500, TimeUnit.MILLISECONDS))
|
||||
.when(executor).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
|
||||
}
|
||||
|
||||
public void tearDown() {
|
||||
|
||||
@@ -74,8 +74,10 @@ public class UpnpRendererHandlerTest extends UpnpHandlerTest {
|
||||
private static final String THING_TYPE_UID = "upnpcontrol:upnprenderer";
|
||||
private static final String THING_UID = THING_TYPE_UID + ":mockrenderer";
|
||||
|
||||
private static final String LAST_CHANGE_HEADER = "<Event xmlns=\"urn:schemas-upnp-org:metadata-1-0/AVT/\">"
|
||||
+ "<InstanceID val=\"0\">";
|
||||
private static final String LAST_CHANGE_HEADER = """
|
||||
<Event xmlns="urn:schemas-upnp-org:metadata-1-0/AVT/">\
|
||||
<InstanceID val="0">\
|
||||
""";
|
||||
private static final String LAST_CHANGE_FOOTER = "</InstanceID></Event>";
|
||||
private static final String AV_TRANSPORT_URI = "<AVTransportURI val=\"";
|
||||
private static final String AV_TRANSPORT_URI_METADATA = "<AVTransportURIMetaData val=\"";
|
||||
|
||||
@@ -60,9 +60,11 @@ public class UpnpServerHandlerTest extends UpnpHandlerTest {
|
||||
private static final String THING_TYPE_UID = "upnpcontrol:upnpserver";
|
||||
private static final String THING_UID = THING_TYPE_UID + ":mockserver";
|
||||
|
||||
private static final String RESPONSE_HEADER = "<DIDL-Lite xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\" "
|
||||
+ "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" "
|
||||
+ "xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\">";
|
||||
private static final String RESPONSE_HEADER = """
|
||||
<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" \
|
||||
xmlns:dc="http://purl.org/dc/elements/1.1/" \
|
||||
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/">\
|
||||
""";
|
||||
private static final String RESPONSE_FOOTER = "</DIDL-Lite>";
|
||||
|
||||
private static final String BASE_CONTAINER = RESPONSE_HEADER
|
||||
|
||||
Reference in New Issue
Block a user