mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +00:00
Created TomlWriter.Builder, so TomlWriter and IndentationPolicy can be
immutable
This commit is contained in:
parent
0f42d37f60
commit
56be048ab1
7 changed files with 80 additions and 93 deletions
|
@ -98,7 +98,6 @@ class DateConverter implements ValueConverter, ValueWriter {
|
|||
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:m:ss");
|
||||
}
|
||||
dateFormat.setTimeZone(context.getTomlWriter().getTimeZone());
|
||||
|
||||
context.write(dateFormat.format(value));
|
||||
|
||||
if (customDateFormat == null) {
|
||||
|
|
|
@ -17,9 +17,7 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
|
|||
Collection values = normalize(value);
|
||||
|
||||
context.write('[');
|
||||
if (!context.getTomlWriter().wantTerseArrays()) {
|
||||
context.write(' ');
|
||||
}
|
||||
context.writeArrayDelimiterPadding();
|
||||
|
||||
boolean first = true;
|
||||
ValueWriter firstWriter = null;
|
||||
|
@ -43,9 +41,7 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
|
|||
WRITERS.write(elem, context);
|
||||
}
|
||||
|
||||
if (!context.getTomlWriter().wantTerseArrays()) {
|
||||
context.write(' ');
|
||||
}
|
||||
context.writeArrayDelimiterPadding();
|
||||
context.write(']');
|
||||
}
|
||||
|
||||
|
|
|
@ -34,15 +34,52 @@ import java.util.TimeZone;
|
|||
*/
|
||||
public class TomlWriter {
|
||||
|
||||
private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy();
|
||||
private boolean wantTerseArraysValue = false;
|
||||
public static class Builder {
|
||||
private int keyIndentation;
|
||||
private int tableIndentation;
|
||||
private int arrayDelimiterPadding = 0;
|
||||
|
||||
public TomlWriter.Builder indentValuesBy(int spaces) {
|
||||
this.keyIndentation = spaces;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public TomlWriter.Builder indentTablesBy(int spaces) {
|
||||
this.tableIndentation = spaces;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param spaces number of spaces to put between opening square bracket and first item and between closing square bracket and last item
|
||||
* @return this TomlWriter.Builder instance
|
||||
*/
|
||||
public TomlWriter.Builder padArrayDelimitersBy(int spaces) {
|
||||
this.arrayDelimiterPadding = spaces;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public TomlWriter build() {
|
||||
return new TomlWriter(keyIndentation, tableIndentation, arrayDelimiterPadding);
|
||||
}
|
||||
}
|
||||
|
||||
private final WriterIndentationPolicy indentationPolicy;
|
||||
private GregorianCalendar calendar = new GregorianCalendar();
|
||||
private DateFormat customDateFormat = null;
|
||||
|
||||
/**
|
||||
* Creates a TomlWriter instance.
|
||||
*/
|
||||
public TomlWriter() {}
|
||||
public TomlWriter() {
|
||||
this(0, 0, 0);
|
||||
}
|
||||
|
||||
private TomlWriter(int keyIndentation, int tableIndentation, int arrayDelimiterPadding) {
|
||||
this.indentationPolicy = new WriterIndentationPolicy(keyIndentation, tableIndentation, arrayDelimiterPadding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an Object into TOML String.
|
||||
|
@ -105,50 +142,6 @@ public class TomlWriter {
|
|||
return indentationPolicy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link WriterIndentationPolicy} for this writer.
|
||||
*
|
||||
* If unset, the default policy (no indentation) is used.
|
||||
*
|
||||
* @param indentationPolicy the new policy
|
||||
* @return this TomlWriter instance
|
||||
*/
|
||||
public TomlWriter setIndentationPolicy(WriterIndentationPolicy indentationPolicy) {
|
||||
this.indentationPolicy = indentationPolicy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Control whitespace in arrays in the TOML output.</p>
|
||||
*
|
||||
* <p>Terse arrays = false (default):</p>
|
||||
*
|
||||
* <pre><code>
|
||||
* a = [ 1, 2, 3 ]
|
||||
* </code></pre>
|
||||
*
|
||||
* <p>Terse arrays = true:</p>
|
||||
*
|
||||
* <pre><code>
|
||||
* a = [1,2,3]
|
||||
* </code></pre>
|
||||
*
|
||||
* @param value terse arrays setting
|
||||
* @return this TomlWriter instance
|
||||
*/
|
||||
public TomlWriter wantTerseArrays(boolean value) {
|
||||
this.wantTerseArraysValue = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current array whitespace policy
|
||||
* @return the current policy
|
||||
*/
|
||||
public boolean wantTerseArrays() {
|
||||
return wantTerseArraysValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link TimeZone} used when formatting datetimes.
|
||||
*
|
||||
|
|
|
@ -93,6 +93,12 @@ class WriterContext {
|
|||
}
|
||||
}
|
||||
|
||||
void writeArrayDelimiterPadding() {
|
||||
for (int i = 0; i < tomlWriter.getIndentationPolicy().getArrayDelimiterPadding(); i++) {
|
||||
write(' ');
|
||||
}
|
||||
}
|
||||
|
||||
void indent() {
|
||||
if (!key.isEmpty()) {
|
||||
write(currentFieldIndent);
|
||||
|
|
|
@ -5,37 +5,26 @@ package com.moandjiezana.toml;
|
|||
*
|
||||
* The default policy is to not indent.
|
||||
*/
|
||||
public class WriterIndentationPolicy {
|
||||
private int tableIndent = 0;
|
||||
private int keyValueIndent = 0;
|
||||
class WriterIndentationPolicy {
|
||||
private final int tableIndent;
|
||||
private final int keyValueIndent;
|
||||
private final int arrayDelimiterPadding;
|
||||
|
||||
public int getTableIndent() {
|
||||
WriterIndentationPolicy(int keyIndentation, int tableIndentation, int arrayDelimiterPadding) {
|
||||
this.keyValueIndent = keyIndentation;
|
||||
this.tableIndent = tableIndentation;
|
||||
this.arrayDelimiterPadding = arrayDelimiterPadding;
|
||||
}
|
||||
|
||||
int getTableIndent() {
|
||||
return tableIndent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number of spaces a nested table name is indented.
|
||||
*
|
||||
* @param tableIndent number of spaces to indent
|
||||
* @return this WriterIndentationPolicy instance
|
||||
*/
|
||||
public WriterIndentationPolicy setTableIndent(int tableIndent) {
|
||||
this.tableIndent = tableIndent;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getKeyValueIndent() {
|
||||
int getKeyValueIndent() {
|
||||
return keyValueIndent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number of spaces key/value pairs within a table are indented.
|
||||
*
|
||||
* @param keyValueIndent number of spaces to indent
|
||||
* @return this WriterIndentationPolicy instance
|
||||
*/
|
||||
public WriterIndentationPolicy setKeyValueIndent(int keyValueIndent) {
|
||||
this.keyValueIndent = keyValueIndent;
|
||||
return this;
|
||||
int getArrayDelimiterPadding() {
|
||||
return arrayDelimiterPadding;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -114,7 +114,7 @@ public class BurntSushiValidEncoderTest {
|
|||
public void table_array_nest_modified() throws Exception {
|
||||
// Modified to remove stray spaces in the expected TOML
|
||||
runEncoder("table-array-nest-modified",
|
||||
new TomlWriter().setIndentationPolicy(new WriterIndentationPolicy().setTableIndent(2)));
|
||||
new TomlWriter.Builder().indentTablesBy(2).build());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -126,8 +126,8 @@ public class BurntSushiValidEncoderTest {
|
|||
|
||||
private void runEncoder(String testName) {
|
||||
runEncoder(testName,
|
||||
new TomlWriter().
|
||||
wantTerseArrays(true).
|
||||
new TomlWriter.Builder().
|
||||
build().
|
||||
setTimeZone(TimeZone.getTimeZone("UTC")).
|
||||
setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")));
|
||||
}
|
||||
|
|
|
@ -123,9 +123,10 @@ public class TomlWriterTest {
|
|||
|
||||
@Test
|
||||
public void should_follow_indentation_policy_of_indented_values() {
|
||||
String output = new TomlWriter().
|
||||
setIndentationPolicy(new WriterIndentationPolicy().setKeyValueIndent(2)).
|
||||
write(buildNestedMap());
|
||||
String output = new TomlWriter.Builder().
|
||||
indentValuesBy(2).
|
||||
build().
|
||||
write(buildNestedMap());
|
||||
String expected = "aBoolean = true\n\n" +
|
||||
"[aMap]\n" +
|
||||
" foo = 1\n" +
|
||||
|
@ -140,9 +141,10 @@ public class TomlWriterTest {
|
|||
|
||||
@Test
|
||||
public void should_follow_indentation_policy_of_indented_tables() {
|
||||
String output = new TomlWriter().
|
||||
setIndentationPolicy(new WriterIndentationPolicy().setTableIndent(2)).
|
||||
write(buildNestedMap());
|
||||
String output = new TomlWriter.Builder().
|
||||
indentTablesBy(2).
|
||||
build().
|
||||
write(buildNestedMap());
|
||||
String expected = "aBoolean = true\n\n" +
|
||||
"[aMap]\n" +
|
||||
"foo = 1\n" +
|
||||
|
@ -157,9 +159,11 @@ public class TomlWriterTest {
|
|||
|
||||
@Test
|
||||
public void should_follow_indentation_policy_of_indented_tables_and_values() {
|
||||
String output = new TomlWriter().
|
||||
setIndentationPolicy(new WriterIndentationPolicy().setTableIndent(2).setKeyValueIndent(2)).
|
||||
write(buildNestedMap());
|
||||
String output = new TomlWriter.Builder().
|
||||
indentValuesBy(2).
|
||||
indentTablesBy(2).
|
||||
build().
|
||||
write(buildNestedMap());
|
||||
String expected = "aBoolean = true\n\n" +
|
||||
"[aMap]\n" +
|
||||
" foo = 1\n" +
|
||||
|
@ -202,7 +206,7 @@ public class TomlWriterTest {
|
|||
}
|
||||
ArrayTest arrayTest = new ArrayTest();
|
||||
|
||||
String output = new TomlWriter().write(arrayTest);
|
||||
String output = new TomlWriter.Builder().padArrayDelimitersBy(1).build().write(arrayTest);
|
||||
String expected = "array = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]\n";
|
||||
assertEquals(expected, output);
|
||||
}
|
||||
|
@ -216,7 +220,7 @@ public class TomlWriterTest {
|
|||
o.aList.add(1);
|
||||
o.aList.add(2);
|
||||
|
||||
assertEquals("aList = [ 1, 2 ]\n", new TomlWriter().write(o));
|
||||
assertEquals("aList = [ 1, 2 ]\n", new TomlWriter.Builder().padArrayDelimitersBy(1).build().write(o));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in a new issue