Java 17 features (A-G) (#15516)

- 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-05 22:30:16 +02:00
committed by GitHub
parent a0dc5c05f2
commit cf10b3e9c7
486 changed files with 2053 additions and 1955 deletions

View File

@@ -71,14 +71,14 @@ public class EcovacsVacuumActions implements ThingActions {
}
public static void playSound(@Nullable ThingActions actions, String type) {
if (actions instanceof EcovacsVacuumActions) {
((EcovacsVacuumActions) actions).playSound(type);
if (actions instanceof EcovacsVacuumActions action) {
action.playSound(type);
}
}
public static void playSoundWithId(@Nullable ThingActions actions, int soundId) {
if (actions instanceof EcovacsVacuumActions) {
((EcovacsVacuumActions) actions).playSoundWithId(soundId);
if (actions instanceof EcovacsVacuumActions action) {
action.playSoundWithId(soundId);
}
}
}

View File

@@ -40,9 +40,8 @@ public class GetActiveMapIdCommand extends IotDeviceCommand<String> {
@Override
public String convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
CachedMapInfoReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
CachedMapInfoReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
CachedMapInfoReport resp = jsonResponse.getResponsePayloadAs(gson, CachedMapInfoReport.class);
return resp.mapInfos.stream().filter(i -> i.used != 0).map(i -> i.mapId).findFirst().orElse("");
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();

View File

@@ -40,9 +40,8 @@ public class GetBatteryInfoCommand extends IotDeviceCommand<Integer> {
@Override
public Integer convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
BatteryReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
BatteryReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
BatteryReport resp = jsonResponse.getResponsePayloadAs(gson, BatteryReport.class);
return resp.percent;
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();

View File

@@ -41,9 +41,8 @@ public class GetChargeStateCommand extends IotDeviceCommand<ChargeMode> {
@Override
public ChargeMode convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
ChargeReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
ChargeReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
ChargeReport resp = jsonResponse.getResponsePayloadAs(gson, ChargeReport.class);
return resp.isCharging != 0 ? ChargeMode.CHARGING : ChargeMode.IDLE;
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();

View File

@@ -50,8 +50,7 @@ public class GetCleanStateCommand extends IotDeviceCommand<CleanMode> {
@Override
public CleanMode convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
final PortalIotCommandJsonResponse jsonResponse = (PortalIotCommandJsonResponse) response;
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
final CleanMode mode;
if (version == ProtocolVersion.JSON) {
CleanReport resp = jsonResponse.getResponsePayloadAs(gson, CleanReport.class);

View File

@@ -65,8 +65,8 @@ public class GetComponentLifeSpanCommand extends IotDeviceCommand<Integer> {
@Override
public Integer convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
JsonElement respPayloadRaw = ((PortalIotCommandJsonResponse) response).getResponsePayload(gson);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
JsonElement respPayloadRaw = jsonResponse.getResponsePayload(gson);
Type type = new TypeToken<List<ComponentLifeSpanReport>>() {
}.getType();
try {

View File

@@ -47,9 +47,8 @@ public class GetContinuousCleaningCommand extends IotDeviceCommand<Boolean> {
@Override
public Boolean convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
EnabledStateReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
EnabledStateReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
EnabledStateReport resp = jsonResponse.getResponsePayloadAs(gson, EnabledStateReport.class);
return resp.enabled != 0;
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();

View File

@@ -42,8 +42,8 @@ public class GetErrorCommand extends IotDeviceCommand<Optional<Integer>> {
@Override
public Optional<Integer> convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version,
Gson gson) throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
ErrorReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson, ErrorReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
ErrorReport resp = jsonResponse.getResponsePayloadAs(gson, ErrorReport.class);
if (resp.errorCodes.isEmpty()) {
return Optional.empty();
}

View File

@@ -65,9 +65,8 @@ public class GetMapSpotAreasWithMapIdCommand extends IotDeviceCommand<List<Strin
@Override
public List<String> convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
MapSetReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
MapSetReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
MapSetReport resp = jsonResponse.getResponsePayloadAs(gson, MapSetReport.class);
return resp.subsets.stream().map(i -> i.id).collect(Collectors.toList());
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();

View File

@@ -41,9 +41,8 @@ public class GetMoppingWaterAmountCommand extends IotDeviceCommand<MoppingWaterA
@Override
public MoppingWaterAmount convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version,
Gson gson) throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
WaterInfoReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
WaterInfoReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
WaterInfoReport resp = jsonResponse.getResponsePayloadAs(gson, WaterInfoReport.class);
return MoppingWaterAmount.fromApiValue(resp.waterAmount);
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();

View File

@@ -42,9 +42,8 @@ public class GetNetworkInfoCommand extends IotDeviceCommand<NetworkInfo> {
@Override
public NetworkInfo convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
NetworkInfoReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
NetworkInfoReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
NetworkInfoReport resp = jsonResponse.getResponsePayloadAs(gson, NetworkInfoReport.class);
try {
return new NetworkInfo(resp.ip, resp.mac, resp.ssid, Integer.valueOf(resp.rssi));
} catch (NumberFormatException e) {

View File

@@ -41,8 +41,8 @@ public class GetSuctionPowerCommand extends IotDeviceCommand<SuctionPower> {
@Override
public SuctionPower convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
SpeedReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson, SpeedReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
SpeedReport resp = jsonResponse.getResponsePayloadAs(gson, SpeedReport.class);
return SuctionPower.fromJsonValue(resp.speedLevel);
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();

View File

@@ -55,8 +55,8 @@ public class GetTotalStatsCommand extends IotDeviceCommand<GetTotalStatsCommand.
@Override
public TotalStats convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
return ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson, TotalStats.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
return jsonResponse.getResponsePayloadAs(gson, TotalStats.class);
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();
String area = XPathUtils.getFirstXPathMatch(payload, "//@a").getNodeValue();

View File

@@ -41,9 +41,8 @@ public class GetVolumeCommand extends IotDeviceCommand<Integer> {
@Override
public Integer convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
JsonResponse resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
JsonResponse.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
JsonResponse resp = jsonResponse.getResponsePayloadAs(gson, JsonResponse.class);
return resp.volume;
} else {
// unsupported in XML case?

View File

@@ -40,9 +40,8 @@ public class GetWaterSystemPresentCommand extends IotDeviceCommand<Boolean> {
@Override
public Boolean convertResponse(AbstractPortalIotCommandResponse response, ProtocolVersion version, Gson gson)
throws DataParsingException {
if (response instanceof PortalIotCommandJsonResponse) {
WaterInfoReport resp = ((PortalIotCommandJsonResponse) response).getResponsePayloadAs(gson,
WaterInfoReport.class);
if (response instanceof PortalIotCommandJsonResponse jsonResponse) {
WaterInfoReport resp = jsonResponse.getResponsePayloadAs(gson, WaterInfoReport.class);
return resp.waterPlatePresent != 0;
} else {
String payload = ((PortalIotCommandXmlResponse) response).getResponsePayloadXml();

View File

@@ -172,7 +172,7 @@ public final class EcovacsApiImpl implements EcovacsApi {
List<EcovacsDevice> devices = new ArrayList<>();
for (Device dev : getDeviceList()) {
Optional<DeviceDescription> descOpt = Optional.ofNullable(descriptions.get(dev.getDeviceClass()));
if (!descOpt.isPresent()) {
if (descOpt.isEmpty()) {
if (products == null) {
products = getIotProductMap();
}
@@ -226,7 +226,7 @@ public final class EcovacsApiImpl implements EcovacsApi {
DeviceDescription desc = descEntry.getValue();
if (desc.deviceClassLink != null) {
Optional<DeviceDescription> linkedDescOpt = Optional.ofNullable(descs.get(desc.deviceClassLink));
if (!linkedDescOpt.isPresent()) {
if (linkedDescOpt.isEmpty()) {
logger.warn("Device description {} links unknown description {}", desc.deviceClass,
desc.deviceClassLink);
}

View File

@@ -177,8 +177,8 @@ public class EcovacsIotMqDevice implements EcovacsDevice {
logger.debug("Established MQTT connection to device {}", getSerialNumber());
} catch (ExecutionException e) {
Throwable cause = e.getCause();
boolean isAuthFailure = cause instanceof Mqtt3ConnAckException && ((Mqtt3ConnAckException) cause)
.getMqttMessage().getReturnCode() == Mqtt3ConnAckReturnCode.NOT_AUTHORIZED;
boolean isAuthFailure = cause instanceof Mqtt3ConnAckException connAckException
&& connAckException.getMqttMessage().getReturnCode() == Mqtt3ConnAckReturnCode.NOT_AUTHORIZED;
throw new EcovacsApiException(e, isAuthFailure);
}
}

View File

@@ -347,8 +347,7 @@ public class EcovacsXmppDevice implements EcovacsDevice {
return null;
}
if (iqRequest instanceof DeviceCommandIQ) {
DeviceCommandIQ iq = (DeviceCommandIQ) iqRequest;
if (iqRequest instanceof DeviceCommandIQ iq) {
try {
if (!iq.id.isEmpty()) {
@@ -372,8 +371,8 @@ public class EcovacsXmppDevice implements EcovacsDevice {
} catch (DataParsingException e) {
listener.onEventStreamFailure(EcovacsXmppDevice.this, e);
}
} else if (iqRequest instanceof ErrorIQ) {
StanzaError error = ((ErrorIQ) iqRequest).getError();
} else if (iqRequest instanceof ErrorIQ errorIQ) {
StanzaError error = errorIQ.getError();
logger.trace("{}: Got error response {}", getSerialNumber(), error);
listener.onEventStreamFailure(EcovacsXmppDevice.this,
new XMPPException.XMPPErrorException(iqRequest, error));

View File

@@ -24,5 +24,5 @@ public enum PortalTodo {
@SerializedName("GetDeviceList")
GET_DEVICE_LIST,
@SerializedName("loginByItToken")
LOGIN_BY_TOKEN;
LOGIN_BY_TOKEN
}

View File

@@ -28,5 +28,5 @@ public enum ChargeMode {
@SerializedName("SlotCharging")
CHARGING,
@SerializedName("Idle")
IDLE;
IDLE
}

View File

@@ -14,9 +14,9 @@ package org.openhab.binding.ecovacs.internal.discovery;
import static org.openhab.binding.ecovacs.internal.EcovacsBindingConstants.*;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -57,13 +57,13 @@ public class EcovacsDeviceDiscoveryService extends AbstractDiscoveryService impl
this::scanForDevices);
public EcovacsDeviceDiscoveryService() {
super(Collections.singleton(THING_TYPE_VACUUM), DISCOVER_TIMEOUT_SECONDS, true);
super(Set.of(THING_TYPE_VACUUM), DISCOVER_TIMEOUT_SECONDS, true);
}
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof EcovacsApiHandler) {
this.apiHandler = (EcovacsApiHandler) handler;
if (handler instanceof EcovacsApiHandler ecovacsApiHandler) {
this.apiHandler = ecovacsApiHandler;
this.apiHandler.setDiscoveryService(this);
}
}

View File

@@ -15,8 +15,8 @@ package org.openhab.binding.ecovacs.internal.handler;
import static org.openhab.binding.ecovacs.internal.EcovacsBindingConstants.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -96,7 +96,7 @@ public class EcovacsApiHandler extends BaseBridgeHandler {
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(EcovacsDeviceDiscoveryService.class);
return Set.of(EcovacsDeviceDiscoveryService.class);
}
@Override

View File

@@ -18,10 +18,10 @@ import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -152,7 +152,7 @@ public class EcovacsVacuumHandler extends BaseThingHandler implements EcovacsDev
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(EcovacsVacuumActions.class);
return Set.of(EcovacsVacuumActions.class);
}
@Override
@@ -171,8 +171,8 @@ public class EcovacsVacuumHandler extends BaseThingHandler implements EcovacsDev
device.sendCommand(cmd);
return;
}
} else if (channel.equals(CHANNEL_ID_VOICE_VOLUME) && command instanceof DecimalType) {
int volumePercent = ((DecimalType) command).intValue();
} else if (channel.equals(CHANNEL_ID_VOICE_VOLUME) && command instanceof DecimalType volume) {
int volumePercent = volume.intValue();
device.sendCommand(new SetVolumeCommand((volumePercent + 5) / 10));
return;
} else if (channel.equals(CHANNEL_ID_SUCTION_POWER) && command instanceof StringType) {
@@ -191,7 +191,7 @@ public class EcovacsVacuumHandler extends BaseThingHandler implements EcovacsDev
if (command instanceof OnOffType) {
device.sendCommand(new SetDustbinAutoEmptyCommand(command == OnOffType.ON));
return;
} else if (command instanceof StringType && command.toString().equals("trigger")) {
} else if (command instanceof StringType && "trigger".equals(command.toString())) {
device.sendCommand(new EmptyDustbinCommand());
return;
}
@@ -201,8 +201,8 @@ public class EcovacsVacuumHandler extends BaseThingHandler implements EcovacsDev
} else if (channel.equals(CHANNEL_ID_CONTINUOUS_CLEANING) && command instanceof OnOffType) {
device.sendCommand(new SetContinuousCleaningCommand(command == OnOffType.ON));
return;
} else if (channel.equals(CHANNEL_ID_CLEANING_PASSES) && command instanceof DecimalType) {
int passes = ((DecimalType) command).intValue();
} else if (channel.equals(CHANNEL_ID_CLEANING_PASSES) && command instanceof DecimalType type) {
int passes = type.intValue();
device.sendCommand(new SetDefaultCleanPassesCommand(passes));
lastDefaultCleaningPasses = passes; // if we get here, the command was executed successfully
return;