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

@@ -102,7 +102,7 @@ public class PLCLogoClient extends S7Client {
// read first portion directly to data
result = super.ReadArea(Area, DBNumber, Start, packet, WordLength, Data);
while ((result == 0) && (offset < Amount)) {
byte buffer[] = new byte[Math.min(Amount - offset, packet)];
byte[] buffer = new byte[Math.min(Amount - offset, packet)];
result = super.ReadArea(Area, DBNumber, offset, buffer.length, WordLength, buffer);
System.arraycopy(buffer, 0, Data, offset, buffer.length);
offset = offset + buffer.length;

View File

@@ -69,8 +69,8 @@ public class PLCLogoHandlerFactory extends BaseThingHandlerFactory {
@Override
protected @Nullable ThingHandler createHandler(Thing thing) {
if (THING_TYPE_DEVICE.equals(thing.getThingTypeUID()) && (thing instanceof Bridge)) {
return new PLCBridgeHandler((Bridge) thing);
if (THING_TYPE_DEVICE.equals(thing.getThingTypeUID()) && (thing instanceof Bridge bridge)) {
return new PLCBridgeHandler(bridge);
} else if (THING_TYPE_ANALOG.equals(thing.getThingTypeUID())) {
return new PLCAnalogHandler(thing);
} else if (THING_TYPE_DIGITAL.equals(thing.getThingTypeUID())) {

View File

@@ -21,7 +21,6 @@ import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Set;
import java.util.TreeSet;
@@ -55,7 +54,7 @@ import org.slf4j.LoggerFactory;
public class PLCDiscoveryService extends AbstractDiscoveryService {
private final Logger logger = LoggerFactory.getLogger(PLCDiscoveryService.class);
private static final Set<ThingTypeUID> THING_TYPES_UIDS = Collections.singleton(THING_TYPE_DEVICE);
private static final Set<ThingTypeUID> THING_TYPES_UIDS = Set.of(THING_TYPE_DEVICE);
private static final String LOGO_HOST = "address";
private static final int LOGO_PORT = 102;

View File

@@ -53,7 +53,7 @@ import Moka7.S7Client;
@NonNullByDefault
public class PLCAnalogHandler extends PLCCommonHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_ANALOG);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ANALOG);
private final Logger logger = LoggerFactory.getLogger(PLCAnalogHandler.class);
private AtomicReference<PLCAnalogConfiguration> config = new AtomicReference<>();
@@ -118,11 +118,11 @@ public class PLCAnalogHandler extends PLCCommonHandler {
} else {
logger.debug("Can not read data from LOGO!: {}.", S7Client.ErrorText(result));
}
} else if (command instanceof DecimalType) {
} else if (command instanceof DecimalType decimalCommand) {
byte[] buffer = new byte[2];
String type = channel.getAcceptedItemType();
if (ANALOG_ITEM.equalsIgnoreCase(type)) {
S7.SetShortAt(buffer, 0, ((DecimalType) command).intValue());
S7.SetShortAt(buffer, 0, decimalCommand.intValue());
} else {
logger.debug("Channel {} will not accept {} items.", channelUID, type);
}
@@ -262,7 +262,7 @@ public class PLCAnalogHandler extends PLCCommonHandler {
cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription("Analog " + text + " block " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
cBuilder.withProperties(Map.of(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
setOldValue(name, null);
}

View File

@@ -17,7 +17,6 @@ import static org.openhab.binding.plclogo.internal.PLCLogoBindingConstants.*;
import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -61,7 +60,7 @@ import Moka7.S7Client;
@NonNullByDefault
public class PLCBridgeHandler extends BaseBridgeHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DEVICE);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_DEVICE);
private final Logger logger = LoggerFactory.getLogger(PLCBridgeHandler.class);
@@ -281,11 +280,10 @@ public class PLCBridgeHandler extends BaseBridgeHandler {
@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
super.childHandlerInitialized(childHandler, childThing);
if (childHandler instanceof PLCCommonHandler) {
PLCCommonHandler handler = (PLCCommonHandler) childHandler;
if (childHandler instanceof PLCCommonHandler plcCommonHandler) {
synchronized (handlers) {
if (!handlers.contains(handler)) {
handlers.add(handler);
if (!handlers.contains(plcCommonHandler)) {
handlers.add(plcCommonHandler);
}
}
}
@@ -293,11 +291,10 @@ public class PLCBridgeHandler extends BaseBridgeHandler {
@Override
public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) {
if (childHandler instanceof PLCCommonHandler) {
PLCCommonHandler handler = (PLCCommonHandler) childHandler;
if (childHandler instanceof PLCCommonHandler plcCommonHandler) {
synchronized (handlers) {
if (handlers.contains(handler)) {
handlers.remove(handler);
if (handlers.contains(plcCommonHandler)) {
handlers.remove(plcCommonHandler);
}
}
}

View File

@@ -243,8 +243,8 @@ public abstract class PLCCommonHandler extends BaseThingHandler {
Bridge bridge = getBridge();
if (bridge != null) {
BridgeHandler handler = bridge.getHandler();
if ((handler != null) && (handler instanceof PLCBridgeHandler)) {
return (PLCBridgeHandler) handler;
if (handler instanceof PLCBridgeHandler bridgeHandler) {
return bridgeHandler;
}
}
return null;

View File

@@ -15,8 +15,8 @@ package org.openhab.binding.plclogo.internal.handler;
import static org.openhab.binding.plclogo.internal.PLCLogoBindingConstants.*;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
@@ -53,7 +53,7 @@ import Moka7.S7Client;
@NonNullByDefault
public class PLCDateTimeHandler extends PLCCommonHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DATETIME);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_DATETIME);
private final Logger logger = LoggerFactory.getLogger(PLCDateTimeHandler.class);
private AtomicReference<PLCDateTimeConfiguration> config = new AtomicReference<>();
@@ -89,11 +89,11 @@ public class PLCDateTimeHandler extends PLCCommonHandler {
} else {
logger.debug("Can not read data from LOGO!: {}.", S7Client.ErrorText(result));
}
} else if (command instanceof DateTimeType) {
} else if (command instanceof DateTimeType dateTimeCommand) {
byte[] buffer = new byte[2];
String type = channel.getAcceptedItemType();
if (DATE_TIME_ITEM.equalsIgnoreCase(type)) {
ZonedDateTime datetime = ((DateTimeType) command).getZonedDateTime();
ZonedDateTime datetime = dateTimeCommand.getZonedDateTime();
if ("Time".equalsIgnoreCase(channelUID.getId())) {
buffer[0] = S7.ByteToBCD(datetime.getHour());
buffer[1] = S7.ByteToBCD(datetime.getMinute());
@@ -216,14 +216,14 @@ public class PLCDateTimeHandler extends PLCCommonHandler {
cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription(text + " block parameter " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
cBuilder.withProperties(Map.of(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
cBuilder = ChannelBuilder.create(new ChannelUID(thing.getUID(), VALUE_CHANNEL), ANALOG_ITEM);
cBuilder.withType(new ChannelTypeUID(BINDING_ID, ANALOG_ITEM.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription(text + " block parameter " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
cBuilder.withProperties(Map.of(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
setOldValue(name, null);

View File

@@ -55,7 +55,7 @@ import Moka7.S7Client;
@NonNullByDefault
public class PLCDigitalHandler extends PLCCommonHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DIGITAL);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_DIGITAL);
private final Logger logger = LoggerFactory.getLogger(PLCDigitalHandler.class);
private AtomicReference<PLCDigitalConfiguration> config = new AtomicReference<>();
@@ -187,9 +187,8 @@ public class PLCDigitalHandler extends PLCCommonHandler {
protected void updateState(ChannelUID channelUID, State state) {
super.updateState(channelUID, state);
DecimalType value = state.as(DecimalType.class);
if (state instanceof OpenClosedType) {
OpenClosedType type = (OpenClosedType) state;
value = new DecimalType(type == OpenClosedType.CLOSED ? 1 : 0);
if (state instanceof OpenClosedType openClosedState) {
value = new DecimalType(openClosedState == OpenClosedType.CLOSED ? 1 : 0);
}
Channel channel = thing.getChannel(channelUID.getId());
@@ -272,7 +271,7 @@ public class PLCDigitalHandler extends PLCCommonHandler {
cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription("Digital " + text + " block " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
cBuilder.withProperties(Map.of(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
setOldValue(name, null);
}

View File

@@ -14,8 +14,8 @@ package org.openhab.binding.plclogo.internal.handler;
import static org.openhab.binding.plclogo.internal.PLCLogoBindingConstants.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
@@ -52,7 +52,7 @@ import Moka7.S7Client;
@NonNullByDefault
public class PLCMemoryHandler extends PLCCommonHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_MEMORY);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_MEMORY);
private final Logger logger = LoggerFactory.getLogger(PLCMemoryHandler.class);
private AtomicReference<PLCMemoryConfiguration> config = new AtomicReference<>();
@@ -108,15 +108,15 @@ public class PLCMemoryHandler extends PLCCommonHandler {
} else {
logger.debug("Can not read data from LOGO!: {}.", S7Client.ErrorText(result));
}
} else if (command instanceof DecimalType) {
} else if (command instanceof DecimalType decimalCommand) {
int length = MEMORY_BYTE.equalsIgnoreCase(kind) ? 1 : 2;
byte[] buffer = new byte[MEMORY_DWORD.equalsIgnoreCase(kind) ? 4 : length];
if (ANALOG_ITEM.equalsIgnoreCase(type) && MEMORY_BYTE.equalsIgnoreCase(kind)) {
buffer[0] = ((DecimalType) command).byteValue();
buffer[0] = decimalCommand.byteValue();
} else if (ANALOG_ITEM.equalsIgnoreCase(type) && MEMORY_WORD.equalsIgnoreCase(kind)) {
S7.SetShortAt(buffer, 0, ((DecimalType) command).intValue());
S7.SetShortAt(buffer, 0, decimalCommand.intValue());
} else if (ANALOG_ITEM.equalsIgnoreCase(type) && MEMORY_DWORD.equalsIgnoreCase(kind)) {
S7.SetDIntAt(buffer, 0, ((DecimalType) command).intValue());
S7.SetDIntAt(buffer, 0, decimalCommand.intValue());
} else {
logger.debug("Channel {} will not accept {} items.", channelUID, type);
}
@@ -124,10 +124,10 @@ public class PLCMemoryHandler extends PLCCommonHandler {
if (result != 0) {
logger.debug("Can not write data to LOGO!: {}.", S7Client.ErrorText(result));
}
} else if (command instanceof OnOffType) {
} else if (command instanceof OnOffType onOffCommand) {
byte[] buffer = new byte[1];
if (DIGITAL_OUTPUT_ITEM.equalsIgnoreCase(type) && MEMORY_BYTE.equalsIgnoreCase(kind)) {
S7.SetBitAt(buffer, 0, 0, ((OnOffType) command) == OnOffType.ON);
S7.SetBitAt(buffer, 0, 0, onOffCommand == OnOffType.ON);
} else {
logger.debug("Channel {} will not accept {} items.", channelUID, type);
}
@@ -291,7 +291,7 @@ public class PLCMemoryHandler extends PLCCommonHandler {
cBuilder.withType(new ChannelTypeUID(BINDING_ID, type.toLowerCase()));
cBuilder.withLabel(name);
cBuilder.withDescription(text + " in/output block " + name);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, name));
cBuilder.withProperties(Map.of(BLOCK_PROPERTY, name));
tBuilder.withChannel(cBuilder.build());
setOldValue(name, null);

View File

@@ -14,8 +14,8 @@ package org.openhab.binding.plclogo.internal.handler;
import static org.openhab.binding.plclogo.internal.PLCLogoBindingConstants.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@@ -55,7 +55,7 @@ import Moka7.S7Client;
@NonNullByDefault
public class PLCPulseHandler extends PLCCommonHandler {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_PULSE);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_PULSE);
private final Logger logger = LoggerFactory.getLogger(PLCPulseHandler.class);
private AtomicReference<PLCPulseConfiguration> config = new AtomicReference<>();
@@ -191,9 +191,8 @@ public class PLCPulseHandler extends PLCCommonHandler {
protected void updateState(ChannelUID channelUID, State state) {
super.updateState(channelUID, state);
DecimalType value = state.as(DecimalType.class);
if (state instanceof OpenClosedType) {
OpenClosedType type = (OpenClosedType) state;
value = new DecimalType(type == OpenClosedType.CLOSED ? 1 : 0);
if (state instanceof OpenClosedType openClosedState) {
value = new DecimalType(openClosedState == OpenClosedType.CLOSED ? 1 : 0);
}
setOldValue(channelUID.getId(), value);
@@ -272,7 +271,7 @@ public class PLCPulseHandler extends PLCCommonHandler {
cBuilder.withType(new ChannelTypeUID(BINDING_ID, bType.toLowerCase()));
cBuilder.withLabel(bName);
cBuilder.withDescription("Control block " + bName);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, bName));
cBuilder.withProperties(Map.of(BLOCK_PROPERTY, bName));
tBuilder.withChannel(cBuilder.build());
setOldValue(STATE_CHANNEL, null);
@@ -282,7 +281,7 @@ public class PLCPulseHandler extends PLCCommonHandler {
cBuilder.withType(new ChannelTypeUID(BINDING_ID, oType.toLowerCase()));
cBuilder.withLabel(oName);
cBuilder.withDescription("Observed block " + oName);
cBuilder.withProperties(Collections.singletonMap(BLOCK_PROPERTY, oName));
cBuilder.withProperties(Map.of(BLOCK_PROPERTY, oName));
tBuilder.withChannel(cBuilder.build());
setOldValue(OBSERVE_CHANNEL, null);