Java 17 features (H-M) (#15520)

- 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-08 14:36:59 +02:00
committed by GitHub
parent 3751fd0646
commit edaf17b345
519 changed files with 2703 additions and 2660 deletions

View File

@@ -12,7 +12,6 @@
*/
package org.openhab.binding.homeconnect.internal.client;
import static java.util.Collections.singletonList;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
import static org.openhab.binding.homeconnect.internal.client.HttpHelper.*;
@@ -880,7 +879,7 @@ public class HomeConnectApiClient {
private void checkResponseCode(int desiredCode, Request request, ContentResponse response, @Nullable String haId,
@Nullable String requestPayload)
throws CommunicationException, AuthorizationException, ApplianceOfflineException {
checkResponseCode(singletonList(desiredCode), request, response, haId, requestPayload);
checkResponseCode(List.of(desiredCode), request, response, haId, requestPayload);
}
private void checkResponseCode(List<Integer> desiredCodes, Request request, ContentResponse response,

View File

@@ -60,8 +60,8 @@ public class HomeConnectDiscoveryService extends AbstractDiscoveryService
@Override
public void setThingHandler(ThingHandler handler) {
if (handler instanceof HomeConnectBridgeHandler) {
this.bridgeHandler = (HomeConnectBridgeHandler) handler;
if (handler instanceof HomeConnectBridgeHandler homeConnectBridgeHandler) {
this.bridgeHandler = homeConnectBridgeHandler;
}
}

View File

@@ -453,8 +453,8 @@ public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler i
Bridge bridge = getBridge();
if (bridge != null) {
BridgeHandler bridgeHandler = bridge.getHandler();
if (bridgeHandler instanceof HomeConnectBridgeHandler) {
return Optional.of((HomeConnectBridgeHandler) bridgeHandler);
if (bridgeHandler instanceof HomeConnectBridgeHandler homeConnectBridgeHandler) {
return Optional.of(homeConnectBridgeHandler);
}
}
return Optional.empty();
@@ -537,7 +537,7 @@ public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler i
* @param channelUID channel UID
*/
protected void updateChannel(ChannelUID channelUID) {
if (!getApiClient().isPresent()) {
if (getApiClient().isEmpty()) {
logger.error("Cannot update channel. No instance of api client found! thing={}, haId={}", getThingLabel(),
getThingHaId());
return;
@@ -1206,23 +1206,23 @@ public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler i
protected void handleTemperatureCommand(final ChannelUID channelUID, final Command command,
final HomeConnectApiClient apiClient)
throws CommunicationException, AuthorizationException, ApplianceOfflineException {
if (command instanceof QuantityType) {
QuantityType<?> quantity = (QuantityType<?>) command;
if (command instanceof QuantityType quantityCommand) {
String value;
String unit;
try {
if (quantity.getUnit().equals(SIUnits.CELSIUS) || quantity.getUnit().equals(ImperialUnits.FAHRENHEIT)) {
unit = quantity.getUnit().toString();
value = String.valueOf(quantity.intValue());
if (quantityCommand.getUnit().equals(SIUnits.CELSIUS)
|| quantityCommand.getUnit().equals(ImperialUnits.FAHRENHEIT)) {
unit = quantityCommand.getUnit().toString();
value = String.valueOf(quantityCommand.intValue());
} else {
logger.debug("Converting target temperature from {}{} to °C value. thing={}, haId={}",
quantity.intValue(), quantity.getUnit().toString(), getThingLabel(), getThingHaId());
quantityCommand.intValue(), quantityCommand.getUnit().toString(), getThingLabel(),
getThingHaId());
unit = "°C";
var celsius = quantity.toUnit(SIUnits.CELSIUS);
var celsius = quantityCommand.toUnit(SIUnits.CELSIUS);
if (celsius == null) {
logger.warn("Converting temperature to celsius failed! quantity={}", quantity);
logger.warn("Converting temperature to celsius failed! quantity={}", quantityCommand);
value = null;
} else {
value = String.valueOf(celsius.intValue());
@@ -1272,10 +1272,10 @@ public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler i
} else {
newBrightness = currentBrightness - BRIGHTNESS_DIM_STEP;
}
} else if (command instanceof PercentType) {
newBrightness = (int) Math.floor(((PercentType) command).doubleValue());
} else if (command instanceof DecimalType) {
newBrightness = ((DecimalType) command).intValue();
} else if (command instanceof PercentType percentCommand) {
newBrightness = (int) Math.floor(percentCommand.doubleValue());
} else if (command instanceof DecimalType decimalCommand) {
newBrightness = decimalCommand.intValue();
}
// check in in range
@@ -1308,8 +1308,8 @@ public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler i
apiClient.setAmbientLightColorState(getThingHaId(), STATE_AMBIENT_LIGHT_COLOR_CUSTOM_COLOR);
}
if (command instanceof HSBType) {
apiClient.setAmbientLightCustomColorState(getThingHaId(), mapColor((HSBType) command));
if (command instanceof HSBType hsbCommand) {
apiClient.setAmbientLightCustomColorState(getThingHaId(), mapColor(hsbCommand));
} else if (command instanceof StringType) {
apiClient.setAmbientLightCustomColorState(getThingHaId(), command.toFullString());
}
@@ -1546,9 +1546,9 @@ public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler i
.filter(option -> OPTION_DRYER_DRYING_TARGET.equals(option.getKey())).findFirst();
// Save options in cache only if we got options for all expected channels
if (cacheToSet && (!channelSpinSpeed.isPresent() || optionsSpinSpeed.isPresent())
&& (!channelTemperature.isPresent() || optionsTemperature.isPresent())
&& (!channelDryingTarget.isPresent() || optionsDryingTarget.isPresent())) {
if (cacheToSet && (channelSpinSpeed.isEmpty() || optionsSpinSpeed.isPresent())
&& (channelTemperature.isEmpty() || optionsTemperature.isPresent())
&& (channelDryingTarget.isEmpty() || optionsDryingTarget.isPresent())) {
logger.debug("Saving options in cache for program '{}'.", programKey);
availableProgramOptionsCache.put(programKey, availableProgramOptions);
}
@@ -1673,7 +1673,7 @@ public abstract class AbstractHomeConnectThingHandler extends BaseThingHandler i
private boolean addUnsupportedProgramInCache(String programKey) {
Optional<AvailableProgram> prog = programsCache.stream().filter(program -> programKey.equals(program.getKey()))
.findFirst();
if (!prog.isPresent()) {
if (prog.isEmpty()) {
programsCache.add(new AvailableProgram(programKey, false));
logger.debug("{} added in programs cache as an unsupported program", programKey);
return true;

View File

@@ -19,10 +19,10 @@ import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -232,7 +232,7 @@ public class HomeConnectBridgeHandler extends BaseBridgeHandler {
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(HomeConnectDiscoveryService.class);
return Set.of(HomeConnectDiscoveryService.class);
}
/**

View File

@@ -49,15 +49,35 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class HomeConnectHoodHandler extends AbstractHomeConnectThingHandler {
private static final String START_VENTING_INTENSIVE_STAGE_PAYLOAD_TEMPLATE = "\n" + "{\n" + " \"data\": {\n"
+ " \"key\": \"Cooking.Common.Program.Hood.Venting\",\n" + " \"options\": [\n"
+ " {\n" + " \"key\": \"Cooking.Common.Option.Hood.IntensiveLevel\",\n"
+ " \"value\": \"%s\"\n" + " }\n" + " ]\n" + " }\n" + "}";
private static final String START_VENTING_INTENSIVE_STAGE_PAYLOAD_TEMPLATE = """
private static final String START_VENTING_STAGE_PAYLOAD_TEMPLATE = "\n" + "{\n" + " \"data\": {\n"
+ " \"key\": \"Cooking.Common.Program.Hood.Venting\",\n" + " \"options\": [\n"
+ " {\n" + " \"key\": \"Cooking.Common.Option.Hood.VentingLevel\",\n"
+ " \"value\": \"%s\"\n" + " }\n" + " ]\n" + " }\n" + "}";
{
"data": {
"key": "Cooking.Common.Program.Hood.Venting",
"options": [
{
"key": "Cooking.Common.Option.Hood.IntensiveLevel",
"value": "%s"
}
]
}
}\
""";
private static final String START_VENTING_STAGE_PAYLOAD_TEMPLATE = """
{
"data": {
"key": "Cooking.Common.Program.Hood.Venting",
"options": [
{
"key": "Cooking.Common.Option.Hood.VentingLevel",
"value": "%s"
}
]
}
}\
""";
private final Logger logger = LoggerFactory.getLogger(HomeConnectHoodHandler.class);