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

org.bitbucket.cowwoc.guava.stream.ImmutableSetMultimapCollectorBuilder 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.ImmutableSetMultimap;
import com.google.common.collect.Multimap;
import java.util.Comparator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collector.Characteristics;
import org.bitbucket.cowwoc.preconditions.Preconditions;

/**
 * Builds a stream {@link Collector} that returns an {@link ImmutableSetMultimap}.
 * 

* @author Gili Tzabari * @param the type of the input elements * @param the type of keys stored in the map * @param the type of values stored in the map */ public final class ImmutableSetMultimapCollectorBuilder extends MultimapCollectorBuilder> { private Comparator keysComparator; private Comparator valuesComparator; /** * Creates a new builder. *

* @param keyMapper a function that transforms the input into map keys * @param valueMapper a function that transforms the input into map values * @param supplier a function which returns a new, empty {@code Multimap} into which * intermediate results will be added * @param characteristics the collector characteristics * @throws NullPointerException if any of the arguments are null */ public ImmutableSetMultimapCollectorBuilder(Function keyMapper, Function valueMapper, Supplier> supplier, Characteristics... characteristics) { super(keyMapper, valueMapper, supplier, ImmutableSetMultimap::copyOf, characteristics); } /** * Sorts the map keys. *

* WARNING: Invoking this method overrides the * {@link #finisher(java.util.function.Function) finisher}. *

* @param comparator specifies the key sorting order * @return this * @throws NullPointerException if comparator is null */ public ImmutableSetMultimapCollectorBuilder orderKeysBy( Comparator comparator) throws NullPointerException { Preconditions.requireThat(comparator, "comparator").isNotNull(); this.keysComparator = comparator; return this; } /** * @return the key sorting order */ public Comparator orderKeysBy() { return keysComparator; } /** * Sorts the map values. *

* WARNING: Invoking this method overrides the * {@link #finisher(java.util.function.Function) finisher}. *

* @param comparator specifies the value sorting order * @return this * @throws NullPointerException if comparator is null */ public ImmutableSetMultimapCollectorBuilder orderValuesBy( Comparator comparator) throws NullPointerException { Preconditions.requireThat(comparator, "comparator").isNotNull(); this.valuesComparator = comparator; return this; } /** * @return the value sorting order */ public Comparator orderValuesBy() { return valuesComparator; } /** * {@inheritDoc} WARNING: Invoking this method overrides the * {@link #orderKeysBy(java.util.Comparator) key} and * {@link #orderValuesBy(java.util.Comparator) value} sort order. */ @Override public ImmutableSetMultimapCollectorBuilder finisher( Function, ImmutableSetMultimap> finisher) throws NullPointerException { this.keysComparator = null; this.valuesComparator = null; super.finisher(finisher); return this; } /** * @return a new collector */ public Collector> build() { if (keysComparator != null || valuesComparator != null) { super.finisher(value -> { ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder(). putAll(value); if (keysComparator != null) builder.orderKeysBy(keysComparator); if (valuesComparator != null) builder.orderValuesBy(valuesComparator); return builder.build(); }); } return super.build(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy