From 01892c6524c7d506b5531419233a5d29c6d84dff Mon Sep 17 00:00:00 2001 From: shevtsiv Date: Fri, 16 Oct 2020 23:56:33 +0300 Subject: [PATCH] Optimization in ResourceArrayPropertyEditor The previous implementation uses ArrayList for storing resolved resources and ArrayList has O(n) time complexity for the contains operation. By switching to the HashSet for storing resolved resources we improve the time complexity of this operation to be O(1). See gh-25927 --- .../io/support/ResourceArrayPropertyEditor.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java b/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java index b7680648dc..75e6754bee 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java @@ -18,10 +18,10 @@ package org.springframework.core.io.support; import java.beans.PropertyEditorSupport; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -129,7 +129,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport { 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)); - List merged = new ArrayList<>(); + Set merged = new HashSet<>(input.size()); for (Object element : input) { if (element instanceof String) { // A location pattern: resolve it into a Resource array. @@ -137,11 +137,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport { String pattern = resolvePath((String) element).trim(); try { Resource[] resources = this.resourcePatternResolver.getResources(pattern); - for (Resource resource : resources) { - if (!merged.contains(resource)) { - merged.add(resource); - } - } + merged.addAll(Arrays.asList(resources)); } catch (IOException ex) { // ignore - might be an unresolved placeholder or non-existing base directory @@ -152,10 +148,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport { } else if (element instanceof Resource) { // A Resource object: add it to the result. - Resource resource = (Resource) element; - if (!merged.contains(resource)) { - merged.add(resource); - } + merged.add((Resource) element); } else { throw new IllegalArgumentException("Cannot convert element [" + element + "] to [" +