Java 17 features (H-M) (#15520)
- 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 - remove null check before instanceof Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
@@ -79,8 +79,7 @@ public class SubscribeFieldToMQTTtopic implements MqttMessageSubscriber {
|
||||
// Handle the conversion case of BigDecimal to Float,Double,Long,Integer and the respective
|
||||
// primitive types
|
||||
String typeName = type.getSimpleName();
|
||||
if (value instanceof BigDecimal && !type.equals(BigDecimal.class)) {
|
||||
BigDecimal bdValue = (BigDecimal) value;
|
||||
if (value instanceof BigDecimal bdValue && !type.equals(BigDecimal.class)) {
|
||||
if (type.equals(Float.class) || "float".equals(typeName)) {
|
||||
result = bdValue.floatValue();
|
||||
} else if (type.equals(Double.class) || "double".equals(typeName)) {
|
||||
@@ -93,8 +92,7 @@ public class SubscribeFieldToMQTTtopic implements MqttMessageSubscriber {
|
||||
} else
|
||||
// Handle the conversion case of String to Float,Double,Long,Integer,BigDecimal and the respective
|
||||
// primitive types
|
||||
if (value instanceof String && !type.equals(String.class)) {
|
||||
String bdValue = (String) value;
|
||||
if (value instanceof String bdValue && !type.equals(String.class)) {
|
||||
if (type.equals(Float.class) || "float".equals(typeName)) {
|
||||
result = Float.valueOf(bdValue);
|
||||
} else if (type.equals(Double.class) || "double".equals(typeName)) {
|
||||
|
||||
@@ -83,15 +83,14 @@ public class ColorValue extends Value {
|
||||
@Override
|
||||
public HSBType parseCommand(Command command) throws IllegalArgumentException {
|
||||
HSBType oldvalue = (state == UnDefType.UNDEF) ? new HSBType() : (HSBType) state;
|
||||
if (command instanceof HSBType) {
|
||||
return (HSBType) command;
|
||||
} else if (command instanceof OnOffType) {
|
||||
OnOffType boolValue = ((OnOffType) command);
|
||||
if (command instanceof HSBType hsbCommand) {
|
||||
return hsbCommand;
|
||||
} else if (command instanceof OnOffType onOffCommand) {
|
||||
PercentType minOn = new PercentType(Math.max(oldvalue.getBrightness().intValue(), onBrightness));
|
||||
return new HSBType(oldvalue.getHue(), oldvalue.getSaturation(),
|
||||
boolValue == OnOffType.ON ? minOn : new PercentType(0));
|
||||
} else if (command instanceof PercentType) {
|
||||
return new HSBType(oldvalue.getHue(), oldvalue.getSaturation(), (PercentType) command);
|
||||
onOffCommand == OnOffType.ON ? minOn : new PercentType(0));
|
||||
} else if (command instanceof PercentType percentCommand) {
|
||||
return new HSBType(oldvalue.getHue(), oldvalue.getSaturation(), percentCommand);
|
||||
} else {
|
||||
final String updatedValue = command.toString();
|
||||
if (onValue.equals(updatedValue)) {
|
||||
|
||||
@@ -35,8 +35,8 @@ public class DateTimeValue extends Value {
|
||||
|
||||
@Override
|
||||
public DateTimeType parseCommand(Command command) throws IllegalArgumentException {
|
||||
if (command instanceof DateTimeType) {
|
||||
return ((DateTimeType) command);
|
||||
if (command instanceof DateTimeType dateTimeCommand) {
|
||||
return dateTimeCommand;
|
||||
} else {
|
||||
return DateTimeType.valueOf(command.toString());
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ public class LocationValue extends Value {
|
||||
|
||||
@Override
|
||||
public PointType parseCommand(Command command) throws IllegalArgumentException {
|
||||
if (command instanceof PointType) {
|
||||
return ((PointType) command);
|
||||
if (command instanceof PointType point) {
|
||||
return point;
|
||||
} else {
|
||||
return PointType.valueOf(command.toString());
|
||||
}
|
||||
|
||||
@@ -86,8 +86,8 @@ public class NumberValue extends Value {
|
||||
@Override
|
||||
public Command parseCommand(Command command) throws IllegalArgumentException {
|
||||
BigDecimal newValue = null;
|
||||
if (command instanceof DecimalType) {
|
||||
newValue = ((DecimalType) command).toBigDecimal();
|
||||
if (command instanceof DecimalType decimalCommand) {
|
||||
newValue = decimalCommand.toBigDecimal();
|
||||
} else if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
|
||||
BigDecimal oldValue = getOldValue();
|
||||
if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
|
||||
@@ -95,8 +95,8 @@ public class NumberValue extends Value {
|
||||
} else {
|
||||
newValue = oldValue.subtract(step);
|
||||
}
|
||||
} else if (command instanceof QuantityType<?>) {
|
||||
newValue = getQuantityTypeAsDecimal((QuantityType<?>) command);
|
||||
} else if (command instanceof QuantityType<?> quantityCommand) {
|
||||
newValue = getQuantityTypeAsDecimal(quantityCommand);
|
||||
} else {
|
||||
newValue = new BigDecimal(command.toString());
|
||||
}
|
||||
@@ -114,10 +114,10 @@ public class NumberValue extends Value {
|
||||
|
||||
private BigDecimal getOldValue() {
|
||||
BigDecimal val = BigDecimal.ZERO;
|
||||
if (state instanceof DecimalType) {
|
||||
val = ((DecimalType) state).toBigDecimal();
|
||||
} else if (state instanceof QuantityType<?>) {
|
||||
val = ((QuantityType<?>) state).toBigDecimal();
|
||||
if (state instanceof DecimalType decimalCommand) {
|
||||
val = decimalCommand.toBigDecimal();
|
||||
} else if (state instanceof QuantityType<?> quantityCommand) {
|
||||
val = quantityCommand.toBigDecimal();
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
@@ -73,8 +73,8 @@ public class OnOffValue extends Value {
|
||||
|
||||
@Override
|
||||
public OnOffType parseCommand(Command command) throws IllegalArgumentException {
|
||||
if (command instanceof OnOffType) {
|
||||
return (OnOffType) command;
|
||||
if (command instanceof OnOffType onOffCommand) {
|
||||
return onOffCommand;
|
||||
} else {
|
||||
final String updatedValue = command.toString();
|
||||
if (onState.equals(updatedValue)) {
|
||||
|
||||
@@ -54,8 +54,8 @@ public class OpenCloseValue extends Value {
|
||||
|
||||
@Override
|
||||
public OpenClosedType parseCommand(Command command) throws IllegalArgumentException {
|
||||
if (command instanceof OpenClosedType) {
|
||||
return (OpenClosedType) command;
|
||||
if (command instanceof OpenClosedType openClosed) {
|
||||
return openClosed;
|
||||
} else {
|
||||
final String updatedValue = command.toString();
|
||||
if (openString.equals(updatedValue)) {
|
||||
|
||||
@@ -75,18 +75,18 @@ public class PercentageValue extends Value {
|
||||
public PercentType parseCommand(Command command) throws IllegalArgumentException {
|
||||
PercentType oldvalue = (state == UnDefType.UNDEF) ? new PercentType() : (PercentType) state;
|
||||
// Nothing do to -> We have received a percentage
|
||||
if (command instanceof PercentType) {
|
||||
return (PercentType) command;
|
||||
if (command instanceof PercentType percent) {
|
||||
return percent;
|
||||
} else //
|
||||
// A decimal type need to be converted according to the current min/max values
|
||||
if (command instanceof DecimalType) {
|
||||
BigDecimal v = ((DecimalType) command).toBigDecimal();
|
||||
if (command instanceof DecimalType decimal) {
|
||||
BigDecimal v = decimal.toBigDecimal();
|
||||
v = v.subtract(min).multiply(HUNDRED).divide(max.subtract(min), MathContext.DECIMAL128);
|
||||
return new PercentType(v);
|
||||
} else //
|
||||
// A quantity type need to be converted according to the current min/max values
|
||||
if (command instanceof QuantityType) {
|
||||
QuantityType<?> qty = ((QuantityType<?>) command).toUnit(Units.PERCENT);
|
||||
if (command instanceof QuantityType quantity) {
|
||||
QuantityType<?> qty = quantity.toUnit(Units.PERCENT);
|
||||
if (qty != null) {
|
||||
BigDecimal v = qty.toBigDecimal();
|
||||
v = v.subtract(min).multiply(HUNDRED).divide(max.subtract(min), MathContext.DECIMAL128);
|
||||
@@ -95,8 +95,8 @@ public class PercentageValue extends Value {
|
||||
return oldvalue;
|
||||
} else //
|
||||
// Increase or decrease by "step"
|
||||
if (command instanceof IncreaseDecreaseType) {
|
||||
if (((IncreaseDecreaseType) command) == IncreaseDecreaseType.INCREASE) {
|
||||
if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
|
||||
if (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE) {
|
||||
final BigDecimal v = oldvalue.toBigDecimal().add(stepPercent);
|
||||
return v.compareTo(HUNDRED) <= 0 ? new PercentType(v) : PercentType.HUNDRED;
|
||||
} else {
|
||||
@@ -105,12 +105,12 @@ public class PercentageValue extends Value {
|
||||
}
|
||||
} else //
|
||||
// On/Off equals 100 or 0 percent
|
||||
if (command instanceof OnOffType) {
|
||||
return ((OnOffType) command) == OnOffType.ON ? PercentType.HUNDRED : PercentType.ZERO;
|
||||
if (command instanceof OnOffType increaseDecreaseCommand) {
|
||||
return increaseDecreaseCommand == OnOffType.ON ? PercentType.HUNDRED : PercentType.ZERO;
|
||||
} else//
|
||||
// Increase or decrease by "step"
|
||||
if (command instanceof UpDownType) {
|
||||
if (((UpDownType) command) == UpDownType.UP) {
|
||||
if (command instanceof UpDownType upDownCommand) {
|
||||
if (upDownCommand == UpDownType.UP) {
|
||||
final BigDecimal v = oldvalue.toBigDecimal().add(stepPercent);
|
||||
return v.compareTo(HUNDRED) <= 0 ? new PercentType(v) : PercentType.HUNDRED;
|
||||
} else {
|
||||
|
||||
@@ -77,8 +77,8 @@ public class RollershutterValue extends Value {
|
||||
return PercentType.HUNDRED;
|
||||
}
|
||||
}
|
||||
} else if (command instanceof PercentType) {
|
||||
return (PercentType) command;
|
||||
} else if (command instanceof PercentType percentage) {
|
||||
return percentage;
|
||||
} else if (command instanceof StringType) {
|
||||
final String updatedValue = command.toString();
|
||||
if (updatedValue.equals(upString)) {
|
||||
@@ -115,13 +115,13 @@ public class RollershutterValue extends Value {
|
||||
} else {
|
||||
return ((StopMoveType) command).name();
|
||||
}
|
||||
} else if (command instanceof PercentType) {
|
||||
} else if (command instanceof PercentType percentage) {
|
||||
if (command.equals(PercentType.HUNDRED) && downString != null) {
|
||||
return downString;
|
||||
} else if (command.equals(PercentType.ZERO) && upString != null) {
|
||||
return upString;
|
||||
} else {
|
||||
return String.valueOf(((PercentType) command).intValue());
|
||||
return String.valueOf(percentage.intValue());
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid command type for Rollershutter item");
|
||||
|
||||
@@ -155,7 +155,7 @@ public abstract class Value {
|
||||
* @param data The binary payload to update the internal value.
|
||||
* @exception IllegalArgumentException Thrown if for example a text is assigned to a number type.
|
||||
*/
|
||||
public void update(byte data[]) throws IllegalArgumentException {
|
||||
public void update(byte[] data) throws IllegalArgumentException {
|
||||
String mimeType = null;
|
||||
|
||||
// URLConnection.guessContentTypeFromStream(input) is not sufficient to detect all JPEG files
|
||||
|
||||
@@ -112,7 +112,7 @@ public class ChannelStateTransformationTests {
|
||||
|
||||
ChannelStateTransformation transformation = channelConfig.transformationsIn.get(0);
|
||||
|
||||
byte payload[] = JSON_PATH_JSON.getBytes();
|
||||
byte[] payload = JSON_PATH_JSON.getBytes();
|
||||
assertThat(transformation.pattern, is(JSON_PATH_PATTERN));
|
||||
// Test process message
|
||||
channelConfig.processMessage(channelConfig.getStateTopic(), payload);
|
||||
|
||||
@@ -185,7 +185,7 @@ public class GenericThingHandlerTests {
|
||||
textValue, thingHandler));
|
||||
doReturn(channelConfig).when(thingHandler).createChannelState(any(), any(), any());
|
||||
thingHandler.initialize();
|
||||
byte payload[] = "UPDATE".getBytes();
|
||||
byte[] payload = "UPDATE".getBytes();
|
||||
// Test process message
|
||||
channelConfig.processMessage("test/state", payload);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user