Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More instanceof pattern matching #4191

Merged
merged 5 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ private boolean checkType(Type type, Object configValue) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ public AnnotationActionHandler(Action module, ActionType mt, Method method, Obje
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public Object doNormalize(Object value) {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ private int getConfigParameter(Map<String, Object> parameters, String parameter,
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ public Response getItems(final @Context UriInfo uriInfo, final @Context HttpHead
.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);
}
}
Expand Down Expand Up @@ -329,8 +329,8 @@ public Response getItemByName(final @Context UriInfo uriInfo, final @Context Htt
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public GenericItemProvider(final @Reference ModelRepository modelRepository,
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,8 @@ public static boolean operator_greaterEqualsThan(QuantityType<?> x, Number y) {
* @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();
}
Expand All @@ -388,8 +387,8 @@ public static BigDecimal numberToBigDecimal(Number number) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public boolean apply(Item item) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public ChannelStateDescriptionProvider(final @Reference ItemChannelLinkRegistry
@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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ private void setWidgetIconPropertyFromComponentConfig(Widget widget, @Nullable U
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();
Expand All @@ -376,8 +376,8 @@ private void addWidgetMappings(EList<Mapping> mappings, UIComponent component) {
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());
Expand All @@ -402,8 +402,8 @@ private void addWidgetButtons(EList<Button> buttons, UIComponent component) {
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
Expand Down Expand Up @@ -432,8 +432,8 @@ private void addIconColor(EList<ColorArray> iconColor, UIComponent component) {
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);
Expand All @@ -451,8 +451,8 @@ private void addColor(EList<ColorArray> color, UIComponent component, String key
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ private List<State> parseStates(@Nullable Item baseItem, String @Nullable [] par
}

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public MetadataStateDescriptionFragmentProvider(final @Reference MetadataRegistr
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ private static int getHttpServicePortProperty(final BundleContext bc, final Stri
} 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;
}
}

Expand Down