|
| 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 | +} |
0 commit comments