Skip to content

Commit 01892c6

Browse files
shevtsivrstoyanchev
authored andcommitted
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
1 parent 9776929 commit 01892c6

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
import java.beans.PropertyEditorSupport;
2020
import java.io.IOException;
21-
import java.util.ArrayList;
2221
import java.util.Arrays;
2322
import java.util.Collection;
24-
import java.util.List;
23+
import java.util.HashSet;
24+
import java.util.Set;
2525

2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
@@ -129,19 +129,15 @@ public void setAsText(String text) {
129129
public void setValue(Object value) throws IllegalArgumentException {
130130
if (value instanceof Collection || (value instanceof Object[] && !(value instanceof Resource[]))) {
131131
Collection<?> input = (value instanceof Collection ? (Collection<?>) value : Arrays.asList((Object[]) value));
132-
List<Resource> merged = new ArrayList<>();
132+
Set<Resource> merged = new HashSet<>(input.size());
133133
for (Object element : input) {
134134
if (element instanceof String) {
135135
// A location pattern: resolve it into a Resource array.
136136
// Might point to a single resource or to multiple resources.
137137
String pattern = resolvePath((String) element).trim();
138138
try {
139139
Resource[] resources = this.resourcePatternResolver.getResources(pattern);
140-
for (Resource resource : resources) {
141-
if (!merged.contains(resource)) {
142-
merged.add(resource);
143-
}
144-
}
140+
merged.addAll(Arrays.asList(resources));
145141
}
146142
catch (IOException ex) {
147143
// ignore - might be an unresolved placeholder or non-existing base directory
@@ -152,10 +148,7 @@ public void setValue(Object value) throws IllegalArgumentException {
152148
}
153149
else if (element instanceof Resource) {
154150
// A Resource object: add it to the result.
155-
Resource resource = (Resource) element;
156-
if (!merged.contains(resource)) {
157-
merged.add(resource);
158-
}
151+
merged.add((Resource) element);
159152
}
160153
else {
161154
throw new IllegalArgumentException("Cannot convert element [" + element + "] to [" +

0 commit comments

Comments
 (0)