more convenience for builders

main
Jörg Prante 3 years ago
parent b6eddd5a13
commit b5491330a5

@ -18,11 +18,19 @@ public interface Builder {
Builder buildCollection(Collection<Object> collection) throws IOException;
Builder buildKey(CharSequence charSequence) throws IOException;
Builder buildKey(CharSequence key) throws IOException;
Builder buildValue(Object object) throws IOException;
Builder buildValue(Object value) throws IOException;
Builder buildNull() throws IOException;
default Builder buildIfNotNull(CharSequence key, Object value) throws IOException {
if (value != null){
buildKey(key);
buildValue(value);
}
return this;
}
String build();
}

@ -28,6 +28,14 @@ public class JsonBuilder implements Builder {
this.state = new State(null, 0, Structure.MAP, true);
}
public static JsonBuilder builder() {
return new JsonBuilder();
}
public static JsonBuilder builder(Writer writer) {
return new JsonBuilder(writer);
}
@Override
public Builder beginCollection() throws IOException {
this.state = new State(state, state.level + 1, Structure.COLLECTION, true);

@ -3,8 +3,8 @@ package org.xbib.datastructures.yaml.tiny;
import org.xbib.datastructures.api.Builder;
import org.xbib.datastructures.api.ByteSizeValue;
import org.xbib.datastructures.api.TimeValue;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.time.Instant;
@ -20,6 +20,10 @@ public class YamlBuilder implements Builder {
private State state;
public YamlBuilder() {
this(new StringWriter());
}
public YamlBuilder(Writer writer) {
this(writer, 2);
}
@ -30,6 +34,14 @@ public class YamlBuilder implements Builder {
this.state = new State(null, 0, Structure.MAP, false);
}
public static YamlBuilder builder() {
return new YamlBuilder();
}
public static YamlBuilder builder(Writer writer) {
return new YamlBuilder(writer);
}
@Override
public Builder beginCollection() {
this.state = new State(state, state.level + 1, Structure.COLLECTION, true);

Loading…
Cancel
Save