dev.marksman.collectionviews.EmptySetBuilder 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.ConcreteSetBuilder.concreteSetBuilder;
final class EmptySetBuilder implements SetBuilder {
private final Maybe initialCapacity;
private EmptySetBuilder(Maybe initialCapacity) {
this.initialCapacity = initialCapacity;
}
@Override
public NonEmptySetBuilder add(A element) {
return concreteSetBuilder(initialCapacity, element);
}
@Override
public SetBuilder addAll(Collection elements) {
Objects.requireNonNull(elements);
if (elements.isEmpty()) {
return this;
} else {
return concreteSetBuilder(initialCapacity, elements);
}
}
@Override
public SetBuilder addAll(FiniteIterable elements) {
Objects.requireNonNull(elements);
if (elements.isEmpty()) {
return this;
} else {
return concreteSetBuilder(initialCapacity, elements);
}
}
@Override
public NonEmptySetBuilder addAll(NonEmptyFiniteIterable elements) {
Objects.requireNonNull(elements);
return concreteSetBuilder(initialCapacity, elements);
}
@Override
public ImmutableSet build() {
return Set.empty();
}
static EmptySetBuilder emptySetBuilder(Maybe initialCapacity) {
return new EmptySetBuilder<>(initialCapacity.filter(n -> n >= 0));
}
}