Java 17 features (N-S) (#15565)

- 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-13 08:03:31 +02:00
committed by GitHub
parent 641b482551
commit ab58f4ffb4
471 changed files with 1624 additions and 1868 deletions

View File

@@ -41,6 +41,7 @@ public interface SerialPort extends Closeable {
* @throws IOException
* if an I/O error occurred.
*/
@Override
void close() throws IOException;
/**

View File

@@ -44,7 +44,7 @@ public class SmartMeterBindingConstants {
public static final String CHANNEL_TYPE_METERREADER_OBIS = "channel-type:" + BINDING_ID + ":obis";
public static String getObisChannelIdPattern(String obis) {
return obis.replaceAll("\\.", "-").replaceAll(":|\\*", "_");
return obis.replace(".", "-").replaceAll(":|\\*", "_");
}
public static String getObisChannelId(String obis) {

View File

@@ -269,12 +269,12 @@ public class SmartMeterHandler extends BaseThingHandler {
valueString += " " + value.getUnit();
}
State state = TypeParser.parseState(List.of(QuantityType.class, StringType.class), valueString);
if (channel != null && state instanceof QuantityType) {
if (channel != null && state instanceof QuantityType quantityCommand) {
state = applyConformity(channel, (QuantityType<Q>) state);
Number conversionRatio = (Number) channel.getConfiguration()
.get(SmartMeterBindingConstants.CONFIGURATION_CONVERSION);
if (conversionRatio != null) {
state = ((QuantityType<?>) state).divide(BigDecimal.valueOf(conversionRatio.doubleValue()));
state = quantityCommand.divide(BigDecimal.valueOf(conversionRatio.doubleValue()));
}
}
return state;

View File

@@ -14,7 +14,6 @@ package org.openhab.binding.smartmeter.internal;
import static org.openhab.binding.smartmeter.SmartMeterBindingConstants.THING_TYPE_SMLREADER;
import java.util.Collections;
import java.util.Set;
import java.util.function.Supplier;
@@ -40,7 +39,7 @@ import org.osgi.service.component.annotations.Reference;
@NonNullByDefault
public class SmartMeterHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_SMLREADER);
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_SMLREADER);
private @NonNullByDefault({}) SmartMeterChannelTypeProvider channelProvider;
private @NonNullByDefault({}) Supplier<SerialPortManager> serialPortManagerSupplier = () -> null;

View File

@@ -44,7 +44,7 @@ public class NegateBitParser {
String[] split = substring.split(":");
int negatePosition = Integer.parseInt(split[0]);
boolean negateBit = Integer.parseInt(split[1]) == 0 ? false : true;
boolean status = split.length > 2 ? split[2].equalsIgnoreCase("status") : false;
boolean status = split.length > 2 ? "status".equalsIgnoreCase(split[2]) : false;
return new NegateBitModel((byte) negatePosition, negateBit, obis, status);
}
} catch (Exception e) {

View File

@@ -151,10 +151,8 @@ public final class SmlValueExtractor {
* @return the hex encoded OBIS code as readable string.
*/
protected static String getObisAsString(byte[] octetBytes) {
String formattedObis = String.format(SmartMeterBindingConstants.OBIS_FORMAT_MINIMAL, byteToInt(octetBytes[0]),
return String.format(SmartMeterBindingConstants.OBIS_FORMAT_MINIMAL, byteToInt(octetBytes[0]),
byteToInt(octetBytes[1]), byteToInt(octetBytes[2]), byteToInt(octetBytes[3]), byteToInt(octetBytes[4]));
return formattedObis;
}
public String getObisCode() {

View File

@@ -40,9 +40,8 @@ public class TestNegateBit {
public void testNegateHandlingTrue() {
String negateProperty = "1-0_1-8-0:5:1";
boolean negateState = NegateHandler.shouldNegateState(negateProperty, obis -> {
return new MeterValue<>(obis, "65954", null);
});
boolean negateState = NegateHandler.shouldNegateState(negateProperty,
obis -> new MeterValue<>(obis, "65954", null));
assertTrue(negateState);
}
@@ -51,9 +50,8 @@ public class TestNegateBit {
public void testNegateHandlingFalse() {
String negateProperty = "1-0_1-8-0:5:1";
boolean negateState = NegateHandler.shouldNegateState(negateProperty, obis -> {
return new MeterValue<>(obis, "0", null, "65922");
});
boolean negateState = NegateHandler.shouldNegateState(negateProperty,
obis -> new MeterValue<>(obis, "0", null, "65922"));
assertFalse(negateState);
}