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

dev.marksman.collectionviews.ConcreteSetBuilder Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
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));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy