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

@@ -43,8 +43,8 @@ public class SatelEventLogActions implements ThingActions {
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof SatelEventLogHandler) {
this.handler = (SatelEventLogHandler) handler;
if (handler instanceof SatelEventLogHandler logHandler) {
this.handler = logHandler;
}
}

View File

@@ -106,9 +106,7 @@ public class ReadEventCommand extends SatelCommandBase {
if (year > currentYear) {
year -= 4;
}
LocalDateTime result = LocalDateTime.of(year, (payload[2] >> 4) & 0x0f, payload[1] & 0x1f, minutes / 60,
minutes % 60);
return result;
return LocalDateTime.of(year, (payload[2] >> 4) & 0x0f, payload[1] & 0x1f, minutes / 60, minutes % 60);
}
/**

View File

@@ -224,9 +224,8 @@ public abstract class SatelCommandBase extends SatelMessage implements SatelComm
public String getVersion(int offset) {
// build version string
final byte[] payload = getResponse().getPayload();
String verStr = new String(payload, offset, 1) + "." + new String(payload, offset + 1, 2) + " "
return new String(payload, offset, 1) + "." + new String(payload, offset + 1, 2) + " "
+ new String(payload, offset + 3, 4) + "-" + new String(payload, offset + 7, 2) + "-"
+ new String(payload, offset + 9, 2);
return verStr;
}
}

View File

@@ -14,7 +14,6 @@ package org.openhab.binding.satel.internal.handler;
import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
@@ -46,7 +45,7 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class Atd100Handler extends WirelessChannelsHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_ATD100);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ATD100);
private final Logger logger = LoggerFactory.getLogger(getClass());

View File

@@ -17,6 +17,7 @@ import static org.openhab.binding.satel.internal.config.Ethm1Config.HOST;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -40,7 +41,7 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class Ethm1BridgeHandler extends SatelBridgeHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_ETHM1);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ETHM1);
private final Logger logger = LoggerFactory.getLogger(Ethm1BridgeHandler.class);
@@ -71,8 +72,8 @@ public class Ethm1BridgeHandler extends SatelBridgeHandler {
// Check whether an IP address is provided
if (host.isBlank()) {
configStatusMessages = Collections.singletonList(ConfigStatusMessage.Builder.error(HOST)
.withMessageKeySuffix("hostEmpty").withArguments(HOST).build());
configStatusMessages = List.of(ConfigStatusMessage.Builder.error(HOST).withMessageKeySuffix("hostEmpty")
.withArguments(HOST).build());
} else {
configStatusMessages = Collections.emptyList();
}

View File

@@ -17,6 +17,7 @@ import static org.openhab.binding.satel.internal.config.IntRSConfig.PORT;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -41,7 +42,7 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class IntRSBridgeHandler extends SatelBridgeHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_INTRS);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_INTRS);
private final Logger logger = LoggerFactory.getLogger(IntRSBridgeHandler.class);
@@ -76,8 +77,8 @@ public class IntRSBridgeHandler extends SatelBridgeHandler {
// Check whether a serial port is provided
if (port.isBlank()) {
configStatusMessages = Collections.singletonList(ConfigStatusMessage.Builder.error(PORT)
.withMessageKeySuffix("portEmpty").withArguments(PORT).build());
configStatusMessages = List.of(ConfigStatusMessage.Builder.error(PORT).withMessageKeySuffix("portEmpty")
.withArguments(PORT).build());
} else {
configStatusMessages = Collections.emptyList();
}

View File

@@ -17,7 +17,6 @@ import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
import java.nio.charset.Charset;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -55,7 +54,7 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class SatelEventLogHandler extends SatelThingHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_EVENTLOG);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_EVENTLOG);
private static final String NOT_AVAILABLE_TEXT = "N/A";
private static final String DETAILS_SEPARATOR = ", ";
@@ -166,8 +165,8 @@ public class SatelEventLogHandler extends SatelThingHandler {
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("New command for {}: {}", channelUID, command);
if (CHANNEL_INDEX.equals(channelUID.getId()) && command instanceof DecimalType) {
int eventIndex = ((DecimalType) command).intValue();
if (CHANNEL_INDEX.equals(channelUID.getId()) && command instanceof DecimalType decimalCommand) {
int eventIndex = decimalCommand.intValue();
withBridgeHandlerPresent(bridgeHandler -> readEvent(eventIndex).ifPresent(entry -> {
// update items
updateState(CHANNEL_INDEX, new DecimalType(entry.getIndex()));
@@ -190,7 +189,7 @@ public class SatelEventLogHandler extends SatelThingHandler {
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(SatelEventLogActions.class);
return Set.of(SatelEventLogActions.class);
}
/**
@@ -276,7 +275,7 @@ public class SatelEventLogHandler extends SatelThingHandler {
eventDetails = "." + readEventCmd.getSource() + "."
+ (readEventCmd.getObject() * 32 + readEventCmd.getUserControlNumber());
Optional<EventDescription> eventDescNext = getEventDescription(readEventCmd.getNextIndex());
if (!eventDescNext.isPresent()) {
if (eventDescNext.isEmpty()) {
return Optional.empty();
}
final EventDescription eventDescNextItem = eventDescNext.get();

View File

@@ -14,7 +14,6 @@ package org.openhab.binding.satel.internal.handler;
import static org.openhab.binding.satel.internal.SatelBindingConstants.THING_TYPE_OUTPUT;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
@@ -39,7 +38,7 @@ import org.openhab.core.types.Command;
@NonNullByDefault
public class SatelOutputHandler extends WirelessChannelsHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_OUTPUT);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_OUTPUT);
public SatelOutputHandler(Thing thing) {
super(thing);

View File

@@ -14,7 +14,6 @@ package org.openhab.binding.satel.internal.handler;
import static org.openhab.binding.satel.internal.SatelBindingConstants.THING_TYPE_PARTITION;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
@@ -39,7 +38,7 @@ import org.openhab.core.types.Command;
@NonNullByDefault
public class SatelPartitionHandler extends SatelStateThingHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_PARTITION);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_PARTITION);
public SatelPartitionHandler(Thing thing) {
super(thing);

View File

@@ -14,7 +14,6 @@ package org.openhab.binding.satel.internal.handler;
import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
@@ -44,7 +43,7 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class SatelShutterHandler extends SatelStateThingHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_SHUTTER);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SHUTTER);
private final Logger logger = LoggerFactory.getLogger(SatelShutterHandler.class);

View File

@@ -15,7 +15,6 @@ package org.openhab.binding.satel.internal.handler;
import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Set;
@@ -51,7 +50,7 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class SatelSystemHandler extends SatelStateThingHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_SYSTEM);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SYSTEM);
private static final Set<String> STATUS_CHANNELS = Stream
.of(CHANNEL_DATE_TIME, CHANNEL_SERVICE_MODE, CHANNEL_TROUBLES, CHANNEL_TROUBLES_MEMORY,
@@ -112,8 +111,8 @@ public class SatelSystemHandler extends SatelStateThingHandler {
DateTimeType dateTime = null;
if (command instanceof StringType) {
dateTime = DateTimeType.valueOf(command.toString());
} else if (command instanceof DateTimeType) {
dateTime = (DateTimeType) command;
} else if (command instanceof DateTimeType dateTimeCommand) {
dateTime = dateTimeCommand;
}
if (dateTime != null) {
return Optional.of(new SetClockCommand(dateTime.getZonedDateTime()

View File

@@ -57,9 +57,9 @@ public abstract class SatelThingHandler extends BaseThingHandler implements Sate
final Bridge bridge = getBridge();
if (bridge != null) {
final ThingHandler handler = bridge.getHandler();
if (handler != null && handler instanceof SatelBridgeHandler) {
((SatelBridgeHandler) handler).addEventListener(this);
this.bridgeHandler = (SatelBridgeHandler) handler;
if (handler instanceof SatelBridgeHandler satelBridgeHandler) {
satelBridgeHandler.addEventListener(this);
this.bridgeHandler = satelBridgeHandler;
}
if (bridge.getStatus() == ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);

View File

@@ -14,7 +14,6 @@ package org.openhab.binding.satel.internal.handler;
import static org.openhab.binding.satel.internal.SatelBindingConstants.THING_TYPE_ZONE;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
@@ -39,7 +38,7 @@ import org.openhab.core.types.Command;
@NonNullByDefault
public class SatelZoneHandler extends WirelessChannelsHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_ZONE);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ZONE);
public SatelZoneHandler(Thing thing) {
super(thing);

View File

@@ -79,9 +79,9 @@ public abstract class WirelessChannelsHandler extends SatelStateThingHandler {
@Override
protected int getStateBitNbr(StateType stateType) {
int bitNbr = getThingConfig().getId() - 1;
if (stateType instanceof TroubleState) {
if (stateType instanceof TroubleState troubleState) {
// for wireless devices we need to correct bit number
switch ((TroubleState) stateType) {
switch (troubleState) {
case DEVICE_LOBATT1:
case DEVICE_NOCOMM1:
case OUTPUT_NOCOMM1:

View File

@@ -44,7 +44,7 @@ public class EncryptionHelper {
}
// build encryption/decryption key based on given password
byte passwordBytes[] = keyString.getBytes();
byte[] passwordBytes = keyString.getBytes();
byte[] keyBytes = new byte[24];
for (int i = 0; i < 12; ++i) {
@@ -67,7 +67,7 @@ public class EncryptionHelper {
* @throws GeneralSecurityException
* on decryption errors
*/
public void decrypt(byte buffer[]) throws GeneralSecurityException {
public void decrypt(byte[] buffer) throws GeneralSecurityException {
byte[] cv = new byte[16];
byte[] c = new byte[16];
byte[] temp = new byte[16];
@@ -104,7 +104,7 @@ public class EncryptionHelper {
* @param buffer bytes to encrypt
* @throws GeneralSecurityException on encryption errors
*/
public void encrypt(byte buffer[]) throws GeneralSecurityException {
public void encrypt(byte[] buffer) throws GeneralSecurityException {
byte[] cv = new byte[16];
byte[] p = new byte[16];
int count = buffer.length;

View File

@@ -113,7 +113,7 @@ public class SatelMessage {
* @return the message as array of bytes
*/
public byte[] getBytes() {
byte buffer[] = new byte[this.payload.length + 3];
byte[] buffer = new byte[this.payload.length + 3];
buffer[0] = this.command;
if (this.payload.length > 0) {
System.arraycopy(this.payload, 0, buffer, 1, this.payload.length);