dev.marksman.collectionviews.ConcreteSetBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of collection-views Show documentation
Show all versions of collection-views Show documentation
Low overhead, protected views over Java collections
package dev.marksman.collectionviews;
import com.jnape.palatable.lambda.adt.Maybe;
import dev.marksman.enhancediterables.FiniteIterable;
import dev.marksman.enhancediterables.NonEmptyFiniteIterable;
import java.util.Collection;
import java.util.Objects;
import static dev.marksman.collectionviews.ConcreteVectorBuilder.concreteVectorBuilder;
final class ConcreteSetBuilder implements NonEmptySetBuilder {
private final NonEmptyVectorBuilder underlying;
private ConcreteSetBuilder(NonEmptyVectorBuilder underlying) {
this.underlying = underlying;
}
@Override
public NonEmptySetBuilder add(A element) {
return concreteSetBuilder(underlying.add(element));
}
@Override
public NonEmptySetBuilder addAll(Collection elements) {
Objects.requireNonNull(elements);
if (elements.isEmpty()) {
return this;
} else {
return concreteSetBuilder(underlying.addAll(elements));
}
}
@Override
public NonEmptySetBuilder addAll(FiniteIterable elements) {
Objects.requireNonNull(elements);
if (elements.isEmpty()) {
return this;
} else {
return concreteSetBuilder(underlying.addAll(elements));
}
}
@Override
public NonEmptySetBuilder addAll(NonEmptyFiniteIterable elements) {
Objects.requireNonNull(elements);
return concreteSetBuilder(underlying.addAll(elements));
}
@Override
public ImmutableNonEmptySet build() {
return ImmutableSets.copyFrom(underlying.build()).toNonEmptyOrThrow();
}
static ConcreteSetBuilder concreteSetBuilder(NonEmptyVectorBuilder underlying) {
return new ConcreteSetBuilder<>(underlying);
}
static ConcreteSetBuilder concreteSetBuilder(Maybe initialCapacity, A firstElement) {
return concreteSetBuilder(concreteVectorBuilder(initialCapacity, firstElement));
}
static ConcreteSetBuilder concreteSetBuilder(Maybe initialCapacity, Iterable elements) {
return concreteSetBuilder(concreteVectorBuilder(initialCapacity, elements));
}
}