[knx] Java17 instanceof pattern matching (#14718)

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich 2023-03-28 20:49:31 +02:00 committed by GitHub
parent 2c2ba4bed1
commit dba83a33e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 10 deletions

View File

@ -78,8 +78,7 @@ public class DPTUnits {
for (Field field : fields) { for (Field field : fields) {
try { try {
Object o = field.get(null); Object o = field.get(null);
if (o instanceof DPT) { if (o instanceof DPT dpt) {
DPT dpt = (DPT) o;
String unit = dpt.getUnit().replaceAll(" ", ""); String unit = dpt.getUnit().replaceAll(" ", "");
// Calimero provides some units (like "ms⁻²") that can't be parsed by our library because of the // Calimero provides some units (like "ms⁻²") that can't be parsed by our library because of the
// negative exponent // negative exponent

View File

@ -87,8 +87,8 @@ public class ValueEncoder {
DPT dpt = translator.getType(); DPT dpt = translator.getType();
// check for HSBType first, because it extends PercentType as well // check for HSBType first, because it extends PercentType as well
if (value instanceof HSBType) { if (value instanceof HSBType type) {
return handleHSBType(dptId, (HSBType) value); return handleHSBType(dptId, type);
} else if (value instanceof OnOffType) { } else if (value instanceof OnOffType) {
return OnOffType.OFF.equals(value) ? dpt.getLowerValue() : dpt.getUpperValue(); return OnOffType.OFF.equals(value) ? dpt.getLowerValue() : dpt.getUpperValue();
} else if (value instanceof UpDownType) { } else if (value instanceof UpDownType) {
@ -101,15 +101,15 @@ public class ValueEncoder {
return OpenClosedType.CLOSED.equals(value) ? dpt.getLowerValue() : dpt.getUpperValue(); return OpenClosedType.CLOSED.equals(value) ? dpt.getLowerValue() : dpt.getUpperValue();
} else if (value instanceof StopMoveType) { } else if (value instanceof StopMoveType) {
return StopMoveType.STOP.equals(value) ? dpt.getLowerValue() : dpt.getUpperValue(); return StopMoveType.STOP.equals(value) ? dpt.getLowerValue() : dpt.getUpperValue();
} else if (value instanceof PercentType) { } else if (value instanceof PercentType type) {
int intValue = ((PercentType) value).intValue(); int intValue = type.intValue();
return "251.600".equals(dptId) ? String.format("- - - %d %%", intValue) : String.valueOf(intValue); return "251.600".equals(dptId) ? String.format("- - - %d %%", intValue) : String.valueOf(intValue);
} else if (value instanceof DecimalType || value instanceof QuantityType<?>) { } else if (value instanceof DecimalType || value instanceof QuantityType<?>) {
return handleNumericTypes(dptId, mainNumber, dpt, value); return handleNumericTypes(dptId, mainNumber, dpt, value);
} else if (value instanceof StringType) { } else if (value instanceof StringType) {
return value.toString(); return value.toString();
} else if (value instanceof DateTimeType) { } else if (value instanceof DateTimeType type) {
return handleDateTimeType(dptId, (DateTimeType) value); return handleDateTimeType(dptId, type);
} }
} catch (KNXException e) { } catch (KNXException e) {
return null; return null;

View File

@ -366,9 +366,9 @@ public class DeviceThingHandler extends BaseThingHandler implements GroupAddress
if (oldFuture != null) { if (oldFuture != null) {
oldFuture.cancel(true); oldFuture.cancel(true);
} }
if (value instanceof IncreaseDecreaseType) { if (value instanceof IncreaseDecreaseType type) {
channelFutures.put(channelUID, scheduler.scheduleWithFixedDelay( channelFutures.put(channelUID, scheduler.scheduleWithFixedDelay(
() -> postCommand(channelUID, (Command) value), 0, frequency, TimeUnit.MILLISECONDS)); () -> postCommand(channelUID, type), 0, frequency, TimeUnit.MILLISECONDS));
} }
} else { } else {
if (value instanceof Command command) { if (value instanceof Command command) {