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

io.github.stewseo.client.util.ListBuilder Maven / Gradle / Ivy

Go to download

java client to build api objects, handle http transport, and parse/deserialize/serialize json to/from json

There is a newer version: 1.8.0
Show newest version
package io.github.stewseo.client.util;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public class ListBuilder implements ObjectBuilder> {
    private final List list = new ArrayList<>();
    private final Supplier builderCtor;

    public static > ListBuilder of(Supplier builderCtor) {
        return new ListBuilder<>(builderCtor);
    }

    public ListBuilder(Supplier builderCtor) {
        this.builderCtor = builderCtor;
    }

    public ListBuilder add(T value) {
        list.add(value);
        return this;
    }

    public ListBuilder add(Function> fn) {
        return add(fn.apply(builderCtor.get()).build());
    }

    public ListBuilder addAll(Iterable iterable) {
        for (T item: iterable) {
            list.add(item);
        }
        return this;
    }

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