Skip to content

Commit

Permalink
Polish "Call the value adapter during NamedContributorsMapAdapter con…
Browse files Browse the repository at this point in the history
…struction"

See gh-31676
  • Loading branch information
philwebb committed Jul 26, 2022
1 parent c530f12 commit 59c9a9c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
Expand Up @@ -18,10 +18,10 @@

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.util.Assert;

Expand All @@ -37,34 +37,30 @@
*/
abstract class NamedContributorsMapAdapter<V, C> implements NamedContributors<C> {

private final Map<String, C> namedContributorsMap;
private final Map<String, C> map;

NamedContributorsMapAdapter(Map<String, V> map, Function<V, ? extends C> valueAdapter) {
Assert.notNull(map, "Map must not be null");
Assert.notNull(valueAdapter, "ValueAdapter must not be null");
this.namedContributorsMap = getContributorsMap(map, valueAdapter);
}

private Map<String, C> getContributorsMap(Map<String, V> map, Function<V, ? extends C> valueAdapter) {
Map<String, C> contributorsMap = new LinkedHashMap<>(map.size());
map.forEach((name, value) -> {
this.validateKey(name);
C contributor = adapt(value, valueAdapter);
Assert.notNull(contributor, "Map must not contain null values");
contributorsMap.put(name, contributor);
});
return Collections.unmodifiableMap(contributorsMap);
map.keySet().forEach(this::validateKey);
this.map = Collections.unmodifiableMap(map.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, (entry) -> adapt(entry.getValue(), valueAdapter))));
}

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

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

@Override
public Iterator<NamedContributor<C>> iterator() {
Iterator<Entry<String, C>> iterator = this.namedContributorsMap.entrySet().iterator();
Iterator<Entry<String, C>> iterator = this.map.entrySet().iterator();
return new Iterator<NamedContributor<C>>() {

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

@Override
public C getContributor(String name) {
return this.namedContributorsMap.get(name);
}

private C adapt(V value, Function<V, ? extends C> valueAdapter) {
return (value != null) ? valueAdapter.apply(value) : null;
return this.map.get(name);
}

}
Expand Up @@ -32,6 +32,7 @@
* Tests for {@link NamedContributorsMapAdapter}.
*
* @author Phillip Webb
* @author Guirong Hu
*/
class NamedContributorsMapAdapterTests {

Expand Down Expand Up @@ -94,12 +95,11 @@ void getContributorReturnsAdaptedEntry() {
}

@Test
void eachValueAdapterShouldBeCalledOnlyOnce() {
void getContributorCallsAdaptersOnlyOnce() {
Map<String, String> map = new LinkedHashMap<>();
map.put("one", "one");
map.put("two", "two");
int callCount = map.size();

AtomicInteger counter = new AtomicInteger(0);
TestNamedContributorsMapAdapter<String> adapter = new TestNamedContributorsMapAdapter<>(map,
(name) -> count(name, counter));
Expand Down

0 comments on commit 59c9a9c

Please sign in to comment.