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

@@ -52,8 +52,8 @@ public class DimmerOutputProfile implements StateProfile {
Optional<Object> outputs12 = getConfig(profileContext, "controlOutputs12");
ramp.ifPresent(b -> {
if (b instanceof BigDecimal) {
rampMs = (int) (((BigDecimal) b).doubleValue() * 1000);
if (b instanceof BigDecimal decimalValue) {
rampMs = (int) (decimalValue.doubleValue() * 1000);
} else {
logger.warn("Could not parse 'ramp', unexpected type, should be float: {}", ramp);
}
@@ -86,10 +86,10 @@ public class DimmerOutputProfile implements StateProfile {
logger.warn("Unsupported 'ramp' setting. Will be forced to 250ms: {}", rampMs);
}
BigDecimal value;
if (command instanceof DecimalType) {
value = ((DecimalType) command).toBigDecimal();
} else if (command instanceof OnOffType) {
value = ((OnOffType) command) == OnOffType.ON ? BigDecimal.valueOf(100) : BigDecimal.ZERO;
if (command instanceof DecimalType decimalCommand) {
value = decimalCommand.toBigDecimal();
} else if (command instanceof OnOffType onOffCommand) {
value = onOffCommand == OnOffType.ON ? BigDecimal.valueOf(100) : BigDecimal.ZERO;
} else {
logger.warn("Unsupported type: {}", command.toFullString());
return;

View File

@@ -82,8 +82,8 @@ public class LcnModuleDiscoveryService extends AbstractDiscoveryService
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof PckGatewayHandler) {
this.bridgeHandler = (PckGatewayHandler) handler;
if (handler instanceof PckGatewayHandler gatewayHandler) {
this.bridgeHandler = gatewayHandler;
}
}

View File

@@ -14,12 +14,12 @@ package org.openhab.binding.lcn.internal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -117,8 +117,8 @@ public class LcnModuleHandler extends BaseThingHandler {
Object invertUpDown = channel.getConfiguration().get("invertUpDown");
// Initialize value converters
if (unitObject instanceof String) {
switch ((String) unitObject) {
if (unitObject instanceof String stringValue) {
switch (stringValue) {
case "power":
case "energy":
converters.put(channel.getUID(), new S0Converter(parameterObject));
@@ -207,35 +207,33 @@ public class LcnModuleHandler extends BaseThingHandler {
if (command instanceof RefreshType) {
number.ifPresent(n -> subHandler.handleRefresh(channelGroup, n));
subHandler.handleRefresh(channelUid.getIdWithoutGroup());
} else if (command instanceof OnOffType) {
subHandler.handleCommandOnOff((OnOffType) command, channelGroup, number.get());
} else if (command instanceof DimmerOutputCommand) {
subHandler.handleCommandDimmerOutput((DimmerOutputCommand) command, number.get());
} else if (command instanceof PercentType && number.isPresent()) {
subHandler.handleCommandPercent((PercentType) command, channelGroup, number.get());
} else if (command instanceof HSBType) {
subHandler.handleCommandHsb((HSBType) command, channelUid.getIdWithoutGroup());
} else if (command instanceof PercentType) {
subHandler.handleCommandPercent((PercentType) command, channelGroup, channelUid.getIdWithoutGroup());
} else if (command instanceof StringType) {
subHandler.handleCommandString((StringType) command, number.orElse(0));
} else if (command instanceof DecimalType) {
DecimalType decimalType = (DecimalType) command;
DecimalType nativeValue = getConverter(channelUid).onCommandFromItem(decimalType.doubleValue());
} else if (command instanceof OnOffType onOffCommand) {
subHandler.handleCommandOnOff(onOffCommand, channelGroup, number.get());
} else if (command instanceof DimmerOutputCommand outputCommand) {
subHandler.handleCommandDimmerOutput(outputCommand, number.get());
} else if (command instanceof PercentType percentCommand && number.isPresent()) {
subHandler.handleCommandPercent(percentCommand, channelGroup, number.get());
} else if (command instanceof HSBType hsbCommand) {
subHandler.handleCommandHsb(hsbCommand, channelUid.getIdWithoutGroup());
} else if (command instanceof PercentType percentCommand) {
subHandler.handleCommandPercent(percentCommand, channelGroup, channelUid.getIdWithoutGroup());
} else if (command instanceof StringType stringCommand) {
subHandler.handleCommandString(stringCommand, number.orElse(0));
} else if (command instanceof DecimalType decimalCommand) {
DecimalType nativeValue = getConverter(channelUid).onCommandFromItem(decimalCommand.doubleValue());
subHandler.handleCommandDecimal(nativeValue, channelGroup, number.get());
} else if (command instanceof QuantityType) {
QuantityType<?> quantityType = (QuantityType<?>) command;
DecimalType nativeValue = getConverter(channelUid).onCommandFromItem(quantityType);
} else if (command instanceof QuantityType quantityCommand) {
DecimalType nativeValue = getConverter(channelUid).onCommandFromItem(quantityCommand);
subHandler.handleCommandDecimal(nativeValue, channelGroup, number.get());
} else if (command instanceof UpDownType) {
} else if (command instanceof UpDownType upDownCommand) {
Channel channel = thing.getChannel(channelUid);
if (channel != null) {
Object invertConfig = channel.getConfiguration().get("invertUpDown");
boolean invertUpDown = invertConfig instanceof Boolean && (boolean) invertConfig;
subHandler.handleCommandUpDown((UpDownType) command, channelGroup, number.get(), invertUpDown);
boolean invertUpDown = invertConfig instanceof Boolean bool && bool;
subHandler.handleCommandUpDown(upDownCommand, channelGroup, number.get(), invertUpDown);
}
} else if (command instanceof StopMoveType) {
subHandler.handleCommandStopMove((StopMoveType) command, channelGroup, number.get());
} else if (command instanceof StopMoveType stopMoveCommand) {
subHandler.handleCommandStopMove(stopMoveCommand, channelGroup, number.get());
} else {
throw new LcnException("Unsupported command type");
}
@@ -397,7 +395,7 @@ public class LcnModuleHandler extends BaseThingHandler {
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(LcnModuleActions.class);
return Set.of(LcnModuleActions.class);
}
/**

View File

@@ -13,8 +13,8 @@
package org.openhab.binding.lcn.internal;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -46,12 +46,12 @@ public class LcnProfileFactory implements ProfileFactory, ProfileTypeProvider {
@Override
public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
return Collections.singleton(DimmerOutputProfile.UID);
return Set.of(DimmerOutputProfile.UID);
}
@Override
public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
return Collections.singleton(ProfileTypeBuilder.newState(DimmerOutputProfile.UID, "Dimmer Output (%)")
return Set.of(ProfileTypeBuilder.newState(DimmerOutputProfile.UID, "Dimmer Output (%)")
.withSupportedItemTypes(CoreItemFactory.DIMMER, CoreItemFactory.COLOR)
.withSupportedChannelTypeUIDs(
new ChannelTypeUID(LcnBindingConstants.BINDING_ID, LcnChannelGroup.OUTPUT.name().toLowerCase()))

View File

@@ -13,8 +13,8 @@
package org.openhab.binding.lcn.internal;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -114,7 +114,7 @@ public class PckGatewayHandler extends BaseBridgeHandler {
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(LcnModuleDiscoveryService.class);
return Set.of(LcnModuleDiscoveryService.class);
}
@Override

View File

@@ -86,14 +86,14 @@ public final class LcnDefs {
OFF,
ON,
BLINK,
FLICKER;
FLICKER
}
/** Possible states for LCN logic-operations. */
public enum LogicOpStatus {
NOT,
OR, // Note: Actually not correct since AND won't be OR also
AND;
AND
}
/** Time units used for several LCN commands. */
@@ -101,7 +101,7 @@ public final class LcnDefs {
SECONDS,
MINUTES,
HOURS,
DAYS;
DAYS
}
/** Relay-state modifiers used in LCN commands. */

View File

@@ -251,7 +251,7 @@ public class Connection {
writeInProgress = false;
if (sendQueue.size() > 0) {
if (!sendQueue.isEmpty()) {
/**
* This could lead to stack overflows, since the CompletionHandler may run in
* the same Thread as triggerWriteToSocket() is invoked (see

View File

@@ -36,8 +36,8 @@ public class S0Converter extends ValueConverter {
if (parameter == null) {
pulsesPerKwh = 1000;
logger.debug("Pulses per kWh not set. Assuming 1000 imp./kWh.");
} else if (parameter instanceof BigDecimal) {
pulsesPerKwh = ((BigDecimal) parameter).doubleValue();
} else if (parameter instanceof BigDecimal decimalValue) {
pulsesPerKwh = decimalValue.doubleValue();
} else {
logger.warn("Could not parse 'pulses', unexpected type, should be float or integer: {}", parameter);
}

View File

@@ -103,10 +103,10 @@ public class ValueConverter extends Converter {
*/
@Override
public State onStateUpdateFromHandler(State state) throws LcnException {
if (state instanceof DecimalType) {
if (state instanceof DecimalType decimalValue) {
Unit<?> localUnit = unit;
if (localUnit != null) {
return QuantityType.valueOf(toHumanReadable(((DecimalType) state).longValue()), localUnit);
return QuantityType.valueOf(toHumanReadable(decimalValue.longValue()), localUnit);
}
return state;

View File

@@ -13,7 +13,7 @@
package org.openhab.binding.lcn.internal.subhandler;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -90,6 +90,6 @@ public abstract class AbstractLcnModuleRollershutterRelaySubHandler extends Abst
@Override
public Collection<Pattern> getPckStatusMessagePatterns() {
return Collections.singleton(PATTERN);
return Set.of(PATTERN);
}
}

View File

@@ -13,7 +13,7 @@
package org.openhab.binding.lcn.internal.subhandler;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
@@ -57,6 +57,6 @@ public class LcnModuleBinarySensorSubHandler extends AbstractLcnModuleSubHandler
@Override
public Collection<Pattern> getPckStatusMessagePatterns() {
return Collections.singleton(PATTERN);
return Set.of(PATTERN);
}
}

View File

@@ -13,7 +13,7 @@
package org.openhab.binding.lcn.internal.subhandler;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -73,6 +73,6 @@ public class LcnModuleHostCommandSubHandler extends AbstractLcnModuleSubHandler
@Override
public Collection<Pattern> getPckStatusMessagePatterns() {
return Collections.singleton(SEND_KEY_PATTERN);
return Set.of(SEND_KEY_PATTERN);
}
}

View File

@@ -14,7 +14,7 @@ package org.openhab.binding.lcn.internal.subhandler;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
@@ -90,6 +90,6 @@ public class LcnModuleKeyLockTableSubHandler extends AbstractLcnModuleSubHandler
@Override
public Collection<Pattern> getPckStatusMessagePatterns() {
return Collections.singleton(PATTERN);
return Set.of(PATTERN);
}
}

View File

@@ -13,7 +13,7 @@
package org.openhab.binding.lcn.internal.subhandler;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -52,6 +52,6 @@ public class LcnModuleMetaFirmwareSubHandler extends AbstractLcnModuleSubHandler
@Override
public Collection<Pattern> getPckStatusMessagePatterns() {
return Collections.singleton(PATTERN);
return Set.of(PATTERN);
}
}

View File

@@ -13,7 +13,7 @@
package org.openhab.binding.lcn.internal.subhandler;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
@@ -81,6 +81,6 @@ public class LcnModuleRelaySubHandler extends AbstractLcnModuleSubHandler {
@Override
public Collection<Pattern> getPckStatusMessagePatterns() {
return Collections.singleton(PATTERN);
return Set.of(PATTERN);
}
}

View File

@@ -13,7 +13,7 @@
package org.openhab.binding.lcn.internal.subhandler;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -53,6 +53,6 @@ public class LcnModuleS0CounterSubHandler extends AbstractLcnModuleVariableSubHa
@Override
public Collection<Pattern> getPckStatusMessagePatterns() {
return Collections.singleton(PATTERN);
return Set.of(PATTERN);
}
}