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

io.polyglotted.common.util.ListBuilder Maven / Gradle / Ivy

package io.polyglotted.common.util;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Supplier;

import static io.polyglotted.common.util.Assertions.checkGte;
import static io.polyglotted.common.util.ReflectionUtil.fieldValue;
import static java.util.Arrays.asList;

@SuppressWarnings({"unused", "WeakerAccess"})
public interface ListBuilder, LB extends ListBuilder> {

    @SuppressWarnings("UnusedReturnValue") LB add(E elem);

    LB addAll(Iterable elems);

    int size();

    L build();

    default L atleastOne() { checkGte(size(), 1, "size"); return build(); }

    static  ImmutableListBuilder immutableListBuilder() { return new ImmutableListBuilder<>(); }

    static  ImmutableList immutableList() { return ImmutableList.of(); }

    @SafeVarargs static  ImmutableList immutableList(E... elems) { return immutableList(asList(elems)); }

    static  ImmutableList immutableList(Iterable coll) { return ListBuilder.immutableListBuilder().addAll(coll).build(); }

    static  SimpleListBuilder simpleListBuilder(Collection coll) { return ListBuilder.simpleListBuilder().addAll(coll); }

    static  SimpleListBuilder simpleListBuilder() { return simpleListBuilder(LinkedList::new); }

    static  SimpleListBuilder simpleListBuilder(Supplier> supplier) { return new SimpleListBuilder<>(supplier.get()); }

    static  List simpleList() { return new LinkedList<>(); }

    @SafeVarargs static  List simpleList(E... elems) { return simpleList(asList(elems)); }

    static  List simpleList(Iterable coll) { return ListBuilder.simpleListBuilder().addAll(coll).build(); }

    static  SimpleSetBuilder simpleSetBuilder(Collection coll) { return ListBuilder.simpleSetBuilder().addAll(coll); }

    static  SimpleSetBuilder simpleSetBuilder() { return simpleSetBuilder(TreeSet::new); }

    static  SimpleSetBuilder simpleSetBuilder(Supplier> supplier) { return new SimpleSetBuilder<>(supplier.get()); }

    static  ImmutableSet immutableSet() { return ImmutableSet.of(); }

    @SafeVarargs static  Set immutableSet(E... elems) { return immutableSet(asList(elems)); }

    static  ImmutableSet immutableSet(SimpleSetBuilder builder) { return immutableSet(builder.build()); }

    static  ImmutableSet immutableSet(Iterable iterable) { return ImmutableSet.copyOf(iterable); }

    @SafeVarargs static  ImmutableSortedSet immutableSortedSet(E... elems) { return immutableSortedSet(asList(elems)); }

    static  ImmutableSortedSet immutableSortedSet(SimpleSetBuilder builder) { return immutableSortedSet(builder.build()); }

    static  ImmutableSortedSet immutableSortedSet(Iterable iterable) { return ImmutableSortedSet.copyOf(iterable); }

    @RequiredArgsConstructor(access = AccessLevel.PRIVATE) class ImmutableListBuilder
        implements ListBuilder, ImmutableListBuilder> {
        private final ImmutableList.Builder builder = ImmutableList.builder();

        @Override public ImmutableListBuilder add(E elem) { if (elem != null) builder.add(elem); return this; }

        @Override public ImmutableListBuilder addAll(Iterable elems) { for (E elem : elems) { add(elem); } return this; }

        @Override public int size() { return fieldValue(builder, "size"); }

        @Override public ImmutableList build() { return builder.build(); }
    }

    @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @Accessors(fluent = true) class SimpleListBuilder
        implements ListBuilder, SimpleListBuilder> {
        @Getter private final List builder;

        @Override public SimpleListBuilder add(E elem) { if (elem != null) builder.add(elem); return this; }

        public SimpleListBuilder remove(E elem) { builder.remove(elem); return this; }

        @Override public SimpleListBuilder addAll(Iterable elems) { for (E elem : elems) { add(elem); } return this; }

        public SimpleListBuilder removeAll(Collection elems) { builder.removeAll(elems); return this; }

        @Override public int size() { return builder.size(); }

        @Override public List build() { return builder; }
    }

    @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @Accessors(fluent = true) class SimpleSetBuilder
        implements ListBuilder, SimpleSetBuilder> {
        @Getter private final Set builder;

        @Override public SimpleSetBuilder add(E elem) { if (elem != null) builder.add(elem); return this; }

        public SimpleSetBuilder remove(E elem) { builder.remove(elem); return this; }

        @Override public SimpleSetBuilder addAll(Iterable elems) { for (E elem : elems) { add(elem); } return this; }

        public SimpleSetBuilder removeAll(Collection elems) { builder.removeAll(elems); return this; }

        @Override public int size() { return builder.size(); }

        @Override public Set build() { return builder; }

        public ImmutableSet immutable() { return ListBuilder.immutableSet(builder); }

        public ImmutableSortedSet immutableSorted() { return ListBuilder.immutableSortedSet(builder); }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy