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