From 56be048ab1657bdddcb30a13d3db5b98afb84b08 Mon Sep 17 00:00:00 2001 From: "moandji.ezana" Date: Tue, 30 Jun 2015 11:37:22 +0200 Subject: [PATCH] Created TomlWriter.Builder, so TomlWriter and IndentationPolicy can be immutable --- .../com/moandjiezana/toml/DateConverter.java | 1 - .../toml/PrimitiveArrayValueWriter.java | 8 +- .../com/moandjiezana/toml/TomlWriter.java | 87 +++++++++---------- .../com/moandjiezana/toml/WriterContext.java | 6 ++ .../toml/WriterIndentationPolicy.java | 39 +++------ .../toml/BurntSushiValidEncoderTest.java | 6 +- .../com/moandjiezana/toml/TomlWriterTest.java | 26 +++--- 7 files changed, 80 insertions(+), 93 deletions(-) diff --git a/src/main/java/com/moandjiezana/toml/DateConverter.java b/src/main/java/com/moandjiezana/toml/DateConverter.java index 0c8f375..eb13397 100644 --- a/src/main/java/com/moandjiezana/toml/DateConverter.java +++ b/src/main/java/com/moandjiezana/toml/DateConverter.java @@ -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) { diff --git a/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java b/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java index b9d8018..adcf05a 100644 --- a/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java +++ b/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java @@ -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(']'); } diff --git a/src/main/java/com/moandjiezana/toml/TomlWriter.java b/src/main/java/com/moandjiezana/toml/TomlWriter.java index f0efaff..ecd894f 100644 --- a/src/main/java/com/moandjiezana/toml/TomlWriter.java +++ b/src/main/java/com/moandjiezana/toml/TomlWriter.java @@ -33,16 +33,53 @@ import java.util.TimeZone; * */ public class TomlWriter { + + 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; + } - private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy(); - private boolean wantTerseArraysValue = false; + 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; - } - - /** - *

Control whitespace in arrays in the TOML output.

- * - *

Terse arrays = false (default):

- * - *

-   *   a = [ 1, 2, 3 ]
-   * 
- * - *

Terse arrays = true:

- * - *

-   *   a = [1,2,3]
-   * 
- * - * @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. * diff --git a/src/main/java/com/moandjiezana/toml/WriterContext.java b/src/main/java/com/moandjiezana/toml/WriterContext.java index 239ffb9..ef59a60 100644 --- a/src/main/java/com/moandjiezana/toml/WriterContext.java +++ b/src/main/java/com/moandjiezana/toml/WriterContext.java @@ -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); diff --git a/src/main/java/com/moandjiezana/toml/WriterIndentationPolicy.java b/src/main/java/com/moandjiezana/toml/WriterIndentationPolicy.java index b8587c5..4a41370 100644 --- a/src/main/java/com/moandjiezana/toml/WriterIndentationPolicy.java +++ b/src/main/java/com/moandjiezana/toml/WriterIndentationPolicy.java @@ -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; } } diff --git a/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java b/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java index 121cdc8..1e0ae75 100644 --- a/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java +++ b/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java @@ -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'"))); } diff --git a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java index 14614c7..56e5068 100644 --- a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java @@ -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