All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.container.map.SetMapCollector Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.map;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
 * A {@link Collector} for {@link Map} with {@link Set}.
 * 

* TODO create unit test * * @author Oliver Richers */ public class SetMapCollector implements Collector>, Map>> { private final Function keyExtractor; private final Function valueExtractor; private final Supplier>> mapFactory; private final Supplier> setFactory; private SetMapCollector(Function keyExtractor, Function valueExtractor, Supplier>> mapFactory, Supplier> setFactory) { this.keyExtractor = keyExtractor; this.valueExtractor = valueExtractor; this.mapFactory = mapFactory; this.setFactory = setFactory; } public static SetMapCollector of(Function keyExtractor) { // TODO cannot use HashMap::new because of defect in javac return new SetMapCollector<>(keyExtractor, Function.identity(), () -> new HashMap<>(), HashSet::new); } public static SetMapCollector of(Function keyExtractor, Supplier>> mapFactory, Supplier> setFactory) { return new SetMapCollector<>(keyExtractor, Function.identity(), mapFactory, setFactory); } public static SetMapCollector of(Function keyExtractor, Function valueExtractor) { // TODO cannot use HashMap::new because of defect in javac return new SetMapCollector<>(keyExtractor, valueExtractor, () -> new HashMap<>(), HashSet::new); } public static SetMapCollector of(Function keyExtractor, Function valueExtractor, Supplier>> mapFactory, Supplier> setFactory) { return new SetMapCollector<>(keyExtractor, valueExtractor, mapFactory, setFactory); } @Override public Supplier>> supplier() { return mapFactory; } @Override public BiConsumer>, T> accumulator() { return (map, element) -> accumulate(map, element); } @Override public BinaryOperator>> combiner() { return this::combine; } @Override public Function>, Map>> finisher() { return map -> map; } @Override public Set characteristics() { return EnumSet.of(Characteristics.IDENTITY_FINISH); } private void accumulate(Map> map, T element) { K key = keyExtractor.apply(element); V value = valueExtractor.apply(element); getOrCreateSet(map, key).add(value); } private Map> combine(Map> map1, Map> map2) { for (Entry> entry: map2.entrySet()) { getOrCreateSet(map1, entry.getKey()).addAll(entry.getValue()); } return map1; } private Set getOrCreateSet(Map> map, K key) { Set set = map.get(key); if (set == null) { map.put(key, set = setFactory.get()); } return set; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy