mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
More instanceof pattern matching (#4191)
* More instanceof pattern matching Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
+1
-1
@@ -589,7 +589,7 @@ public class RuleRegistryImpl extends AbstractRegistry<Rule, String, RuleProvide
|
||||
return configValue instanceof Boolean;
|
||||
case INTEGER:
|
||||
return configValue instanceof BigDecimal || configValue instanceof Integer
|
||||
|| configValue instanceof Double && ((Double) configValue).intValue() == (Double) configValue;
|
||||
|| configValue instanceof Double doubleValue && doubleValue.intValue() == doubleValue;
|
||||
case DECIMAL:
|
||||
return configValue instanceof BigDecimal || configValue instanceof Double;
|
||||
}
|
||||
|
||||
+6
-6
@@ -105,16 +105,16 @@ public class AnnotationActionHandler extends BaseActionModuleHandler {
|
||||
method, moduleType.getUID(), ex.getMessage());
|
||||
}
|
||||
// we allow simple data types as return values and put them under the context key "result".
|
||||
} else if (result instanceof Boolean boolean1) {
|
||||
output.put(MODULE_RESULT, boolean1);
|
||||
} else if (result instanceof Boolean booleanValue) {
|
||||
output.put(MODULE_RESULT, booleanValue);
|
||||
} else if (result instanceof String) {
|
||||
output.put(MODULE_RESULT, result);
|
||||
} else if (result instanceof Integer) {
|
||||
output.put(MODULE_RESULT, result);
|
||||
} else if (result instanceof Double) {
|
||||
output.put(MODULE_RESULT, (double) result);
|
||||
} else if (result instanceof Float) {
|
||||
output.put(MODULE_RESULT, (float) result);
|
||||
} else if (result instanceof Double doubleValue) {
|
||||
output.put(MODULE_RESULT, doubleValue);
|
||||
} else if (result instanceof Float floatValue) {
|
||||
output.put(MODULE_RESULT, floatValue);
|
||||
} else {
|
||||
logger.warn("Non compatible return type '{}' on action method.", result.getClass());
|
||||
}
|
||||
|
||||
+6
-6
@@ -31,14 +31,14 @@ final class BooleanNormalizer extends AbstractNormalizer {
|
||||
if (value instanceof Boolean) {
|
||||
return value;
|
||||
}
|
||||
if (value instanceof Byte) {
|
||||
return handleNumeric(((Byte) value).longValue());
|
||||
if (value instanceof Byte byteValue) {
|
||||
return handleNumeric(byteValue.longValue());
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return handleNumeric(((Integer) value).longValue());
|
||||
if (value instanceof Integer integerValue) {
|
||||
return handleNumeric(integerValue.longValue());
|
||||
}
|
||||
if (value instanceof Long) {
|
||||
return handleNumeric((Long) value);
|
||||
if (value instanceof Long longValue) {
|
||||
return handleNumeric(longValue);
|
||||
}
|
||||
String s = value.toString();
|
||||
if ("true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s)
|
||||
|
||||
+14
-14
@@ -28,26 +28,26 @@ final class DecimalNormalizer extends AbstractNormalizer {
|
||||
@Override
|
||||
public Object doNormalize(Object value) {
|
||||
try {
|
||||
if (value instanceof BigDecimal) {
|
||||
return stripTrailingZeros((BigDecimal) value);
|
||||
if (value instanceof BigDecimal bigDecimalValue) {
|
||||
return stripTrailingZeros(bigDecimalValue);
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return stripTrailingZeros(new BigDecimal((String) value));
|
||||
if (value instanceof String stringValue) {
|
||||
return stripTrailingZeros(new BigDecimal(stringValue));
|
||||
}
|
||||
if (value instanceof Byte) {
|
||||
return new BigDecimal((Byte) value).setScale(1);
|
||||
if (value instanceof Byte byteValue) {
|
||||
return new BigDecimal(byteValue).setScale(1);
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return new BigDecimal((Integer) value).setScale(1);
|
||||
if (value instanceof Integer integerValue) {
|
||||
return new BigDecimal(integerValue).setScale(1);
|
||||
}
|
||||
if (value instanceof Long) {
|
||||
return new BigDecimal((Long) value).setScale(1);
|
||||
if (value instanceof Long longValue) {
|
||||
return new BigDecimal(longValue).setScale(1);
|
||||
}
|
||||
if (value instanceof Float) {
|
||||
return new BigDecimal(((Float) value).toString());
|
||||
if (value instanceof Float floatValue) {
|
||||
return new BigDecimal(floatValue.toString());
|
||||
}
|
||||
if (value instanceof Double) {
|
||||
return BigDecimal.valueOf((Double) value);
|
||||
if (value instanceof Double doubleValue) {
|
||||
return BigDecimal.valueOf(doubleValue);
|
||||
}
|
||||
} catch (ArithmeticException | NumberFormatException e) {
|
||||
logger.trace("\"{}\" is not a valid decimal number.", value, e);
|
||||
|
||||
+14
-14
@@ -29,26 +29,26 @@ final class IntNormalizer extends AbstractNormalizer {
|
||||
@Override
|
||||
public Object doNormalize(Object value) {
|
||||
try {
|
||||
if (value instanceof BigDecimal) {
|
||||
return ((BigDecimal) value).setScale(0, RoundingMode.UNNECESSARY);
|
||||
if (value instanceof BigDecimal bigDecimalValue) {
|
||||
return bigDecimalValue.setScale(0, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
if (value instanceof Byte) {
|
||||
return new BigDecimal((Byte) value);
|
||||
if (value instanceof Byte byteValue) {
|
||||
return new BigDecimal(byteValue);
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return new BigDecimal((Integer) value);
|
||||
if (value instanceof Integer integerValue) {
|
||||
return new BigDecimal(integerValue);
|
||||
}
|
||||
if (value instanceof Long) {
|
||||
return BigDecimal.valueOf((Long) value);
|
||||
if (value instanceof Long longValue) {
|
||||
return BigDecimal.valueOf(longValue);
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return new BigDecimal((String) value).setScale(0, RoundingMode.UNNECESSARY);
|
||||
if (value instanceof String stringValue) {
|
||||
return new BigDecimal(stringValue).setScale(0, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
if (value instanceof Float) {
|
||||
return new BigDecimal(((Float) value).toString()).setScale(0, RoundingMode.UNNECESSARY);
|
||||
if (value instanceof Float floatValue) {
|
||||
return new BigDecimal(floatValue.toString()).setScale(0, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
if (value instanceof Double) {
|
||||
return BigDecimal.valueOf((Double) value).setScale(0, RoundingMode.UNNECESSARY);
|
||||
if (value instanceof Double doubleValue) {
|
||||
return BigDecimal.valueOf(doubleValue).setScale(0, RoundingMode.UNNECESSARY);
|
||||
}
|
||||
} catch (ArithmeticException | NumberFormatException e) {
|
||||
logger.trace("\"{}\" is not a valid integer number.", value, e);
|
||||
|
||||
+2
-2
@@ -195,8 +195,8 @@ public class WebClientFactoryImpl implements HttpClientFactory, WebSocketFactory
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return (Integer) value;
|
||||
if (value instanceof Integer integerValue) {
|
||||
return integerValue;
|
||||
}
|
||||
if (value instanceof String string) {
|
||||
try {
|
||||
|
||||
+4
-4
@@ -271,8 +271,8 @@ public class ItemResource implements RESTResource {
|
||||
.peek(dto -> addMetadata(dto, namespaces, null)) //
|
||||
.peek(dto -> dto.editable = isEditable(dto.name)) //
|
||||
.peek(dto -> {
|
||||
if (dto instanceof EnrichedGroupItemDTO) {
|
||||
for (EnrichedItemDTO member : ((EnrichedGroupItemDTO) dto).members) {
|
||||
if (dto instanceof EnrichedGroupItemDTO enrichedGroupItemDTO) {
|
||||
for (EnrichedItemDTO member : enrichedGroupItemDTO.members) {
|
||||
member.editable = isEditable(member.name);
|
||||
}
|
||||
}
|
||||
@@ -329,8 +329,8 @@ public class ItemResource implements RESTResource {
|
||||
locale);
|
||||
addMetadata(dto, namespaces, null);
|
||||
dto.editable = isEditable(dto.name);
|
||||
if (dto instanceof EnrichedGroupItemDTO) {
|
||||
for (EnrichedItemDTO member : ((EnrichedGroupItemDTO) dto).members) {
|
||||
if (dto instanceof EnrichedGroupItemDTO enrichedGroupItemDTO) {
|
||||
for (EnrichedItemDTO member : enrichedGroupItemDTO.members) {
|
||||
member.editable = isEditable(member.name);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -97,8 +97,8 @@ public class GenericItemProvider extends AbstractProvider<Item>
|
||||
this.genericMetaDataProvider = genericMetadataProvider;
|
||||
|
||||
Object serviceRanking = properties.get(Constants.SERVICE_RANKING);
|
||||
if (serviceRanking instanceof Integer) {
|
||||
rank = (Integer) serviceRanking;
|
||||
if (serviceRanking instanceof Integer integerValue) {
|
||||
rank = integerValue;
|
||||
} else {
|
||||
rank = 0;
|
||||
}
|
||||
|
||||
+4
-5
@@ -372,9 +372,8 @@ public class NumberExtensions {
|
||||
* @return the given number as BigDecimal or null if number is null
|
||||
*/
|
||||
public static BigDecimal numberToBigDecimal(Number number) {
|
||||
if (number instanceof QuantityType) {
|
||||
QuantityType<?> state = ((QuantityType<?>) number)
|
||||
.toInvertibleUnit(((QuantityType<?>) number).getUnit().getSystemUnit());
|
||||
if (number instanceof QuantityType quantity) {
|
||||
QuantityType<?> state = quantity.toInvertibleUnit(quantity.getUnit().getSystemUnit());
|
||||
if (state != null) {
|
||||
return state.toBigDecimal();
|
||||
}
|
||||
@@ -388,8 +387,8 @@ public class NumberExtensions {
|
||||
}
|
||||
|
||||
private static boolean oneIsQuantity(Number left, Number right) {
|
||||
return (left instanceof QuantityType && !isAbstractUnitOne((QuantityType<?>) left))
|
||||
|| (right instanceof QuantityType && !isAbstractUnitOne((QuantityType<?>) right));
|
||||
return (left instanceof QuantityType leftQuantity && !isAbstractUnitOne(leftQuantity))
|
||||
|| (right instanceof QuantityType rightQuantity && !isAbstractUnitOne(rightQuantity));
|
||||
}
|
||||
|
||||
private static boolean isAbstractUnitOne(QuantityType<?> left) {
|
||||
|
||||
+2
-2
@@ -83,9 +83,9 @@ public class PersistenceThresholdFilter extends PersistenceFilter {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (state instanceof DecimalType) {
|
||||
if (state instanceof DecimalType decimalState) {
|
||||
BigDecimal oldState = ((DecimalType) cachedState).toBigDecimal();
|
||||
BigDecimal delta = oldState.subtract(((DecimalType) state).toBigDecimal());
|
||||
BigDecimal delta = oldState.subtract(decimalState.toBigDecimal());
|
||||
if (relative && !BigDecimal.ZERO.equals(oldState)) {
|
||||
delta = delta.multiply(HUNDRED).divide(oldState, 2, RoundingMode.HALF_UP);
|
||||
}
|
||||
|
||||
+2
-2
@@ -71,8 +71,8 @@ public class ChannelStateDescriptionProvider implements StateDescriptionFragment
|
||||
@Activate
|
||||
protected void activate(Map<String, Object> properties) {
|
||||
Object serviceRanking = properties.get(Constants.SERVICE_RANKING);
|
||||
if (serviceRanking instanceof Integer) {
|
||||
rank = (Integer) serviceRanking;
|
||||
if (serviceRanking instanceof Integer integerValue) {
|
||||
rank = integerValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-10
@@ -355,8 +355,8 @@ public class UIComponentSitemapProvider implements SitemapProvider, RegistryChan
|
||||
private void addWidgetMappings(EList<Mapping> mappings, UIComponent component) {
|
||||
if (component.getConfig() != null && component.getConfig().containsKey("mappings")) {
|
||||
Object sourceMappings = component.getConfig().get("mappings");
|
||||
if (sourceMappings instanceof Collection<?>) {
|
||||
for (Object sourceMapping : (Collection<?>) sourceMappings) {
|
||||
if (sourceMappings instanceof Collection<?> sourceMappingsCollection) {
|
||||
for (Object sourceMapping : sourceMappingsCollection) {
|
||||
if (sourceMapping instanceof String) {
|
||||
String[] splitMapping = sourceMapping.toString().split("=");
|
||||
String cmd = splitMapping[0].trim();
|
||||
@@ -376,8 +376,8 @@ public class UIComponentSitemapProvider implements SitemapProvider, RegistryChan
|
||||
private void addWidgetButtons(EList<Button> buttons, UIComponent component) {
|
||||
if (component.getConfig() != null && component.getConfig().containsKey("buttons")) {
|
||||
Object sourceButtons = component.getConfig().get("buttons");
|
||||
if (sourceButtons instanceof Collection<?>) {
|
||||
for (Object sourceButton : (Collection<?>) sourceButtons) {
|
||||
if (sourceButtons instanceof Collection<?> sourceButtonsCollection) {
|
||||
for (Object sourceButton : sourceButtonsCollection) {
|
||||
if (sourceButton instanceof String) {
|
||||
String[] splitted1 = sourceButton.toString().split(":", 3);
|
||||
int row = Integer.parseInt(splitted1[0].trim());
|
||||
@@ -402,8 +402,8 @@ public class UIComponentSitemapProvider implements SitemapProvider, RegistryChan
|
||||
private void addWidgetVisibility(EList<VisibilityRule> visibility, UIComponent component) {
|
||||
if (component.getConfig() != null && component.getConfig().containsKey("visibility")) {
|
||||
Object sourceVisibilities = component.getConfig().get("visibility");
|
||||
if (sourceVisibilities instanceof Collection<?>) {
|
||||
for (Object sourceVisibility : (Collection<?>) sourceVisibilities) {
|
||||
if (sourceVisibilities instanceof Collection<?> sourceVisibilitiesCollection) {
|
||||
for (Object sourceVisibility : sourceVisibilitiesCollection) {
|
||||
if (sourceVisibility instanceof String) {
|
||||
List<String> conditionsString = getRuleConditions(sourceVisibility.toString(), null);
|
||||
VisibilityRuleImpl visibilityRule = (VisibilityRuleImpl) SitemapFactory.eINSTANCE
|
||||
@@ -432,8 +432,8 @@ public class UIComponentSitemapProvider implements SitemapProvider, RegistryChan
|
||||
private void addColor(EList<ColorArray> color, UIComponent component, String key) {
|
||||
if (component.getConfig() != null && component.getConfig().containsKey(key)) {
|
||||
Object sourceColors = component.getConfig().get(key);
|
||||
if (sourceColors instanceof Collection<?>) {
|
||||
for (Object sourceColor : (Collection<?>) sourceColors) {
|
||||
if (sourceColors instanceof Collection<?> sourceColorsCollection) {
|
||||
for (Object sourceColor : sourceColorsCollection) {
|
||||
if (sourceColor instanceof String) {
|
||||
String argument = getRuleArgument(sourceColor.toString());
|
||||
List<String> conditionsString = getRuleConditions(sourceColor.toString(), argument);
|
||||
@@ -451,8 +451,8 @@ public class UIComponentSitemapProvider implements SitemapProvider, RegistryChan
|
||||
private void addIconRules(EList<IconRule> icon, UIComponent component) {
|
||||
if (component.getConfig() != null && component.getConfig().containsKey("iconrules")) {
|
||||
Object sourceIcons = component.getConfig().get("iconrules");
|
||||
if (sourceIcons instanceof Collection<?>) {
|
||||
for (Object sourceIcon : (Collection<?>) sourceIcons) {
|
||||
if (sourceIcons instanceof Collection<?> sourceIconsCollection) {
|
||||
for (Object sourceIcon : sourceIconsCollection) {
|
||||
if (sourceIcon instanceof String) {
|
||||
String argument = getRuleArgument(sourceIcon.toString());
|
||||
List<String> conditionsString = getRuleConditions(sourceIcon.toString(), argument);
|
||||
|
||||
+2
-2
@@ -79,8 +79,8 @@ public class GroupFunctionHelper {
|
||||
}
|
||||
|
||||
private @Nullable Class<? extends Quantity<?>> getDimension(@Nullable Item baseItem) {
|
||||
if (baseItem instanceof NumberItem) {
|
||||
return ((NumberItem) baseItem).getDimension();
|
||||
if (baseItem instanceof NumberItem numberItem) {
|
||||
return numberItem.getDimension();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+2
-2
@@ -61,8 +61,8 @@ public class MetadataStateDescriptionFragmentProvider implements StateDescriptio
|
||||
this.metadataRegistry = metadataRegistry;
|
||||
|
||||
Object serviceRanking = properties.get(Constants.SERVICE_RANKING);
|
||||
if (serviceRanking instanceof Integer) {
|
||||
rank = (Integer) serviceRanking;
|
||||
if (serviceRanking instanceof Integer rankValue) {
|
||||
rank = rankValue;
|
||||
} else {
|
||||
rank = 1; // takes precedence over other providers usually ranked 0
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ public class HttpServiceUtil {
|
||||
} catch (final NumberFormatException ex) {
|
||||
// If the property could not be parsed, the HTTP servlet itself has to care and warn about.
|
||||
}
|
||||
} else if (value instanceof Integer) {
|
||||
return (Integer) value;
|
||||
} else if (value instanceof Integer integerValue) {
|
||||
return integerValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user