Skip to content

Commit 59c9a9c

Browse files
committed
Polish "Call the value adapter during NamedContributorsMapAdapter construction"
See gh-31676
1 parent c530f12 commit 59c9a9c

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/NamedContributorsMapAdapter.java

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

1919
import java.util.Collections;
2020
import java.util.Iterator;
21-
import java.util.LinkedHashMap;
2221
import java.util.Map;
2322
import java.util.Map.Entry;
2423
import java.util.function.Function;
24+
import java.util.stream.Collectors;
2525

2626
import org.springframework.util.Assert;
2727

@@ -37,34 +37,30 @@
3737
*/
3838
abstract class NamedContributorsMapAdapter<V, C> implements NamedContributors<C> {
3939

40-
private final Map<String, C> namedContributorsMap;
40+
private final Map<String, C> map;
4141

4242
NamedContributorsMapAdapter(Map<String, V> map, Function<V, ? extends C> valueAdapter) {
4343
Assert.notNull(map, "Map must not be null");
4444
Assert.notNull(valueAdapter, "ValueAdapter must not be null");
45-
this.namedContributorsMap = getContributorsMap(map, valueAdapter);
46-
}
47-
48-
private Map<String, C> getContributorsMap(Map<String, V> map, Function<V, ? extends C> valueAdapter) {
49-
Map<String, C> contributorsMap = new LinkedHashMap<>(map.size());
50-
map.forEach((name, value) -> {
51-
this.validateKey(name);
52-
C contributor = adapt(value, valueAdapter);
53-
Assert.notNull(contributor, "Map must not contain null values");
54-
contributorsMap.put(name, contributor);
55-
});
56-
return Collections.unmodifiableMap(contributorsMap);
45+
map.keySet().forEach(this::validateKey);
46+
this.map = Collections.unmodifiableMap(map.entrySet().stream()
47+
.collect(Collectors.toMap(Entry::getKey, (entry) -> adapt(entry.getValue(), valueAdapter))));
5748
}
5849

5950
private void validateKey(String value) {
6051
Assert.notNull(value, "Map must not contain null keys");
6152
Assert.isTrue(!value.contains("/"), "Map keys must not contain a '/'");
53+
}
6254

55+
private C adapt(V value, Function<V, ? extends C> valueAdapter) {
56+
C contributor = (value != null) ? valueAdapter.apply(value) : null;
57+
Assert.notNull(contributor, "Map must not contain null values");
58+
return contributor;
6359
}
6460

6561
@Override
6662
public Iterator<NamedContributor<C>> iterator() {
67-
Iterator<Entry<String, C>> iterator = this.namedContributorsMap.entrySet().iterator();
63+
Iterator<Entry<String, C>> iterator = this.map.entrySet().iterator();
6864
return new Iterator<NamedContributor<C>>() {
6965

7066
@Override
@@ -83,11 +79,7 @@ public NamedContributor<C> next() {
8379

8480
@Override
8581
public C getContributor(String name) {
86-
return this.namedContributorsMap.get(name);
87-
}
88-
89-
private C adapt(V value, Function<V, ? extends C> valueAdapter) {
90-
return (value != null) ? valueAdapter.apply(value) : null;
82+
return this.map.get(name);
9183
}
9284

9385
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/NamedContributorsMapAdapterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Tests for {@link NamedContributorsMapAdapter}.
3333
*
3434
* @author Phillip Webb
35+
* @author Guirong Hu
3536
*/
3637
class NamedContributorsMapAdapterTests {
3738

@@ -94,12 +95,11 @@ void getContributorReturnsAdaptedEntry() {
9495
}
9596

9697
@Test
97-
void eachValueAdapterShouldBeCalledOnlyOnce() {
98+
void getContributorCallsAdaptersOnlyOnce() {
9899
Map<String, String> map = new LinkedHashMap<>();
99100
map.put("one", "one");
100101
map.put("two", "two");
101102
int callCount = map.size();
102-
103103
AtomicInteger counter = new AtomicInteger(0);
104104
TestNamedContributorsMapAdapter<String> adapter = new TestNamedContributorsMapAdapter<>(map,
105105
(name) -> count(name, counter));

0 commit comments

Comments
 (0)