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:
Holger Friedrich
2023-09-21 07:58:53 +02:00
committed by GitHub
parent bf1aa3deb2
commit 1b122a53b9
277 changed files with 1402 additions and 1298 deletions

View File

@@ -147,25 +147,21 @@ public class VeSyncV2ApiHelper {
requestData.applyAuthentication(loggedInSession);
// Apply specific addressing parameters
if (requestData instanceof VeSyncRequestManagedDeviceBypassV2) {
if (requestData instanceof VeSyncRequestManagedDeviceBypassV2 veSyncRequestManagedDeviceBypassV2) {
final VeSyncManagedDeviceBase deviceData = macLookup.get(macId);
if (deviceData == null) {
throw new DeviceUnknownException(String.format("Device not discovered with mac id: %s", macId));
}
((VeSyncRequestManagedDeviceBypassV2) requestData).cid = deviceData.cid;
((VeSyncRequestManagedDeviceBypassV2) requestData).configModule = deviceData.configModule;
((VeSyncRequestManagedDeviceBypassV2) requestData).deviceRegion = deviceData.deviceRegion;
veSyncRequestManagedDeviceBypassV2.cid = deviceData.cid;
veSyncRequestManagedDeviceBypassV2.configModule = deviceData.configModule;
veSyncRequestManagedDeviceBypassV2.deviceRegion = deviceData.deviceRegion;
}
return reqV1Authorized(url, requestData);
}
public String reqV1Authorized(final String url, final VeSyncAuthenticatedRequest requestData)
throws AuthenticationException {
try {
return directReqV1Authorized(url, requestData);
} catch (final AuthenticationException ae) {
throw ae;
}
return directReqV1Authorized(url, requestData);
}
private String directReqV1Authorized(final String url, final VeSyncAuthenticatedRequest requestData)

View File

@@ -14,7 +14,6 @@ package org.openhab.binding.vesync.internal.discovery;
import static org.openhab.binding.vesync.internal.VeSyncConstants.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -46,7 +45,7 @@ import org.osgi.service.component.annotations.Component;
public class VeSyncDiscoveryService extends AbstractDiscoveryService
implements DiscoveryService, ThingHandlerService, DeviceMetaDataUpdatedHandler {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_BRIDGE);
private static final int DISCOVER_TIMEOUT_SECONDS = 5;
@@ -79,8 +78,8 @@ public class VeSyncDiscoveryService extends AbstractDiscoveryService
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof VeSyncBridgeHandler) {
bridgeHandler = (VeSyncBridgeHandler) handler;
if (handler instanceof VeSyncBridgeHandler veSyncBridgeHandler) {
bridgeHandler = veSyncBridgeHandler;
bridgeUID = bridgeHandler.getUID();
}
}

View File

@@ -148,10 +148,9 @@ public abstract class VeSyncBaseDeviceHandler extends BaseThingHandler {
protected boolean isDeviceOnline() {
BridgeHandler bridgeHandler = getBridgeHandler();
if (bridgeHandler != null && bridgeHandler instanceof VeSyncBridgeHandler) {
VeSyncBridgeHandler vesyncBridgeHandler = (VeSyncBridgeHandler) bridgeHandler;
if (bridgeHandler instanceof VeSyncBridgeHandler veSyncBridgeHandler) {
@Nullable
VeSyncManagedDeviceBase metadata = vesyncBridgeHandler.api.getMacLookupMap().get(deviceLookupKey);
VeSyncManagedDeviceBase metadata = veSyncBridgeHandler.api.getMacLookupMap().get(deviceLookupKey);
if (metadata == null) {
return false;
@@ -166,10 +165,9 @@ public abstract class VeSyncBaseDeviceHandler extends BaseThingHandler {
Map<String, String> newProps = null;
BridgeHandler bridgeHandler = getBridgeHandler();
if (bridgeHandler != null && bridgeHandler instanceof VeSyncBridgeHandler) {
VeSyncBridgeHandler vesyncBridgeHandler = (VeSyncBridgeHandler) bridgeHandler;
if (bridgeHandler instanceof VeSyncBridgeHandler veSyncBridgeHandler) {
@Nullable
VeSyncManagedDeviceBase metadata = vesyncBridgeHandler.api.getMacLookupMap().get(deviceLookupKey);
VeSyncManagedDeviceBase metadata = veSyncBridgeHandler.api.getMacLookupMap().get(deviceLookupKey);
if (metadata == null) {
return;
@@ -248,8 +246,8 @@ public abstract class VeSyncBaseDeviceHandler extends BaseThingHandler {
return null;
}
ThingHandler handler = bridge.getHandler();
if (handler instanceof VeSyncClient) {
veSyncClient = (VeSyncClient) handler;
if (handler instanceof VeSyncClient client) {
veSyncClient = client;
} else {
return null;
}
@@ -260,8 +258,7 @@ public abstract class VeSyncBaseDeviceHandler extends BaseThingHandler {
protected void requestBridgeFreqScanMetadataIfReq() {
if (requiresMetaDataFrequentUpdates()) {
BridgeHandler bridgeHandler = getBridgeHandler();
if (bridgeHandler != null && bridgeHandler instanceof VeSyncBridgeHandler) {
VeSyncBridgeHandler vesyncBridgeHandler = (VeSyncBridgeHandler) bridgeHandler;
if (bridgeHandler instanceof VeSyncBridgeHandler vesyncBridgeHandler) {
vesyncBridgeHandler.checkIfIncreaseScanRateRequired();
}
}
@@ -272,8 +269,7 @@ public abstract class VeSyncBaseDeviceHandler extends BaseThingHandler {
final VeSyncDeviceConfiguration config = getConfigAs(VeSyncDeviceConfiguration.class);
BridgeHandler bridgeHandler = getBridgeHandler();
if (bridgeHandler != null && bridgeHandler instanceof VeSyncBridgeHandler) {
VeSyncBridgeHandler vesyncBridgeHandler = (VeSyncBridgeHandler) bridgeHandler;
if (bridgeHandler instanceof VeSyncBridgeHandler vesyncBridgeHandler) {
final String configMac = config.macId;

View File

@@ -15,9 +15,9 @@ package org.openhab.binding.vesync.internal.handlers;
import static org.openhab.binding.vesync.internal.VeSyncConstants.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -86,8 +86,8 @@ public class VeSyncBridgeHandler extends BaseBridgeHandler implements VeSyncClie
boolean frequentScanReq = false;
for (Thing th : getThing().getThings()) {
ThingHandler handler = th.getHandler();
if (handler instanceof VeSyncBaseDeviceHandler) {
if (((VeSyncBaseDeviceHandler) handler).requiresMetaDataFrequentUpdates()) {
if (handler instanceof VeSyncBaseDeviceHandler veSyncBaseDeviceHandler) {
if (veSyncBaseDeviceHandler.requiresMetaDataFrequentUpdates()) {
frequentScanReq = true;
break;
}
@@ -185,15 +185,15 @@ public class VeSyncBridgeHandler extends BaseBridgeHandler implements VeSyncClie
}
private void updateThing(VeSyncBridgeConfiguration config, @Nullable ThingHandler handler) {
if (handler instanceof VeSyncBaseDeviceHandler) {
((VeSyncBaseDeviceHandler) handler).updateDeviceMetaData();
((VeSyncBaseDeviceHandler) handler).updateBridgeBasedPolls(config);
if (handler instanceof VeSyncBaseDeviceHandler veSyncBaseDeviceHandler) {
veSyncBaseDeviceHandler.updateDeviceMetaData();
veSyncBaseDeviceHandler.updateBridgeBasedPolls(config);
}
}
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(VeSyncDiscoveryService.class);
return Set.of(VeSyncDiscoveryService.class);
}
@Override
@@ -240,6 +240,7 @@ public class VeSyncBridgeHandler extends BaseBridgeHandler implements VeSyncClie
this.updateProperties(newProps);
}
@Override
public String reqV2Authorized(final String url, final String macId, final VeSyncAuthenticatedRequest requestData)
throws AuthenticationException, DeviceUnknownException {
return api.reqV2Authorized(url, macId, requestData);

View File

@@ -174,10 +174,10 @@ public class VeSyncDeviceAirHumidifierHandler extends VeSyncBaseDeviceHandler {
logger.warn("Warm mode API is unknown in order to send the command");
break;
}
} else if (command instanceof QuantityType) {
} else if (command instanceof QuantityType quantityCommand) {
switch (channelUID.getId()) {
case DEVICE_CHANNEL_CONFIG_TARGET_HUMIDITY:
int targetHumidity = ((QuantityType<?>) command).intValue();
int targetHumidity = quantityCommand.intValue();
if (targetHumidity < 30) {
logger.warn("Target Humidity less than 30 - adjusting to 30 as the valid API value");
targetHumidity = 30;
@@ -193,7 +193,7 @@ public class VeSyncDeviceAirHumidifierHandler extends VeSyncBaseDeviceHandler {
new VeSyncRequestManagedDeviceBypassV2.SetTargetHumidity(targetHumidity));
break;
case DEVICE_CHANNEL_MIST_LEVEL:
int targetMistLevel = ((QuantityType<?>) command).intValue();
int targetMistLevel = quantityCommand.intValue();
// If more devices have this the hope is it's those with the prefix LUH so the check can
// be simplified, originally devices mapped 1/5/9 to 1/2/3.
if (DEV_FAMILY_DUAL_200S.equals(deviceFamily)) {

View File

@@ -234,14 +234,14 @@ public class VeSyncDeviceAirPurifierHandler extends VeSyncBaseDeviceHandler {
}
break;
}
} else if (command instanceof QuantityType) {
} else if (command instanceof QuantityType quantityCommand) {
switch (channelUID.getId()) {
case DEVICE_CHANNEL_FAN_SPEED_ENABLED:
// If the fan speed is being set enforce manual mode
sendV2BypassControlCommand(DEVICE_SET_PURIFIER_MODE,
new VeSyncRequestManagedDeviceBypassV2.SetMode(MODE_MANUAL), false);
int requestedLevel = ((QuantityType<?>) command).intValue();
int requestedLevel = quantityCommand.intValue();
if (requestedLevel < 1) {
logger.warn("Fan speed command less than 1 - adjusting to 1 as the valid API value");
requestedLevel = 1;

View File

@@ -33,7 +33,7 @@ import org.openhab.binding.vesync.internal.dto.responses.VesyncLoginResponse;
@NonNullByDefault
public class VesyncAuthenticatedRequestTest {
public final static VesyncLoginResponse.VesyncUserSession testUser = VeSyncConstants.GSON.fromJson(
public static final VesyncLoginResponse.VesyncUserSession testUser = VeSyncConstants.GSON.fromJson(
org.openhab.binding.vesync.internal.handler.responses.VesyncLoginResponseTest.testGoodLoginResponseBody,
VesyncLoginResponse.class).result;

View File

@@ -46,7 +46,7 @@ public class VesyncRequestManagedDeviceBypassV2Test {
public void checkEmptyPayload() {
final VesyncRequestManagedDeviceBypassV2.EmptyPayload testPaylaod = new VesyncRequestManagedDeviceBypassV2.EmptyPayload();
final String contentTest1 = VeSyncConstants.GSON.toJson(testPaylaod);
assertEquals(true, contentTest1.equals("{}"));
assertEquals(true, "{}".equals(contentTest1));
}
@Test

View File

@@ -27,26 +27,48 @@ import org.openhab.binding.vesync.internal.dto.responses.VesyncLoginResponse;
@NonNullByDefault
public class VesyncLoginResponseTest {
public final static String testGoodLoginResponseBody = "{\r\n" + " \"traceId\": \"1634253816\",\r\n"
+ " \"code\": 0,\r\n" + " \"msg\": \"request success\",\r\n" + " \"result\": {\r\n"
+ " \"isRequiredVerify\": true,\r\n" + " \"accountID\": \"5328043\",\r\n"
+ " \"avatarIcon\": \"https://image.vesync.com/defaultImages/user/avatar_nor.png\",\r\n"
+ " \"birthday\": \"\",\r\n" + " \"gender\": \"\",\r\n"
+ " \"acceptLanguage\": \"en\",\r\n" + " \"userType\": \"1\",\r\n"
+ " \"nickName\": \"david.goodyear\",\r\n" + " \"mailConfirmation\": true,\r\n"
+ " \"termsStatus\": true,\r\n" + " \"gdprStatus\": true,\r\n"
+ " \"countryCode\": \"GB\",\r\n" + " \"registerAppVersion\": \"VeSync 3.1.37 build3\",\r\n"
+ " \"registerTime\": \"2021-10-14 17:35:50\",\r\n"
+ " \"verifyEmail\": \"david.goodyear@gmail.com\",\r\n" + " \"heightCm\": 0.0,\r\n"
+ " \"weightTargetSt\": 0.0,\r\n" + " \"heightUnit\": \"FT\",\r\n"
+ " \"heightFt\": 0.0,\r\n" + " \"weightTargetKg\": 0.0,\r\n"
+ " \"weightTargetLb\": 0.0,\r\n" + " \"weightUnit\": \"LB\",\r\n"
+ " \"targetBfr\": 0.0,\r\n" + " \"displayFlag\": [],\r\n"
+ " \"real_weight_kg\": 0.0,\r\n" + " \"real_weight_lb\": 0.0,\r\n"
+ " \"real_weight_unit\": \"lb\",\r\n" + " \"heart_rate_zones\": 0.0,\r\n"
+ " \"run_step_long_cm\": 0.0,\r\n" + " \"walk_step_long_cm\": 0.0,\r\n"
+ " \"step_target\": 0.0,\r\n" + " \"sleep_target_mins\": 0.0,\r\n"
+ " \"token\": \"AccessTokenString=\"\r\n" + " }\r\n" + "}";
public static final String testGoodLoginResponseBody = """
{
"traceId": "1634253816",
"code": 0,
"msg": "request success",
"result": {
"isRequiredVerify": true,
"accountID": "5328043",
"avatarIcon": "https://image.vesync.com/defaultImages/user/avatar_nor.png",
"birthday": "",
"gender": "",
"acceptLanguage": "en",
"userType": "1",
"nickName": "david.goodyear",
"mailConfirmation": true,
"termsStatus": true,
"gdprStatus": true,
"countryCode": "GB",
"registerAppVersion": "VeSync 3.1.37 build3",
"registerTime": "2021-10-14 17:35:50",
"verifyEmail": "david.goodyear@gmail.com",
"heightCm": 0.0,
"weightTargetSt": 0.0,
"heightUnit": "FT",
"heightFt": 0.0,
"weightTargetKg": 0.0,
"weightTargetLb": 0.0,
"weightUnit": "LB",
"targetBfr": 0.0,
"displayFlag": [],
"real_weight_kg": 0.0,
"real_weight_lb": 0.0,
"real_weight_unit": "lb",
"heart_rate_zones": 0.0,
"run_step_long_cm": 0.0,
"walk_step_long_cm": 0.0,
"step_target": 0.0,
"sleep_target_mins": 0.0,
"token": "AccessTokenString="
}
}\
""";
@Test
public void testParseLoginGoodResponse() {
@@ -67,8 +89,14 @@ public class VesyncLoginResponseTest {
@Test
public void testParseLoginFailResponse() {
String testReponse = "{\r\n" + " \"traceId\": \"1634253816\",\r\n" + " \"code\": -11201022,\r\n"
+ " \"msg\": \"password incorrect\",\r\n" + " \"result\": null\r\n" + "}";
String testReponse = """
{
"traceId": "1634253816",
"code": -11201022,
"msg": "password incorrect",
"result": null
}\
""";
VesyncLoginResponse response = VeSyncConstants.GSON.fromJson(testReponse,
VesyncLoginResponse.class);
if (response != null) {

View File

@@ -28,46 +28,48 @@ import static org.junit.jupiter.api.Assertions.*;
@NonNullByDefault
public class VesyncManagedDevicesPageTest {
public final static String testGoodSearchResponsePageBody = "{\n" +
" \"traceId\": \"1634387642\",\n" +
" \"code\": 0,\n" +
" \"msg\": \"request success\",\n" +
" \"result\": {\n" +
" \"total\": 2,\n" +
" \"pageSize\": 100,\n" +
" \"pageNo\": 1,\n" +
" \"list\": [\n" +
" {\n" +
" \"deviceRegion\": \"EU\",\n" +
" \"isOwner\": true,\n" +
" \"authKey\": null,\n" +
" \"deviceName\": \"Air Filter\",\n" +
" \"deviceImg\": \"https://image.vesync.com/defaultImages/Core_400S_Series/icon_core400s_purifier_160.png\",\n" +
" \"cid\": \"cidValue1\",\n" +
" \"deviceStatus\": \"on\",\n" +
" \"connectionStatus\": \"online\",\n" +
" \"connectionType\": \"WiFi+BTOnboarding+BTNotify\",\n" +
" \"deviceType\": \"Core400S\",\n" +
" \"type\": \"wifi-air\",\n" +
" \"uuid\": \"abcdefab-1234-1234-abcd-123498761234\",\n" +
" \"configModule\": \"WiFiBTOnboardingNotify_AirPurifier_Core400S_EU\",\n" +
" \"macID\": \"ab:cd:ef:12:34:56\",\n" +
" \"mode\": \"simModeData\",\n" +
" \"speed\": 4,\n" +
" \"extension\": {\n" +
" \"airQuality\": -1,\n" +
" \"airQualityLevel\": 1,\n" +
" \"mode\": \"auto\",\n" +
" \"fanSpeedLevel\": \"1\"\n" +
" },\n" +
" \"currentFirmVersion\": null,\n" +
" \"subDeviceNo\": \"simSubDevice\",\n" +
" \"subDeviceType\": \"simSubDeviceType\",\n" +
" \"deviceFirstSetupTime\": \"Oct 15, 2021 3:43:02 PM\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
public static final String testGoodSearchResponsePageBody = """
{
"traceId": "1634387642",
"code": 0,
"msg": "request success",
"result": {
"total": 2,
"pageSize": 100,
"pageNo": 1,
"list": [
{
"deviceRegion": "EU",
"isOwner": true,
"authKey": null,
"deviceName": "Air Filter",
"deviceImg": "https://image.vesync.com/defaultImages/Core_400S_Series/icon_core400s_purifier_160.png",
"cid": "cidValue1",
"deviceStatus": "on",
"connectionStatus": "online",
"connectionType": "WiFi+BTOnboarding+BTNotify",
"deviceType": "Core400S",
"type": "wifi-air",
"uuid": "abcdefab-1234-1234-abcd-123498761234",
"configModule": "WiFiBTOnboardingNotify_AirPurifier_Core400S_EU",
"macID": "ab:cd:ef:12:34:56",
"mode": "simModeData",
"speed": 4,
"extension": {
"airQuality": -1,
"airQualityLevel": 1,
"mode": "auto",
"fanSpeedLevel": "1"
},
"currentFirmVersion": null,
"subDeviceNo": "simSubDevice",
"subDeviceType": "simSubDeviceType",
"deviceFirstSetupTime": "Oct 15, 2021 3:43:02 PM"
}
]
}
}\
""";
@Test
public void testParseManagedDevicesSearchGoodResponse() {

View File

@@ -29,21 +29,23 @@ import static org.junit.jupiter.api.Assertions.fail;
@NonNullByDefault
public class VesyncV1AirPurifierDeviceDetailsTest {
public final static String testAirPurifierResponseBasedOnCore400S = "{\n" +
" \"code\": 0,\n" +
" \"msg\": \"request success\",\n" +
" \"traceId\": \"1634255391\",\n" +
" \"screenStatus\": \"on1\",\n" +
" \"airQuality\": 1,\n" +
" \"level\": 2,\n" +
" \"mode\": \"manual\",\n" +
" \"deviceName\": \"Lounge Air Purifier\",\n" +
" \"currentFirmVersion\": \"1.0.17\",\n" +
" \"childLock\": \"off1\",\n" +
" \"deviceStatus\": \"on2\",\n" +
" \"deviceImg\": \"https://image.vesync.com/defaultImages/Core_400S_Series/icon_core400s_purifier_160.png\",\n" +
" \"connectionStatus\": \"online\"\n" +
"}";
public static final String testAirPurifierResponseBasedOnCore400S = """
{
"code": 0,
"msg": "request success",
"traceId": "1634255391",
"screenStatus": "on1",
"airQuality": 1,
"level": 2,
"mode": "manual",
"deviceName": "Lounge Air Purifier",
"currentFirmVersion": "1.0.17",
"childLock": "off1",
"deviceStatus": "on2",
"deviceImg": "https://image.vesync.com/defaultImages/Core_400S_Series/icon_core400s_purifier_160.png",
"connectionStatus": "online"
}\
""";
@Test
public void testParseV1AirPurifierDeviceDetailsResponse() {