001package ca.uhn.fhir.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import com.google.common.collect.ArrayListMultimap;
024import com.google.common.collect.ImmutableSet;
025import com.google.common.collect.ListMultimap;
026
027import java.util.Set;
028import java.util.function.BiConsumer;
029import java.util.function.BinaryOperator;
030import java.util.function.Function;
031import java.util.function.Supplier;
032import java.util.stream.Collector;
033
034/**
035 * Copied from https://stackoverflow.com/questions/23003542/cleanest-way-to-create-a-guava-multimap-from-a-java-8-stream
036 */
037public class MultimapCollector<T, K, V> implements
038        Collector<T, ListMultimap<K, V>, ListMultimap<K, V>> {
039
040        private final Function<T, K> keyGetter;
041        private final Function<T, V> valueGetter;
042
043        public MultimapCollector(Function<T, K> keyGetter, Function<T, V> valueGetter) {
044                this.keyGetter = keyGetter;
045                this.valueGetter = valueGetter;
046        }
047
048        public static <T, K, V> MultimapCollector<T, K, V> toMultimap(Function<T, K> keyGetter, Function<T, V> valueGetter) {
049                return new MultimapCollector<>(keyGetter, valueGetter);
050        }
051
052        public static <T, K, V> MultimapCollector<T, K, T> toMultimap(Function<T, K> keyGetter) {
053                return new MultimapCollector<>(keyGetter, v -> v);
054        }
055
056        @Override
057        public Supplier<ListMultimap<K, V>> supplier() {
058                return ArrayListMultimap::create;
059        }
060
061        @Override
062        public BiConsumer<ListMultimap<K, V>, T> accumulator() {
063                return (map, element) -> map.put(keyGetter.apply(element), valueGetter.apply(element));
064        }
065
066        @Override
067        public BinaryOperator<ListMultimap<K, V>> combiner() {
068                return (map1, map2) -> {
069                        map1.putAll(map2);
070                        return map1;
071                };
072        }
073
074        @Override
075        public Function<ListMultimap<K, V>, ListMultimap<K, V>> finisher() {
076                return map -> map;
077        }
078
079        @Override
080        public Set<Characteristics> characteristics() {
081                return ImmutableSet.of(Characteristics.IDENTITY_FINISH);
082        }
083}