Skip to content

Commit d2c7dfb

Browse files
committed
Add convenience factory method for Managed[List|Set|Map]
Closes gh-28026
1 parent 7139a87 commit d2c7dfb

File tree

9 files changed

+92
-74
lines changed

9 files changed

+92
-74
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedList.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.beans.factory.support;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.List;
2122

2223
import org.springframework.beans.BeanMetadataElement;
@@ -53,6 +54,20 @@ public ManagedList(int initialCapacity) {
5354
}
5455

5556

57+
/**
58+
* Return a new instance containing an arbitrary number of elements.
59+
* @param elements the elements to be contained in the list
60+
* @param <E> the {@code List}'s element type
61+
* @return a {@code List} containing the specified elements
62+
* @since 5.3.16
63+
*/
64+
@SuppressWarnings("unchecked")
65+
public static <E> ManagedList<E> of(E... elements) {
66+
ManagedList<E> list = new ManagedList<>();
67+
list.addAll(Arrays.asList(elements));
68+
return list;
69+
}
70+
5671
/**
5772
* Set the configuration source {@code Object} for this metadata element.
5873
* <p>The exact type of the object will depend on the configuration mechanism used.

spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedMap.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
21+
import java.util.Map.Entry;
2122

2223
import org.springframework.beans.BeanMetadataElement;
2324
import org.springframework.beans.Mergeable;
@@ -56,6 +57,25 @@ public ManagedMap(int initialCapacity) {
5657
}
5758

5859

60+
/**
61+
* Return a new instance containing keys and values extracted from the
62+
* given entries. The entries themselves are not stored in the map.
63+
* @param entries {@code Map.Entry}s containing the keys and values
64+
* from which the map is populated
65+
* @param <K> the {@code Map}'s key type
66+
* @param <V> the {@code Map}'s value type
67+
* @return a {@code Map} containing the specified mappings
68+
* @since 5.3.16
69+
*/
70+
@SuppressWarnings("unchecked")
71+
public static <K,V> ManagedMap<K,V> ofEntries(Entry<? extends K, ? extends V>... entries) {
72+
ManagedMap<K,V > map = new ManagedMap<>();
73+
for (Entry<? extends K, ? extends V> entry : entries) {
74+
map.put(entry.getKey(), entry.getValue());
75+
}
76+
return map;
77+
}
78+
5979
/**
6080
* Set the configuration source {@code Object} for this metadata element.
6181
* <p>The exact type of the object will depend on the configuration mechanism used.

spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedSet.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.beans.factory.support;
1818

19+
import java.util.Arrays;
1920
import java.util.LinkedHashSet;
2021
import java.util.Set;
2122

@@ -52,6 +53,20 @@ public ManagedSet(int initialCapacity) {
5253
}
5354

5455

56+
/**
57+
* Return a new instance containing an arbitrary number of elements.
58+
* @param elements the elements to be contained in the set
59+
* @param <E> the {@code Set}'s element type
60+
* @return a {@code Set} containing the specified elements
61+
* @since 5.3.16
62+
*/
63+
@SuppressWarnings("unchecked")
64+
public static <E> ManagedSet<E> of(E... elements) {
65+
ManagedSet<E> set = new ManagedSet<>();
66+
set.addAll(Arrays.asList(elements));
67+
return set;
68+
}
69+
5570
/**
5671
* Set the configuration source {@code Object} for this metadata element.
5772
* <p>The exact type of the object will depend on the configuration mechanism used.

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,9 +2298,7 @@ void prototypeStringCreatedRepeatedly() {
22982298

22992299
@Test
23002300
void prototypeWithArrayConversionForConstructor() {
2301-
List<String> list = new ManagedList<>();
2302-
list.add("myName");
2303-
list.add("myBeanName");
2301+
List<String> list = ManagedList.of("myName", "myBeanName");
23042302
RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
23052303
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
23062304
bd.getConstructorArgumentValues().addGenericArgumentValue(list);
@@ -2316,9 +2314,7 @@ void prototypeWithArrayConversionForConstructor() {
23162314

23172315
@Test
23182316
void prototypeWithArrayConversionForFactoryMethod() {
2319-
List<String> list = new ManagedList<>();
2320-
list.add("myName");
2321-
list.add("myBeanName");
2317+
List<String> list = ManagedList.of("myName", "myBeanName");
23222318
RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
23232319
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
23242320
bd.setFactoryMethodName("create");

spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.beans.factory.config;
1818

19+
import java.util.AbstractMap.SimpleEntry;
1920
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.List;
@@ -357,22 +358,18 @@ private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation)
357358
MutablePropertyValues pvs = new MutablePropertyValues();
358359
pvs.add("stringArray", new String[] {"${os.name}", "${age}"});
359360

360-
List<Object> friends = new ManagedList<>();
361-
friends.add("na${age}me");
362-
friends.add(new RuntimeBeanReference("${ref}"));
361+
List<Object> friends = ManagedList.of("na${age}me", new RuntimeBeanReference("${ref}"));
363362
pvs.add("friends", friends);
364363

365-
Set<Object> someSet = new ManagedSet<>();
366-
someSet.add("na${age}me");
367-
someSet.add(new RuntimeBeanReference("${ref}"));
368-
someSet.add(new TypedStringValue("${age}", Integer.class));
364+
Set<Object> someSet = ManagedSet.of("na${age}me",
365+
new RuntimeBeanReference("${ref}"), new TypedStringValue("${age}", Integer.class));
369366
pvs.add("someSet", someSet);
370367

371-
Map<Object, Object> someMap = new ManagedMap<>();
372-
someMap.put(new TypedStringValue("key${age}"), new TypedStringValue("${age}"));
373-
someMap.put(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}"));
374-
someMap.put("key1", new RuntimeBeanReference("${ref}"));
375-
someMap.put("key2", "${age}name");
368+
Map<Object, Object> someMap = ManagedMap.ofEntries(
369+
new SimpleEntry<>(new TypedStringValue("key${age}"), new TypedStringValue("${age}")),
370+
new SimpleEntry<>(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}")),
371+
new SimpleEntry<>("key1", new RuntimeBeanReference("${ref}")),
372+
new SimpleEntry<>("key2", "${age}name"));
376373
MutablePropertyValues innerPvs = new MutablePropertyValues();
377374
innerPvs.add("country", "${os.name}");
378375
RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);

spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,20 +34,16 @@ public class ManagedListTests {
3434

3535
@Test
3636
public void mergeSunnyDay() {
37-
ManagedList parent = new ManagedList();
38-
parent.add("one");
39-
parent.add("two");
40-
ManagedList child = new ManagedList();
41-
child.add("three");
37+
ManagedList parent = ManagedList.of("one", "two");
38+
ManagedList child = ManagedList.of("three");
4239
child.setMergeEnabled(true);
4340
List mergedList = child.merge(parent);
4441
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3);
4542
}
4643

4744
@Test
4845
public void mergeWithNullParent() {
49-
ManagedList child = new ManagedList();
50-
child.add("one");
46+
ManagedList child = ManagedList.of("one");
5147
child.setMergeEnabled(true);
5248
assertThat(child.merge(null)).isSameAs(child);
5349
}
@@ -61,18 +57,15 @@ public void mergeNotAllowedWhenMergeNotEnabled() {
6157

6258
@Test
6359
public void mergeWithNonCompatibleParentType() {
64-
ManagedList child = new ManagedList();
65-
child.add("one");
60+
ManagedList child = ManagedList.of("one");
6661
child.setMergeEnabled(true);
6762
assertThatIllegalArgumentException().isThrownBy(() ->
6863
child.merge("hello"));
6964
}
7065

7166
@Test
7267
public void mergeEmptyChild() {
73-
ManagedList parent = new ManagedList();
74-
parent.add("one");
75-
parent.add("two");
68+
ManagedList parent = ManagedList.of("one", "two");
7669
ManagedList child = new ManagedList();
7770
child.setMergeEnabled(true);
7871
List mergedList = child.merge(parent);
@@ -82,11 +75,8 @@ public void mergeEmptyChild() {
8275
@Test
8376
public void mergeChildValuesOverrideTheParents() {
8477
// doesn't make much sense in the context of a list...
85-
ManagedList parent = new ManagedList();
86-
parent.add("one");
87-
parent.add("two");
88-
ManagedList child = new ManagedList();
89-
child.add("one");
78+
ManagedList parent = ManagedList.of("one", "two");
79+
ManagedList child = ManagedList.of("one");
9080
child.setMergeEnabled(true);
9181
List mergedList = child.merge(parent);
9282
assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3);

spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.beans.factory.support;
1818

19+
import java.util.AbstractMap.SimpleEntry;
1920
import java.util.Map;
2021

2122
import org.junit.jupiter.api.Test;
@@ -34,11 +35,9 @@ public class ManagedMapTests {
3435

3536
@Test
3637
public void mergeSunnyDay() {
37-
ManagedMap parent = new ManagedMap();
38-
parent.put("one", "one");
39-
parent.put("two", "two");
40-
ManagedMap child = new ManagedMap();
41-
child.put("three", "three");
38+
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
39+
new SimpleEntry<>("two", "two"));
40+
ManagedMap child = ManagedMap.ofEntries(new SimpleEntry<>("tree", "three"));
4241
child.setMergeEnabled(true);
4342
Map mergedMap = (Map) child.merge(parent);
4443
assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(3);
@@ -67,9 +66,8 @@ public void mergeNotAllowedWhenMergeNotEnabled() {
6766

6867
@Test
6968
public void mergeEmptyChild() {
70-
ManagedMap parent = new ManagedMap();
71-
parent.put("one", "one");
72-
parent.put("two", "two");
69+
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
70+
new SimpleEntry<>("two", "two"));
7371
ManagedMap child = new ManagedMap();
7472
child.setMergeEnabled(true);
7573
Map mergedMap = (Map) child.merge(parent);
@@ -78,11 +76,9 @@ public void mergeEmptyChild() {
7876

7977
@Test
8078
public void mergeChildValuesOverrideTheParents() {
81-
ManagedMap parent = new ManagedMap();
82-
parent.put("one", "one");
83-
parent.put("two", "two");
84-
ManagedMap child = new ManagedMap();
85-
child.put("one", "fork");
79+
ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"),
80+
new SimpleEntry<>("two", "two"));
81+
ManagedMap child = ManagedMap.ofEntries(new SimpleEntry<>("one", "fork"));
8682
child.setMergeEnabled(true);
8783
Map mergedMap = (Map) child.merge(parent);
8884
// child value for 'one' must override parent value...

spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,10 +34,8 @@ public class ManagedSetTests {
3434

3535
@Test
3636
public void mergeSunnyDay() {
37-
ManagedSet parent = new ManagedSet();
38-
parent.add("one");
39-
parent.add("two");
40-
ManagedSet child = new ManagedSet();
37+
ManagedSet parent = ManagedSet.of("one", "two");
38+
ManagedSet child = ManagedSet.of("three");
4139
child.add("three");
4240
child.setMergeEnabled(true);
4341
Set mergedSet = child.merge(parent);
@@ -46,8 +44,7 @@ public void mergeSunnyDay() {
4644

4745
@Test
4846
public void mergeWithNullParent() {
49-
ManagedSet child = new ManagedSet();
50-
child.add("one");
47+
ManagedSet child = ManagedSet.of("one");
5148
child.setMergeEnabled(true);
5249
assertThat(child.merge(null)).isSameAs(child);
5350
}
@@ -60,18 +57,15 @@ public void mergeNotAllowedWhenMergeNotEnabled() {
6057

6158
@Test
6259
public void mergeWithNonCompatibleParentType() {
63-
ManagedSet child = new ManagedSet();
64-
child.add("one");
60+
ManagedSet child = ManagedSet.of("one");
6561
child.setMergeEnabled(true);
6662
assertThatIllegalArgumentException().isThrownBy(() ->
6763
child.merge("hello"));
6864
}
6965

7066
@Test
7167
public void mergeEmptyChild() {
72-
ManagedSet parent = new ManagedSet();
73-
parent.add("one");
74-
parent.add("two");
68+
ManagedSet parent = ManagedSet.of("one", "two");
7569
ManagedSet child = new ManagedSet();
7670
child.setMergeEnabled(true);
7771
Set mergedSet = child.merge(parent);
@@ -81,11 +75,8 @@ public void mergeEmptyChild() {
8175
@Test
8276
public void mergeChildValuesOverrideTheParents() {
8377
// asserts that the set contract is not violated during a merge() operation...
84-
ManagedSet parent = new ManagedSet();
85-
parent.add("one");
86-
parent.add("two");
87-
ManagedSet child = new ManagedSet();
88-
child.add("one");
78+
ManagedSet parent = ManagedSet.of("one", "two");
79+
ManagedSet child = ManagedSet.of("one");
8980
child.setMergeEnabled(true);
9081
Set mergedSet = child.merge(parent);
9182
assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2);

spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -158,9 +158,7 @@ public void refresh() throws BeansException {
158158
pvs = new MutablePropertyValues();
159159
pvs.add("order", "0");
160160
pvs.add("exceptionMappings", "java.lang.Exception=failed1");
161-
List<RuntimeBeanReference> mappedHandlers = new ManagedList<>();
162-
mappedHandlers.add(new RuntimeBeanReference("anotherLocaleHandler"));
163-
pvs.add("mappedHandlers", mappedHandlers);
161+
pvs.add("mappedHandlers", ManagedList.of(new RuntimeBeanReference("anotherLocaleHandler")));
164162
pvs.add("defaultStatusCode", "500");
165163
pvs.add("defaultErrorView", "failed2");
166164
registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);

0 commit comments

Comments
 (0)