more convenience for builders

This commit is contained in:
Jörg Prante 2021-10-15 21:57:12 +02:00
parent b6eddd5a13
commit b5491330a5
3 changed files with 31 additions and 3 deletions

View file

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

View file

@ -28,6 +28,14 @@ public class JsonBuilder implements Builder {
this.state = new State(null, 0, Structure.MAP, true); 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 @Override
public Builder beginCollection() throws IOException { public Builder beginCollection() throws IOException {
this.state = new State(state, state.level + 1, Structure.COLLECTION, true); this.state = new State(state, state.level + 1, Structure.COLLECTION, true);

View file

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