seatsio.json.JsonArrayBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of seatsio-java Show documentation
Show all versions of seatsio-java Show documentation
The official seats.io Java client library
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();
}
}