[io] Use Java 17 features (#15485)

* instanceof matching and multiline strings
* use Map/Set/List.of instead of Collections

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2023-08-24 06:44:56 +02:00
committed by GitHub
parent 6e49c6e6ec
commit 56e7daf898
16 changed files with 50 additions and 59 deletions

View File

@@ -17,8 +17,8 @@ import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
@@ -95,7 +95,7 @@ public class NetworkUtils {
public static Response singleError(Gson gson, UriInfo uri, int type, @Nullable String message) {
HueResponse e = new HueResponse(
new HueErrorMessage(type, uri.getPath().replace("/api", ""), message != null ? message : ""));
String str = gson.toJson(Collections.singleton(e), new TypeToken<List<?>>() {
String str = gson.toJson(Set.of(e), new TypeToken<List<?>>() {
}.getType());
int httpCode = 500;
switch (type) {

View File

@@ -12,8 +12,8 @@
*/
package org.openhab.io.hueemulation.internal.rest;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
@@ -132,7 +132,7 @@ public class ConfigurationAccess {
public Response catchAll(@Context UriInfo uri) {
HueResponse e = new HueResponse(
new HueErrorMessage(HueResponse.INVALID_JSON, uri.getPath().replace("/api", ""), "Invalid request: "));
String str = cs.gson.toJson(Collections.singleton(e), new TypeToken<List<?>>() {
String str = cs.gson.toJson(Set.of(e), new TypeToken<List<?>>() {
}.getType());
return Response.status(404).entity(str).build();
}

View File

@@ -14,7 +14,6 @@ package org.openhab.io.hueemulation.internal.rest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -358,8 +357,7 @@ public class Scenes implements RegistryChangeListener<Rule> {
if (groupItem == null) {
return NetworkUtils.singleError(cs.gson, uri, HueResponse.ARGUMENTS_INVALID, "Group does not exist!");
}
List<Action> actions = Collections
.singletonList(actionFromState(cs.mapItemUIDtoHueID(groupItem), groupItem.getState()));
List<Action> actions = List.of(actionFromState(cs.mapItemUIDtoHueID(groupItem), groupItem.getState()));
builder.withActions(actions);
}

View File

@@ -14,8 +14,8 @@ package org.openhab.io.hueemulation.internal.rest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.ws.rs.DELETE;
@@ -172,7 +172,7 @@ public class UserManagement extends DefaultAbstractManagedProvider<HueUserAuthWi
String clientKey = UUID.randomUUID().toString();
addUser(apiKey, clientKey, userRequest.devicetype);
HueSuccessResponseCreateUser h = new HueSuccessResponseCreateUser(apiKey, clientKey);
String result = cs.gson.toJson(Collections.singleton(new HueResponse(h)), new TypeToken<List<?>>() {
String result = cs.gson.toJson(Set.of(new HueResponse(h)), new TypeToken<List<?>>() {
}.getType());
return Response.ok(result).build();

View File

@@ -19,9 +19,10 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.URI;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeoutException;
@@ -126,10 +127,10 @@ public class CommonSetup {
} else {
cs = new ConfigStoreWithoutMetadata(networkAddressService, configAdmin, scheduler);
}
cs.activate(Collections.singletonMap("uuid", "a668dc9b-7172-49c3-832f-acb07dda2a20"));
cs.switchFilter = Collections.singleton("Switchable");
cs.whiteFilter = Collections.singleton("Switchable");
cs.colorFilter = Collections.singleton("ColorLighting");
cs.activate(Map.of("uuid", "a668dc9b-7172-49c3-832f-acb07dda2a20"));
cs.switchFilter = Set.of("Switchable");
cs.whiteFilter = Set.of("Switchable");
cs.colorFilter = Set.of("ColorLighting");
userManagement = Mockito.spy(new UserManagement(storageService, cs));

View File

@@ -17,7 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -73,7 +73,7 @@ public class ItemUIDtoHueIDMappingTests {
// Pretend there is a metadata entry for the imaginary item "demo1" with hueid 10
commonSetup.metadataRegistry.add(new Metadata(new MetadataKey(ConfigStore.METAKEY, "demo1"), "10", null));
cs.activate(Collections.singletonMap("uuid", "demouuid"));
cs.activate(Map.of("uuid", "demouuid"));
assertThat(cs.getHighestAssignedHueID(), CoreMatchers.is(10));
}

View File

@@ -20,7 +20,7 @@ import static org.mockito.Mockito.*;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
@@ -341,7 +341,7 @@ public class ScheduleTests {
configuration.put("time", "12:12:00");
trigger = TriggerBuilder.create().withId("absolutetrigger").withTypeUID("timer.AbsoluteDateTimeTrigger")
.withConfiguration(configuration).build();
timeString = RuleUtils.timeStringFromTrigger(Collections.singletonList(trigger));
timeString = RuleUtils.timeStringFromTrigger(List.of(trigger));
assertThat(timeString, is("2020-02-01T12:12:00"));
@@ -352,7 +352,7 @@ public class ScheduleTests {
configuration.put("randomizeTime", "14:12:34");
trigger = TriggerBuilder.create().withId("absolutetrigger").withTypeUID("timer.AbsoluteDateTimeTrigger")
.withConfiguration(configuration).build();
timeString = RuleUtils.timeStringFromTrigger(Collections.singletonList(trigger));
timeString = RuleUtils.timeStringFromTrigger(List.of(trigger));
assertThat(timeString, is("2020-02-01T12:12:00A14:12:34"));
@@ -362,7 +362,7 @@ public class ScheduleTests {
configuration.put("cronExpression", "15 12 * * 6,7");
trigger = TriggerBuilder.create().withId("crontrigger").withTypeUID("timer.GenericCronTrigger")
.withConfiguration(configuration).build();
timeString = RuleUtils.timeStringFromTrigger(Collections.singletonList(trigger));
timeString = RuleUtils.timeStringFromTrigger(List.of(trigger));
assertThat(timeString, is("W3/T12:15:00"));
@@ -371,7 +371,7 @@ public class ScheduleTests {
configuration.put("cronExpression", "15 14 * * 1,2,3,4,5,6,7");
trigger = TriggerBuilder.create().withId("crontrigger").withTypeUID("timer.GenericCronTrigger")
.withConfiguration(configuration).build();
timeString = RuleUtils.timeStringFromTrigger(Collections.singletonList(trigger));
timeString = RuleUtils.timeStringFromTrigger(List.of(trigger));
assertThat(timeString, is("W127/T14:15:00"));
@@ -380,7 +380,7 @@ public class ScheduleTests {
configuration.put("time", "12:12:00");
trigger = TriggerBuilder.create().withId("timertrigger").withTypeUID("timer.TimerTrigger")
.withConfiguration(configuration).build();
timeString = RuleUtils.timeStringFromTrigger(Collections.singletonList(trigger));
timeString = RuleUtils.timeStringFromTrigger(List.of(trigger));
assertThat(timeString, is("PT12:12:00"));
@@ -390,7 +390,7 @@ public class ScheduleTests {
configuration.put("randomizeTime", "14:12:34");
trigger = TriggerBuilder.create().withId("timertrigger").withTypeUID("timer.TimerTrigger")
.withConfiguration(configuration).build();
timeString = RuleUtils.timeStringFromTrigger(Collections.singletonList(trigger));
timeString = RuleUtils.timeStringFromTrigger(List.of(trigger));
assertThat(timeString, is("PT12:12:00A14:12:34"));
@@ -400,7 +400,7 @@ public class ScheduleTests {
configuration.put("repeat", -1);
trigger = TriggerBuilder.create().withId("timertrigger").withTypeUID("timer.TimerTrigger")
.withConfiguration(configuration).build();
timeString = RuleUtils.timeStringFromTrigger(Collections.singletonList(trigger));
timeString = RuleUtils.timeStringFromTrigger(List.of(trigger));
assertThat(timeString, is("R/PT12:12:00"));
@@ -411,7 +411,7 @@ public class ScheduleTests {
configuration.put("repeat", 12);
trigger = TriggerBuilder.create().withId("timertrigger").withTypeUID("timer.TimerTrigger")
.withConfiguration(configuration).build();
timeString = RuleUtils.timeStringFromTrigger(Collections.singletonList(trigger));
timeString = RuleUtils.timeStringFromTrigger(List.of(trigger));
assertThat(timeString, is("R12/PT12:12:00A14:12:34"));
}