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

@@ -132,7 +132,7 @@ public class EnOceanDeviceDiscoveryService extends AbstractDiscoveryService impl
if (senderIdOffset == null) {
return;
}
} else if ((eep instanceof _4BSMessage) && ((_4BSMessage) eep).isTeachInVariation3Supported()) {
} else if (eep instanceof _4BSMessage message && message.isTeachInVariation3Supported()) {
// if 4BS teach in variation 3 => send response
logger.debug("Sending 4BS teach in variation 3 response to {}", enoceanId);
senderIdOffset = sendTeachInResponse(msg, enoceanId);

View File

@@ -60,7 +60,7 @@ public class A5_20_04 extends A5_20 {
}
private String getStatusRequestEvent() {
return Boolean.valueOf(getBit(getDB0Value(), 6)).toString();
return Boolean.toString(getBit(getDB0Value(), 6));
// return getBit(getDB_0Value(), 6) ? "triggered" : null;
}

View File

@@ -77,15 +77,15 @@ public class A5_38_08_Blinds extends _4BSMessage {
byte angle = 0; // for now, no angle configuration supported
boolean doStop = false;
if (outputCommand instanceof DecimalType) {
position = ((DecimalType) outputCommand).byteValue();
} else if (outputCommand instanceof OnOffType) {
position = (byte) (((OnOffType) outputCommand == OnOffType.ON) ? 0 : 100);
if (outputCommand instanceof DecimalType decimalCommand) {
position = decimalCommand.byteValue();
} else if (outputCommand instanceof OnOffType onOffCommand) {
position = (byte) ((onOffCommand == OnOffType.ON) ? 0 : 100);
} else if (outputCommand instanceof StopMoveType) {
position = ZERO;
doStop = true;
} else if (outputCommand instanceof UpDownType) {
position = (byte) (((UpDownType) outputCommand == UpDownType.UP) ? 0 : 100);
} else if (outputCommand instanceof UpDownType upDownCommand) {
position = (byte) ((upDownCommand == UpDownType.UP) ? 0 : 100);
} else {
logger.warn("Unknown command type {}", outputCommand.getClass().getCanonicalName());
return;

View File

@@ -63,16 +63,14 @@ public class A5_38_08_Dimming extends _4BSMessage {
case CHANNEL_DIMMER:
byte dimmValue;
if (outputCommand instanceof DecimalType) {
dimmValue = ((DecimalType) outputCommand).byteValue();
} else if (outputCommand instanceof OnOffType) {
dimmValue = ((OnOffType) outputCommand == OnOffType.ON) ? SWITCH_100_PERCENT : ZERO;
} else if (outputCommand instanceof IncreaseDecreaseType) {
dimmValue = ((IncreaseDecreaseType) outputCommand == IncreaseDecreaseType.INCREASE)
? SWITCH_100_PERCENT
: ZERO;
} else if (outputCommand instanceof UpDownType) {
dimmValue = ((UpDownType) outputCommand == UpDownType.UP) ? SWITCH_100_PERCENT : ZERO;
if (outputCommand instanceof DecimalType decimalCommand) {
dimmValue = decimalCommand.byteValue();
} else if (outputCommand instanceof OnOffType onOffCommand) {
dimmValue = (onOffCommand == OnOffType.ON) ? SWITCH_100_PERCENT : ZERO;
} else if (outputCommand instanceof IncreaseDecreaseType increaseDecreaseCommand) {
dimmValue = (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE) ? SWITCH_100_PERCENT : ZERO;
} else if (outputCommand instanceof UpDownType upDownCommand) {
dimmValue = (upDownCommand == UpDownType.UP) ? SWITCH_100_PERCENT : ZERO;
} else {
throw new IllegalArgumentException(outputCommand.toFullString() + " is no valid dimming command.");
}

View File

@@ -52,20 +52,19 @@ public class A5_3F_7F_EltakoFRM extends _4BSMessage {
@Override
protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
if (command instanceof PercentType) {
PercentType target = (PercentType) command;
int rawPosition = Math.round(
(PercentType.HUNDRED.floatValue() - target.floatValue()) * TOP / PercentType.HUNDRED.floatValue());
if (command instanceof PercentType percentCommand) {
int rawPosition = Math.round((PercentType.HUNDRED.floatValue() - percentCommand.floatValue()) * TOP
/ PercentType.HUNDRED.floatValue());
int position = Math.min(TOP, Math.max(BOTTOM, rawPosition));
setData((byte) position, ZERO, MOVE, TEACHIN_BIT);
} else if (command instanceof UpDownType) {
if ((UpDownType) command == UpDownType.UP) {
} else if (command instanceof UpDownType upDownCommand) {
if (upDownCommand == UpDownType.UP) {
setData((byte) TOP, ZERO, MOVE, TEACHIN_BIT); // => 0 percent
} else if ((UpDownType) command == UpDownType.DOWN) {
} else if (upDownCommand == UpDownType.DOWN) {
setData((byte) BOTTOM, ZERO, MOVE, TEACHIN_BIT); // => 100 percent
}
} else if (command instanceof StopMoveType) {
if ((StopMoveType) command == StopMoveType.STOP) {
} else if (command instanceof StopMoveType stopMoveCommand) {
if (stopMoveCommand == StopMoveType.STOP) {
setData(ZERO, ZERO, STOP, TEACHIN_BIT);
}
}

View File

@@ -61,21 +61,19 @@ public class A5_3F_7F_EltakoFSB extends _4BSMessage {
shutTime = Math.min(255, config.as(EnOceanChannelRollershutterConfig.class).shutTime);
}
if (command instanceof PercentType) {
if (command instanceof PercentType percentCommand) {
State channelState = getCurrentStateFunc.apply(channelId);
PercentType target = (PercentType) command;
if (target.intValue() == PercentType.ZERO.intValue()) {
if (percentCommand.intValue() == PercentType.ZERO.intValue()) {
setData(ZERO, (byte) shutTime, MOVE_UP, TEACHIN_BIT); // => move completely up
} else if (target.intValue() == PercentType.HUNDRED.intValue()) {
} else if (percentCommand.intValue() == PercentType.HUNDRED.intValue()) {
setData(ZERO, (byte) shutTime, MOVE_DOWN, TEACHIN_BIT); // => move completely down
} else if (channelState != null) {
PercentType current = channelState.as(PercentType.class);
if (current != null) {
if (current.intValue() != target.intValue()) {
byte direction = current.intValue() > target.intValue() ? MOVE_UP : MOVE_DOWN;
if (current.intValue() != percentCommand.intValue()) {
byte direction = current.intValue() > percentCommand.intValue() ? MOVE_UP : MOVE_DOWN;
byte duration = (byte) Math.min(255,
(Math.abs(current.intValue() - target.intValue()) * shutTime)
(Math.abs(current.intValue() - percentCommand.intValue()) * shutTime)
/ PercentType.HUNDRED.intValue());
setData(ZERO, duration, direction, TEACHIN_BIT);
@@ -83,14 +81,14 @@ public class A5_3F_7F_EltakoFSB extends _4BSMessage {
}
}
} else if (command instanceof UpDownType) {
if ((UpDownType) command == UpDownType.UP) {
} else if (command instanceof UpDownType upDownCommand) {
if (upDownCommand == UpDownType.UP) {
setData(ZERO, (byte) shutTime, MOVE_UP, TEACHIN_BIT); // => 0 percent
} else if ((UpDownType) command == UpDownType.DOWN) {
} else if (upDownCommand == UpDownType.DOWN) {
setData(ZERO, (byte) shutTime, MOVE_DOWN, TEACHIN_BIT); // => 100 percent
}
} else if (command instanceof StopMoveType) {
if ((StopMoveType) command == StopMoveType.STOP) {
} else if (command instanceof StopMoveType stopMoveCommand) {
if (stopMoveCommand == StopMoveType.STOP) {
setData(ZERO, (byte) 0xFF, STOP, TEACHIN_BIT);
}
}

View File

@@ -107,11 +107,11 @@ public abstract class D2_01 extends _VLDMessage {
protected void setDimmingData(Command command, byte outputChannel, Configuration config) {
byte outputValue;
if (command instanceof DecimalType) {
if (((DecimalType) command).equals(DecimalType.ZERO)) {
if (command instanceof DecimalType decimalCommand) {
if (decimalCommand.equals(DecimalType.ZERO)) {
outputValue = STATUS_SWITCHING_OFF;
} else {
outputValue = ((DecimalType) command).byteValue();
outputValue = decimalCommand.byteValue();
}
} else if ((OnOffType) command == OnOffType.ON) {
outputValue = STATUS_DIMMING_100;

View File

@@ -58,8 +58,8 @@ public class D2_01_09_Permundo extends D2_01 {
private void setRepeaterMode(Command command) {
if (command == RefreshType.REFRESH) {
senderId = new byte[0]; // make this message invalid as we do not support refresh of repeater status
} else if (command instanceof StringType) {
switch (((StringType) command).toString()) {
} else if (command instanceof StringType stringCommand) {
switch (stringCommand.toString()) {
case EnOceanBindingConstants.REPEATERMODE_LEVEL_1:
setRORG(RORG.MSC).setData((byte) 0x03, (byte) 0x35, (byte) 0x01);
break;
@@ -75,8 +75,8 @@ public class D2_01_09_Permundo extends D2_01 {
private void setEcoMode(Command command) {
if (command == RefreshType.REFRESH) {
senderId = new byte[0]; // make this message invalid as we do not support refresh of ecomode status
} else if (command instanceof OnOffType) {
if (((OnOffType) command) == OnOffType.ON) {
} else if (command instanceof OnOffType onOffCommand) {
if (onOffCommand == OnOffType.ON) {
setRORG(RORG.MSC).setData((byte) 0x03, (byte) 0x36, (byte) 0x01);
} else {
setRORG(RORG.MSC).setData((byte) 0x03, (byte) 0x36, (byte) 0x00);

View File

@@ -48,8 +48,8 @@ public class D2_01_0F_NodON extends D2_01 {
if (channelId.equalsIgnoreCase(CHANNEL_REPEATERMODE)) {
if (command instanceof RefreshType) {
senderId = new byte[0]; // make this message invalid as we do not support refresh of repeter status
} else if (command instanceof StringType) {
switch (((StringType) command).toString()) {
} else if (command instanceof StringType stringCommand) {
switch (stringCommand.toString()) {
case EnOceanBindingConstants.REPEATERMODE_LEVEL_1:
setRORG(RORG.MSC).setData((byte) 0x00, (byte) 0x46, (byte) 0x08, (byte) 0x01, (byte) 0x01);
break;

View File

@@ -48,8 +48,8 @@ public class D2_01_12_NodON extends D2_01 {
if (channelId.equalsIgnoreCase(CHANNEL_REPEATERMODE)) {
if (command instanceof RefreshType) {
senderId = new byte[0]; // make this message invalid as we do not support refresh of repeter status
} else if (command instanceof StringType) {
switch (((StringType) command).toString()) {
} else if (command instanceof StringType stringCommand) {
switch (stringCommand.toString()) {
case EnOceanBindingConstants.REPEATERMODE_LEVEL_1:
setRORG(RORG.MSC).setData((byte) 0x00, (byte) 0x46, (byte) 0x08, (byte) 0x01, (byte) 0x01);
break;

View File

@@ -75,8 +75,8 @@ public class D2_05_00 extends _VLDMessage {
if (command == StopMoveType.STOP) {
setData((byte) (outputChannel + CMD_ACTUATOR_STOP));
}
} else if (command instanceof PercentType) {
setData((byte) (((PercentType) command).intValue()), (byte) 0x00, (byte) 0x00,
} else if (command instanceof PercentType percentCommand) {
setData((byte) (percentCommand.intValue()), (byte) 0x00, (byte) 0x00,
(byte) (outputChannel + CMD_ACTUATOR_SET_POSITION));
}
}

View File

@@ -49,8 +49,8 @@ public class D2_05_00_NodON extends D2_05_00 {
if (channelId.equals(CHANNEL_REPEATERMODE)) {
if (command == RefreshType.REFRESH) {
senderId = new byte[0]; // make this message invalid as we do not support refresh of repeter status
} else if (command instanceof StringType) {
switch (((StringType) command).toString()) {
} else if (command instanceof StringType stringCommand) {
switch (stringCommand.toString()) {
case EnOceanBindingConstants.REPEATERMODE_LEVEL_1:
setRORG(RORG.MSC).setData((byte) 0x00, (byte) 0x46, (byte) 0x08, (byte) 0x01, (byte) 0x01);
break;

View File

@@ -86,15 +86,15 @@ public class D2_50 extends _VLDMessage {
} else {
switch (channelId) {
case CHANNEL_VENTILATIONOPERATIONMODE:
if (command instanceof StringType) {
byte value = (byte) (Helper.tryParseInt(((StringType) command).toString(), 15) & 0x0f);
if (command instanceof StringType stringCommand) {
byte value = (byte) (Helper.tryParseInt(stringCommand.toString(), 15) & 0x0f);
setData((byte) (MT_CONTROL + value), CONTROL_NOACTION, TMOC_NOACTION, THRESHOLD_NOACTION,
THRESHOLD_NOACTION, CONTROL_NOACTION);
}
break;
case CHANNEL_TIMEROPERATIONMODE:
if (command instanceof OnOffType) {
byte value = (OnOffType) command == OnOffType.ON ? TMOC_ACTIVATE : TMOC_NOACTION;
if (command instanceof OnOffType onOffCommand) {
byte value = onOffCommand == OnOffType.ON ? TMOC_ACTIVATE : TMOC_NOACTION;
setData((byte) (MT_CONTROL + DOMC_NOACTION), CONTROL_NOACTION, value, THRESHOLD_NOACTION,
THRESHOLD_NOACTION, CONTROL_NOACTION);
}

View File

@@ -95,7 +95,7 @@ public class EEPFactory {
}
public static @Nullable EEP buildEEPFromTeachInERP1(ERP1Message msg) {
if (!msg.getIsTeachIn() && !(msg.getRORG() == RORG.RPS)) {
if (!msg.getIsTeachIn() && msg.getRORG() != RORG.RPS) {
return null;
}

View File

@@ -74,8 +74,8 @@ public class F6_02_01 extends F6_02 {
@Override
protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
if (command instanceof StringType) {
String s = ((StringType) command).toString();
if (command instanceof StringType stringCommand) {
String s = stringCommand.toString();
if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
setStatus(_RPSMessage.T21_FLAG);

View File

@@ -73,8 +73,8 @@ public class F6_02_02 extends F6_02 {
@Override
protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
if (command instanceof StringType) {
String s = ((StringType) command).toString();
if (command instanceof StringType stringCommand) {
String s = stringCommand.toString();
if (s.equals(CommonTriggerEvents.DIR1_RELEASED) || s.equals(CommonTriggerEvents.DIR2_RELEASED)) {
setStatus(_RPSMessage.T21_FLAG);

View File

@@ -209,8 +209,8 @@ public abstract class EnOceanBaseThingHandler extends ConfigStatusThingHandler {
return null;
}
ThingHandler handler = bridge.getHandler();
if (handler instanceof EnOceanBridgeHandler) {
this.gateway = (EnOceanBridgeHandler) handler;
if (handler instanceof EnOceanBridgeHandler bridgeHandler) {
this.gateway = bridgeHandler;
} else {
return null;
}

View File

@@ -116,13 +116,13 @@ public class EnOceanBridgeHandler extends ConfigStatusBridgeHandler implements T
}
}
});
} else if (command instanceof StringType) {
sendMessage(ESP3PacketFactory.CO_WR_REPEATER((StringType) command),
} else if (command instanceof StringType stringCommand) {
sendMessage(ESP3PacketFactory.CO_WR_REPEATER(stringCommand),
new ResponseListenerIgnoringTimeouts<BaseResponse>() {
@Override
public void responseReceived(BaseResponse response) {
if (response.isOK()) {
updateState(channelUID, (StringType) command);
updateState(channelUID, stringCommand);
}
}
});
@@ -130,9 +130,9 @@ public class EnOceanBridgeHandler extends ConfigStatusBridgeHandler implements T
break;
case CHANNEL_SETBASEID:
if (command instanceof StringType) {
if (command instanceof StringType stringCommand) {
try {
byte[] id = HexUtils.hexToBytes(((StringType) command).toFullString());
byte[] id = HexUtils.hexToBytes(stringCommand.toFullString());
sendMessage(ESP3PacketFactory.CO_WR_IDBASE(id),
new ResponseListenerIgnoringTimeouts<BaseResponse>() {

View File

@@ -180,8 +180,8 @@ public class EnOceanClassicDeviceHandler extends EnOceanBaseActuatorHandler {
}
private @Nullable StringType convertToPressedCommand(Command command, SwitchMode switchMode) {
if (command instanceof StringType) {
return (StringType) command;
if (command instanceof StringType stringCommand) {
return stringCommand;
} else if (command instanceof OnOffType) {
switch (switchMode) {
case RockerSwitch:

View File

@@ -210,7 +210,7 @@ public class ESP2Packet {
return ZERO;
}
private byte calcCheckSum(byte data[], int offset, int length) {
private byte calcCheckSum(byte[] data, int offset, int length) {
int checkSum = 0;
for (int i = 0; i < length; i++) {
checkSum += (data[offset + i] & 0xff);
@@ -251,7 +251,7 @@ public class ESP2Packet {
}
}
public static boolean validateCheckSum(byte data[], int length, byte checkSum) {
public static boolean validateCheckSum(byte[] data, int length, byte checkSum) {
int sum = 0;
for (int i = 0; i < length; i++) {
sum += (data[i] & 0xff);

View File

@@ -73,7 +73,7 @@ public class ESP3Packet {
this.basePacket = basePacket;
}
private byte calcCRC8(byte data[], int offset, int length) {
private byte calcCRC8(byte[] data, int offset, int length) {
byte output = 0;
for (int i = offset; i < offset + length; i++) {
int index = (output ^ data[i]) & 0xff;
@@ -112,7 +112,7 @@ public class ESP3Packet {
}
}
public static boolean checkCRC8(byte data[], int length, byte crc8) {
public static boolean checkCRC8(byte[] data, int length, byte crc8) {
byte output = 0;
for (int i = 0; i < length; i++) {
int index = (output ^ data[i]) & 0xff;