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

seatsio.json.JsonArrayBuilder Maven / Gradle / Ivy

The newest version!
package seatsio.json;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;

public class JsonArrayBuilder {

    private Collection items = new ArrayList<>();

    public static JsonArrayBuilder aJsonArray() {
        return new JsonArrayBuilder();
    }

    public JsonArrayBuilder withItems(JsonElement... items) {
        return withItems(Arrays.asList(items));
    }

    public JsonArrayBuilder withItems(String... items) {
        List itemsAsJsonElement = stream(items)
                .map(JsonPrimitive::new)
                .collect(toList());
        return withItems(itemsAsJsonElement);
    }

    public JsonArrayBuilder withItems(Collection items) {
        this.items = items;
        return this;
    }

    public JsonArray build() {
        JsonArray jsonArray = new JsonArray();
        for (JsonElement item : items) {
            jsonArray.add(item);
        }
        return jsonArray;
    }

    public String buildAsString() {
        return build().toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy