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

org.bitbucket.cowwoc.guava.stream.GuavaCollectorBuilder Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package org.bitbucket.cowwoc.guava.stream;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.ImmutableSortedMultiset;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultiset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector.Characteristics;
import static java.util.stream.Collector.Characteristics.UNORDERED;

/**
 * Captures the type of the input elements.
 * 

* @param the type of the input elements * @author Gili Tzabari */ public final class GuavaCollectorBuilder { /** * Meant to be constructed by GuavaCollector. */ GuavaCollectorBuilder() { } /** * Returns a builder for a {@code Collector} that accumulates elements into a {@code Multimap}. *

* The default intermediate container is not thread-safe. Use * {@link MultimapCollectorBuilder#supplier(Supplier)} to replace it. *

* @param the type of the map keys * @param the type of the map values * @param keyMapper a function that transforms the input to map keys * @param valueMapper a function that transforms the input to map values * @return a builder for a {@code Multimap} collector * @throws NullPointerException if any of the arguments are null */ public MultimapCollectorBuilder> toMultimap( Function keyMapper, Function valueMapper) throws NullPointerException { return new MultimapCollectorBuilder<>(keyMapper, valueMapper, ArrayListMultimap::create, input -> input, Characteristics.UNORDERED, Characteristics.IDENTITY_FINISH); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableMultimap}. *

* The default intermediate container is not thread-safe. Use * {@link MultimapCollectorBuilder#supplier(Supplier)} to replace it. *

* @param the type of the map keys * @param the type of the map values * @param keyMapper a function that transforms the input to map keys * @param valueMapper a function that transforms the input to map values * @return a builder for an {@code ImmutableMultimap} collector * @throws NullPointerException if any of the arguments are null */ public MultimapCollectorBuilder> toImmutableMultimap( Function keyMapper, Function valueMapper) throws NullPointerException { return new MultimapCollectorBuilder<>(keyMapper, valueMapper, ArrayListMultimap::create, ImmutableMultimap::copyOf, Characteristics.UNORDERED); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableMultiset}. *

* The default intermediate container is not thread-safe. Use * {@link MultisetCollectorBuilder#supplier(Supplier)} to replace it. *

* @return a builder for an {@code ImmutableMultiset} collector */ public MultisetCollectorBuilder> toImmutableMultiset() { return new MultisetCollectorBuilder<>(HashMultiset::create, ImmutableMultiset::copyOf, Characteristics.UNORDERED); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableMap}. *

* The default intermediate container is not thread-safe. Use * {@link MapCollectorBuilder#supplier(Supplier)} to replace it. *

* @param the type of the map keys * @param the type of the map values * @param keyMapper a function that transforms the map keys * @param valueMapper a function that transforms the map values * @return a builder for a {@code ImmutableMap} collector * @throws NullPointerException if any of the arguments are null */ public MapCollectorBuilder> toImmutableMap( Function keyMapper, Function valueMapper) throws NullPointerException { return new MapCollectorBuilder<>(keyMapper, valueMapper, HashMap::new, ImmutableMap::copyOf, Characteristics.UNORDERED); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableSortedMap}. *

* The default intermediate container is not thread-safe. Use * {@link MapCollectorBuilder#supplier(Supplier)} to replace it. *

* @param the type of the map keys * @param the type of the map values * @param keyMapper a function that transforms the map keys * @param valueMapper a function that transforms the map values * @param comparator a comparator that defines the sort order of the keys * @return a builder for an {@code ImmutableSortedMap} collector * @see Comparator#naturalOrder() * @throws NullPointerException if any of the arguments are null */ public MapCollectorBuilder> toImmutableSortedMap(Function keyMapper, Function valueMapper, Comparator comparator) throws NullPointerException { return new MapCollectorBuilder<>(keyMapper, valueMapper, HashMap::new, input -> ImmutableSortedMap.copyOf(input, comparator)); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableBiMap}. *

* The default intermediate container is not thread-safe. Use * {@link MultimapCollectorBuilder#supplier(Supplier)} to replace it. *

* @param the type of the map keys * @param the type of the map values * @param keyMapper a function that transforms the map keys * @param valueMapper a function that transforms the map values * @return a builder for an {@code ImmutableBiMap} collector * @throws NullPointerException if any of the arguments are null */ public MapCollectorBuilder> toImmutableBiMap(Function keyMapper, Function valueMapper) throws NullPointerException { return new MapCollectorBuilder<>(keyMapper, valueMapper, HashBiMap::create, ImmutableBiMap::copyOf, UNORDERED); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableList}. *

* The default intermediate container is not thread-safe. Use * {@link ListCollectorBuilder#supplier(Supplier)} to replace it. *

* @return a builder for an {@code ImmutableList} collector */ public ListCollectorBuilder> toImmutableList() { return new ListCollectorBuilder<>(ArrayList::new, ImmutableList::copyOf); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableSet}. *

* The default intermediate container is not thread-safe. Use * {@link SetCollectorBuilder#supplier(Supplier)} to replace it. *

* @return a builder for an {@code ImmutableSet} collector */ public SetCollectorBuilder> toImmutableSet() { return new SetCollectorBuilder<>(HashSet::new, ImmutableSet::copyOf); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableSortedSet}. *

* The default intermediate container is not thread-safe. Use * {@link SetCollectorBuilder#supplier(Supplier)} to replace it. *

* @param comparator a comparator that defines the sort order of the keys * @return a builder for an {@code ImmutableSortedSet} collector * @see Comparator#naturalOrder() * @throws NullPointerException if any of the arguments are null */ public SetCollectorBuilder> toImmutableSortedSet( Comparator comparator) throws NullPointerException { return new SetCollectorBuilder<>(HashSet::new, ImmutableSortedSet::copyOf); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableSortedSet}. *

* The default intermediate container is not thread-safe. Use * {@link MultisetCollectorBuilder#supplier(Supplier)} to replace it. *

* @param comparator a comparator that defines the sort order of the elements * @return a builder for an {@code ImmutableSortedSet} collector * @see Comparator#naturalOrder() * @throws NullPointerException if any of the arguments are null */ public MultisetCollectorBuilder> toImmutableSortedMultiset(Comparator comparator) throws NullPointerException { return new MultisetCollectorBuilder<>(() -> TreeMultiset.create(comparator), input -> ImmutableSortedMultiset.copyOf(comparator, input)); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableListMultimap}. *

* The default intermediate container is not thread-safe. Use * {@link MultimapCollectorBuilder#supplier(Supplier)} to replace it. *

* @param the type of the map keys * @param the type of the map values * @param keyMapper a function that transforms the input to map keys * @param valueMapper a function that transforms the input to map values * @return a builder for an {@code ImmutableListMultimap} collector * @throws NullPointerException if any of the arguments are null */ public MultimapCollectorBuilder> toImmutableListMultimap(Function keyMapper, Function valueMapper) throws NullPointerException { return new MultimapCollectorBuilder<>(keyMapper, valueMapper, ArrayListMultimap::create, ImmutableListMultimap::copyOf); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableSetMultimap}. *

* The default intermediate container is not thread-safe. Use * {@link MultimapCollectorBuilder#supplier(Supplier)} to replace it. *

* @param the type of the map keys * @param the type of the map values * @param keyMapper a function that transforms the input to map keys * @param valueMapper a function that transforms the input to map values * @return a builder for an {@code ImmutableListMultimap} collector * @throws NullPointerException if any of the arguments are null */ public ImmutableSetMultimapCollectorBuilder toImmutableSetMultimap(Function keyMapper, Function valueMapper) throws NullPointerException { return new ImmutableSetMultimapCollectorBuilder<>(keyMapper, valueMapper, HashMultimap::create, UNORDERED); } /** * Returns a builder for a {@code Collector} that accumulates elements into an * {@code ImmutableTable}. *

* The default intermediate container is not thread-safe. Use * {@link TableCollectorBuilder#supplier(Supplier)} to replace it. *

* @param the type of the table rows * @param the type of the table columns * @param the type of the table values * @param rowMapper a function that transforms the input to table rows * @param columnMapper a function that transforms the input to table columns * @param valueMapper a function that transforms the input to table values * @return a builder for an {@code ImmutableListMultimap} collector * @throws NullPointerException if any of the arguments are null */ public ImmutableTableCollectorBuilder toImmutableTable(Function rowMapper, Function columnMapper, Function valueMapper) throws NullPointerException { return new ImmutableTableCollectorBuilder<>(rowMapper, columnMapper, valueMapper, HashBasedTable::create, UNORDERED); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy