Skip to content

Commit bfc02cd

Browse files
florianhofsbrannen
authored andcommitted
Introduce MultiValueMapCollector for use with streams
See spring-projects/spring-data-commons#3420 See gh-35958 Signed-off-by: Florian Hof <florian.hof@sbb.ch>
1 parent f734d04 commit bfc02cd

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.util.EnumSet;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map.Entry;
23+
import java.util.Set;
24+
import java.util.function.BiConsumer;
25+
import java.util.function.BinaryOperator;
26+
import java.util.function.Function;
27+
import java.util.function.Supplier;
28+
import java.util.stream.Collector;
29+
30+
/**
31+
* A {@link Collector} for building a {@link MultiValueMap} from a {@link java.util.stream.Stream}.
32+
* <br/>
33+
* Moved from {@code org.springframework.data.util.MultiValueMapCollector}.
34+
*
35+
* @author Jens Schauder
36+
*
37+
* @param <T> – the type of input elements to the reduction operation
38+
* @param <K> – the type of the key elements
39+
* @param <V> – the type of the value elements
40+
*/
41+
public class MultiValueMapCollector<T, K, V> implements Collector<T, MultiValueMap<K, V>, MultiValueMap<K, V>> {
42+
private final Function<T, K> keyFunction;
43+
private final Function<T, V> valueFunction;
44+
45+
public MultiValueMapCollector(Function<T, K> keyFunction, Function<T, V> valueFunction) {
46+
this.keyFunction = keyFunction;
47+
this.valueFunction = valueFunction;
48+
}
49+
50+
public static <K, V> MultiValueMapCollector<V, K, V> indexingBy(Function<V, K> indexer) {
51+
return new MultiValueMapCollector<>(indexer, Function.identity());
52+
}
53+
54+
@Override
55+
public Supplier<MultiValueMap<K, V>> supplier() {
56+
return () -> CollectionUtils.toMultiValueMap(new HashMap<K, List<V>>());
57+
}
58+
59+
@Override
60+
public BiConsumer<MultiValueMap<K, V>, T> accumulator() {
61+
return (map, t) -> map.add(this.keyFunction.apply(t), this.valueFunction.apply(t));
62+
}
63+
64+
@Override
65+
public BinaryOperator<MultiValueMap<K, V>> combiner() {
66+
return (map1, map2) -> {
67+
for (Entry<K, List<V>> entry : map2.entrySet()) {
68+
map1.addAll(entry.getKey(), entry.getValue());
69+
}
70+
return map1;
71+
};
72+
}
73+
74+
@Override
75+
public Function<MultiValueMap<K, V>, MultiValueMap<K, V>> finisher() {
76+
return Function.identity();
77+
}
78+
79+
@Override
80+
public Set<Characteristics> characteristics() {
81+
return EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.UNORDERED);
82+
}
83+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.util.stream.Stream;
20+
21+
import org.junit.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link MultiValueMapCollector}.
27+
*
28+
* @author Florian Hof
29+
*/
30+
class MultiValueMapCollectorTests {
31+
32+
@Test
33+
void indexingBy() {
34+
MultiValueMapCollector<String, Integer, String> collector = MultiValueMapCollector.indexingBy(String::length);
35+
MultiValueMap<Integer, String> content = Stream.of("abc", "ABC", "123", "1234", "abcdef", "ABCDEF").collect(collector);
36+
assertThat(content.get(3)).containsOnly("abc", "ABC", "123");
37+
assertThat(content.get(4)).containsOnly("abcdef", "ABCDEF");
38+
assertThat(content.get(6)).containsOnly("1234");
39+
assertThat(content.get(1)).isNull();
40+
}
41+
}

0 commit comments

Comments
 (0)