Java 17 features (A-G) (#15516)

- 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-05 22:30:16 +02:00
committed by GitHub
parent a0dc5c05f2
commit cf10b3e9c7
486 changed files with 2053 additions and 1955 deletions

View File

@@ -70,6 +70,7 @@ public enum EchonetClass {
return classProperties;
}
@Override
public String toString() {
return name() + "{" + "groupCode=0x" + Integer.toHexString(groupCode) + ", classCode=0x"
+ Integer.toHexString(0xFF & classCode) + '}';

View File

@@ -48,6 +48,7 @@ public class EchonetDevice extends EchonetObject {
this.listener = listener;
}
@Override
public void applyProperty(InstanceKey sourceInstanceKey, Esv esv, final int epcCode, final int pdc,
final ByteBuffer edt) {
final Epc epc = Epc.lookup(instanceKey().klass.groupCode(), instanceKey().klass.classCode(), epcCode);
@@ -112,6 +113,7 @@ public class EchonetDevice extends EchonetObject {
return identificationNumber.toString();
}
@Override
public boolean buildUpdateMessage(final EchonetMessageBuilder messageBuilder, final ShortSupplier tidSupplier,
final long nowMs, InstanceKey managementControllerKey) {
if (pendingSets.isEmpty()) {
@@ -141,6 +143,7 @@ public class EchonetDevice extends EchonetObject {
return true;
}
@Override
public void update(String channelId, State state) {
final Epc epc = epcByChannelId.get(channelId);
if (null == epc) {
@@ -156,12 +159,14 @@ public class EchonetDevice extends EchonetObject {
listener.onRemoved();
}
@Override
public void checkTimeouts() {
if (EchonetLiteBindingConstants.OFFLINE_TIMEOUT_COUNT <= inflightGetRequest.timeoutCount()) {
listener.onOffline();
}
}
@Override
public void refreshAll(long nowMs) {
final EchonetPropertyMap getPropertyMap = this.getPropertyMap;
if (lastPollMs + pollIntervalMs <= nowMs && null != getPropertyMap) {

View File

@@ -100,8 +100,8 @@ public class EchonetDiscoveryService extends AbstractDiscoveryService
@Override
public void setThingHandler(ThingHandler thingHandler) {
if (thingHandler instanceof EchonetLiteBridgeHandler) {
this.bridgeHandler = (EchonetLiteBridgeHandler) thingHandler;
if (thingHandler instanceof EchonetLiteBridgeHandler bridgeHandler) {
this.bridgeHandler = bridgeHandler;
}
}

View File

@@ -19,8 +19,8 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
@@ -90,9 +90,8 @@ public class EchonetLiteBridgeHandler extends BaseBridgeHandler {
private void newDeviceInternal(final NewDeviceMessage message) {
final EchonetObject echonetObject = devicesByKey.get(message.instanceKey);
if (null != echonetObject) {
if (echonetObject instanceof EchonetDevice) {
if (echonetObject instanceof EchonetDevice device) {
logger.debug("Update item: {} already discovered", message.instanceKey);
EchonetDevice device = (EchonetDevice) echonetObject;
device.setTimeouts(message.pollIntervalMs, message.retryTimeoutMs);
device.setListener(message.echonetDeviceListener);
} else {
@@ -198,18 +197,18 @@ public class EchonetLiteBridgeHandler extends BaseBridgeHandler {
Message message;
while (null != (message = requestsPoll())) {
logger.debug("Received request: {}", message);
if (message instanceof NewDeviceMessage) {
newDeviceInternal((NewDeviceMessage) message);
} else if (message instanceof RefreshMessage) {
refreshDeviceInternal((RefreshMessage) message);
} else if (message instanceof RemoveDevice) {
removeDeviceInternal((RemoveDevice) message);
} else if (message instanceof UpdateDevice) {
updateDeviceInternal((UpdateDevice) message);
} else if (message instanceof StartDiscoveryMessage) {
startDiscoveryInternal((StartDiscoveryMessage) message);
} else if (message instanceof StopDiscoveryMessage) {
stopDiscoveryInternal((StopDiscoveryMessage) message);
if (message instanceof NewDeviceMessage deviceMessage) {
newDeviceInternal(deviceMessage);
} else if (message instanceof RefreshMessage refreshMessage) {
refreshDeviceInternal(refreshMessage);
} else if (message instanceof RemoveDevice device) {
removeDeviceInternal(device);
} else if (message instanceof UpdateDevice device) {
updateDeviceInternal(device);
} else if (message instanceof StartDiscoveryMessage discoveryMessage) {
startDiscoveryInternal(discoveryMessage);
} else if (message instanceof StopDiscoveryMessage discoveryMessage) {
stopDiscoveryInternal(discoveryMessage);
}
}
}
@@ -319,7 +318,7 @@ public class EchonetLiteBridgeHandler extends BaseBridgeHandler {
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singletonList(EchonetDiscoveryService.class);
return List.of(EchonetDiscoveryService.class);
}
private abstract static class Message {
@@ -390,6 +389,7 @@ public class EchonetLiteBridgeHandler extends BaseBridgeHandler {
this.state = state;
}
@Override
public String toString() {
return "UpdateDevice{" + "instanceKey=" + instanceKey + ", channelId='" + channelId + '\'' + ", state="
+ state + "} " + super.toString();

View File

@@ -88,10 +88,10 @@ public class EchonetLiteHandler extends BaseThingHandler implements EchonetDevic
} else {
updateState(channelUID, currentState);
}
} else if (command instanceof State) {
} else if (command instanceof State stateCommand) {
logger.debug("Updating: {} to {}", channelUID, command);
handler.updateDevice(requireNonNull(instanceKey), channelUID.getId(), (State) command);
handler.updateDevice(requireNonNull(instanceKey), channelUID.getId(), stateCommand);
}
}
@@ -124,6 +124,7 @@ public class EchonetLiteHandler extends BaseThingHandler implements EchonetDevic
}
}
@Override
public void handleRemoval() {
@Nullable
final EchonetLiteBridgeHandler bridgeHandler = bridgeHandler();
@@ -136,6 +137,7 @@ public class EchonetLiteHandler extends BaseThingHandler implements EchonetDevic
bridgeHandler.removeDevice(requireNonNull(instanceKey));
}
@Override
public void onInitialised(String identifier, InstanceKey instanceKey, Map<String, String> channelIdAndType) {
logger.debug("Initialised Channels: {}", channelIdAndType);
@@ -167,6 +169,7 @@ public class EchonetLiteHandler extends BaseThingHandler implements EchonetDevic
updateStatus(ThingStatus.ONLINE);
}
@Override
public void onUpdated(final String channelId, final State value) {
stateByChannelId.put(channelId, value);
@@ -176,10 +179,12 @@ public class EchonetLiteHandler extends BaseThingHandler implements EchonetDevic
updateState(channelId, value);
}
@Override
public void onRemoved() {
updateStatus(ThingStatus.REMOVED);
}
@Override
public void onOffline() {
if (ThingStatus.OFFLINE != getThing().getStatus()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);

View File

@@ -96,6 +96,7 @@ public abstract class EchonetObject {
public void refreshAll(long nowMs) {
}
@Override
public String toString() {
return "ItemBase{" + "instanceKey=" + instanceKey + ", pendingProperties=" + pendingGets + '}';
}

View File

@@ -86,6 +86,7 @@ public class EchonetPropertyMap {
return properties;
}
@Override
public String toString() {
return "EnPropertyMap{" + "propertyMap=" + HexUtil.hex(propertyMap) + '}';
}

View File

@@ -234,16 +234,19 @@ public interface Epc {
this(code, stateCodec, stateCodec);
}
@Override
public int code() {
return code;
}
@Nullable
@Override
public StateDecode decoder() {
return stateDecode;
}
@Nullable
@Override
public StateEncode encoder() {
return stateEncode;
}
@@ -363,16 +366,19 @@ public interface Epc {
this(code, stateCodec, stateCodec);
}
@Override
public int code() {
return code;
}
@Nullable
@Override
public StateDecode decoder() {
return stateDecode;
}
@Nullable
@Override
public StateEncode encoder() {
return stateEncode;
}
@@ -389,6 +395,7 @@ public interface Epc {
this.code = code;
}
@Override
public int code() {
return code;
}
@@ -421,16 +428,19 @@ public interface Epc {
this(code, stateCodec, stateCodec);
}
@Override
public int code() {
return code;
}
@Nullable
@Override
public StateDecode decoder() {
return stateDecode;
}
@Nullable
@Override
public StateEncode encoder() {
return stateEncode;
}
@@ -445,6 +455,7 @@ public interface Epc {
this.code = code;
}
@Override
public int code() {
return code;
}
@@ -480,6 +491,7 @@ public interface Epc {
this.code = code;
}
@Override
public int code() {
return code;
}

View File

@@ -36,6 +36,7 @@ public class InstanceKey {
this.instance = instance;
}
@Override
public String toString() {
return "InstanceKey{" + "address=" + address + ", klass=" + klass + ", instance=" + instance + '}';
}
@@ -45,6 +46,7 @@ public class InstanceKey {
+ hex(instance);
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
@@ -56,6 +58,7 @@ public class InstanceKey {
return instance == that.instance && address.equals(that.address) && klass == that.klass;
}
@Override
public int hashCode() {
return Objects.hash(address, klass, instance);
}

View File

@@ -49,15 +49,18 @@ public interface StateCodec extends StateEncode, StateDecode {
this.off = off;
}
@Override
public State decodeState(final ByteBuffer edt) {
return b(on) == edt.get() ? OnOffType.ON : OnOffType.OFF;
}
@Override
public void encodeState(final State state, final ByteBuffer edt) {
final OnOffType onOff = (OnOffType) state;
edt.put(onOff == OnOffType.ON ? b(on) : b(off));
}
@Override
public String itemType() {
return "Switch";
}
@@ -67,6 +70,7 @@ public interface StateCodec extends StateEncode, StateDecode {
INSTANCE;
@Override
public State decodeState(final ByteBuffer edt) {
final int pdc = edt.remaining();
if (pdc != 4) {
@@ -76,6 +80,7 @@ public interface StateCodec extends StateEncode, StateDecode {
return new StringType("" + (char) edt.get(edt.position() + 2));
}
@Override
public String itemType() {
return "String";
}
@@ -85,10 +90,12 @@ public interface StateCodec extends StateEncode, StateDecode {
INSTANCE;
@Override
public State decodeState(final ByteBuffer edt) {
return new StringType(hex(edt, "", "", "", ""));
}
@Override
public String itemType() {
return "String";
}
@@ -97,6 +104,7 @@ public interface StateCodec extends StateEncode, StateDecode {
enum OperatingTimeDecode implements StateDecode {
INSTANCE;
@Override
public State decodeState(final ByteBuffer edt) {
// Specification isn't explicit about byte order, but seems to be work with testing.
edt.order(ByteOrder.BIG_ENDIAN);
@@ -127,6 +135,7 @@ public interface StateCodec extends StateEncode, StateDecode {
return new QuantityType<>(timeUnit.toSeconds(time), Units.SECOND);
}
@Override
public String itemType() {
return "Number:Time";
}
@@ -158,16 +167,19 @@ public interface StateCodec extends StateEncode, StateDecode {
}
}
@Override
public String itemType() {
return "String";
}
@Override
public State decodeState(final ByteBuffer edt) {
final int value = edt.get() & 0xFF;
final Option option = optionByValue[value];
return null != option ? option.state : unknown;
}
@Override
public void encodeState(final State state, final ByteBuffer edt) {
final Option option = optionByName.get(state.toFullString());
if (null != option) {
@@ -182,15 +194,18 @@ public interface StateCodec extends StateEncode, StateDecode {
INSTANCE;
@Override
public String itemType() {
return "Number";
}
@Override
public State decodeState(final ByteBuffer edt) {
final int value = edt.get(); // Should expand to typed value (mask excluded)
return new DecimalType(value);
}
@Override
public void encodeState(final State state, final ByteBuffer edt) {
edt.put((byte) (((DecimalType) state).intValue()));
}
@@ -199,15 +214,18 @@ public interface StateCodec extends StateEncode, StateDecode {
enum Temperature8bitCodec implements StateCodec {
INSTANCE;
@Override
public State decodeState(final ByteBuffer edt) {
final int value = edt.get();
return new QuantityType<>(value, SIUnits.CELSIUS);
}
@Override
public String itemType() {
return "Number:Temperature";
}
@Override
public void encodeState(final State state, final ByteBuffer edt) {
final @Nullable QuantityType<?> tempCelsius = ((QuantityType<?>) state).toUnit(SIUnits.CELSIUS);
edt.put((byte) (Objects.requireNonNull(tempCelsius).intValue()));