Apply "instanceof pattern matching"

This commit is contained in:
Sam Brannen
2022-05-30 13:29:33 +02:00
parent b629487b44
commit ad46f0295c
6 changed files with 45 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -62,11 +62,10 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter {
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (this.conversionService instanceof GenericConversionService) {
if (this.conversionService instanceof GenericConversionService genericConversionService) {
TypeDescriptor targetElement = targetType.getElementTypeDescriptor();
if (targetElement != null &&
((GenericConversionService) this.conversionService).canBypassConvert(
sourceType.getElementTypeDescriptor(), targetElement)) {
if (targetElement != null && genericConversionService.canBypassConvert(
sourceType.getElementTypeDescriptor(), targetElement)) {
return source;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -80,11 +80,11 @@ public class CompositePropertySource extends EnumerablePropertySource<Object> {
public String[] getPropertyNames() {
Set<String> names = new LinkedHashSet<>();
for (PropertySource<?> propertySource : this.propertySources) {
if (!(propertySource instanceof EnumerablePropertySource)) {
if (!(propertySource instanceof EnumerablePropertySource<?> enumerablePropertySource)) {
throw new IllegalStateException(
"Failed to enumerate property names due to non-enumerable property source: " + propertySource);
}
names.addAll(Arrays.asList(((EnumerablePropertySource<?>) propertySource).getPropertyNames()));
names.addAll(Arrays.asList(enumerablePropertySource.getPropertyNames()));
}
return StringUtils.toStringArray(names);
}

View File

@@ -124,18 +124,18 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
/**
* Treat the given value as a collection or array and convert it to a Resource array.
* Considers String elements as location patterns and takes Resource elements as-is.
* <p>Considers String elements as location patterns and takes Resource elements as-is.
*/
@Override
public void setValue(Object value) throws IllegalArgumentException {
if (value instanceof Collection || (value instanceof Object[] && !(value instanceof Resource[]))) {
Collection<?> input = (value instanceof Collection ? (Collection<?>) value : Arrays.asList((Object[]) value));
Collection<?> input = (value instanceof Collection<?> collection ? collection : Arrays.asList((Object[]) value));
Set<Resource> merged = new LinkedHashSet<>();
for (Object element : input) {
if (element instanceof String) {
if (element instanceof String path) {
// A location pattern: resolve it into a Resource array.
// Might point to a single resource or to multiple resources.
String pattern = resolvePath((String) element).trim();
String pattern = resolvePath(path.trim());
try {
Resource[] resources = this.resourcePatternResolver.getResources(pattern);
Collections.addAll(merged, resources);
@@ -147,9 +147,9 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
}
}
}
else if (element instanceof Resource) {
else if (element instanceof Resource resource) {
// A Resource object: add it to the result.
merged.add((Resource) element);
merged.add(resource);
}
else {
throw new IllegalArgumentException("Cannot convert element [" + element + "] to [" +

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,8 +65,8 @@ public abstract class DomUtils {
List<Element> childEles = new ArrayList<>();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element && nodeNameMatch(node, childEleNameList)) {
childEles.add((Element) node);
if (node instanceof Element element && nodeNameMatch(node, childEleNameList)) {
childEles.add(element);
}
}
return childEles;
@@ -99,8 +99,8 @@ public abstract class DomUtils {
NodeList nl = ele.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element && nodeNameMatch(node, childEleName)) {
return (Element) node;
if (node instanceof Element element && nodeNameMatch(node, childEleName)) {
return element;
}
}
return null;
@@ -129,8 +129,8 @@ public abstract class DomUtils {
List<Element> childEles = new ArrayList<>();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
childEles.add((Element) node);
if (node instanceof Element element) {
childEles.add(element);
}
}
return childEles;