org.bitbucket.cowwoc.guava.stream.SetCollectorBuilder Maven / Gradle / Ivy
Show all versions of guava-jdk8 Show documentation
package org.bitbucket.cowwoc.guava.stream;
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;
import java.util.stream.Collector.Characteristics;
import org.bitbucket.cowwoc.preconditions.Preconditions;
/**
* Builds a stream {@link Collector} that returns a {@link Set}.
*
* @author Gili Tzabari
* @param the type of the input elements
* @param the output type of the collector
*/
public final class SetCollectorBuilder>
{
private Supplier> supplier;
private Function, R> finisher;
private Characteristics[] characteristics;
/**
* Creates a new builder.
*
* @param supplier a function which returns a new, empty {@code Set} into which
* intermediate results will be added
* @param finisher a function that transforms the intermediate {@code Set} into the final
* result
* @param characteristics the collector characteristics
* @throws NullPointerException if any of the arguments are null
*/
public SetCollectorBuilder(Supplier> supplier, Function, R> finisher,
Characteristics... characteristics)
{
Preconditions.requireThat(supplier, "supplier").isNotNull();
Preconditions.requireThat(finisher, "finisher").isNotNull();
Preconditions.requireThat(characteristics, "characteristics").isNotNull();
this.supplier = supplier;
this.finisher = finisher;
this.characteristics = characteristics;
}
/**
* @return a function which returns a new, empty {@code Set} into which intermediate results will
* be added
*/
public Supplier> supplier()
{
return supplier;
}
/**
* @param supplier a function which returns a new, empty {@code Set} into which intermediate
* results will be added
* @return this
* @throws NullPointerException if supplier is null
*/
public SetCollectorBuilder supplier(Supplier> supplier)
throws NullPointerException
{
Preconditions.requireThat(supplier, "supplier").isNotNull();
this.supplier = supplier;
return this;
}
/**
* @return a function that transforms the intermediate {@code Set} into the final result
*/
public Function, R> finisher()
{
return finisher;
}
/**
* Sets a function that transforms the intermediate {@code Set} into the final result.
*
* Make sure to update the collector
* {@link #characteristics(java.util.stream.Collector.Characteristics[]) characteristics} if
* necessary.
*
* @param finisher a function that transforms the intermediate {@code Set} into the final result
* @return this
* @throws NullPointerException if finisher is null
*/
public SetCollectorBuilder finisher(Function, R> finisher)
throws NullPointerException
{
Preconditions.requireThat(finisher, "finisher").isNotNull();
this.finisher = finisher;
return this;
}
/**
* @return the collector characteristics
*/
@SuppressWarnings("ReturnOfCollectionOrArrayField")
public Characteristics[] characteristics()
{
return characteristics;
}
/**
* @param characteristics the collector characteristics
* @return this
* @throws NullPointerException if characteristics is null
*/
@SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
public SetCollectorBuilder characteristics(Characteristics... characteristics)
throws NullPointerException
{
Preconditions.requireThat(characteristics, "characteristics").isNotNull();
this.characteristics = characteristics;
return this;
}
/**
* @return a new collector
*/
public Collector build()
{
BiConsumer, T> accumulator = (set, value) ->
{
set.add(value);
};
BinaryOperator> combiner = (left, right) ->
{
left.addAll(right);
return left;
};
return Collector.of(supplier, accumulator, combiner, finisher, characteristics);
}
}