render option to configure indentation characters instead of hardcoded 4 spaces.

This commit is contained in:
aalleexxeeii 2016-07-02 21:28:57 +03:00
parent a8d691ad74
commit 07259c7e96
2 changed files with 37 additions and 19 deletions

View File

@ -17,17 +17,21 @@ package com.typesafe.config;
* </pre> * </pre>
*/ */
public final class ConfigRenderOptions { public final class ConfigRenderOptions {
private final static String DEFAULT_INDENTATION = " ";
private final boolean originComments; private final boolean originComments;
private final boolean comments; private final boolean comments;
private final boolean formatted; private final boolean formatted;
private final boolean json; private final boolean json;
private final String indentation;
private ConfigRenderOptions(boolean originComments, boolean comments, boolean formatted, private ConfigRenderOptions(boolean originComments, boolean comments, boolean formatted,
boolean json) { boolean json, String indentation) {
this.originComments = originComments; this.originComments = originComments;
this.comments = comments; this.comments = comments;
this.formatted = formatted; this.formatted = formatted;
this.json = json; this.json = json;
this.indentation = indentation;
} }
/** /**
@ -38,7 +42,7 @@ public final class ConfigRenderOptions {
* @return the default render options * @return the default render options
*/ */
public static ConfigRenderOptions defaults() { public static ConfigRenderOptions defaults() {
return new ConfigRenderOptions(true, true, true, true); return new ConfigRenderOptions(true, true, true, true, DEFAULT_INDENTATION);
} }
/** /**
@ -48,7 +52,7 @@ public final class ConfigRenderOptions {
* @return the concise render options * @return the concise render options
*/ */
public static ConfigRenderOptions concise() { public static ConfigRenderOptions concise() {
return new ConfigRenderOptions(false, false, false, true); return new ConfigRenderOptions(false, false, false, true, DEFAULT_INDENTATION);
} }
/** /**
@ -64,7 +68,7 @@ public final class ConfigRenderOptions {
if (value == comments) if (value == comments)
return this; return this;
else else
return new ConfigRenderOptions(originComments, value, formatted, json); return new ConfigRenderOptions(originComments, value, formatted, json, indentation);
} }
/** /**
@ -97,7 +101,7 @@ public final class ConfigRenderOptions {
if (value == originComments) if (value == originComments)
return this; return this;
else else
return new ConfigRenderOptions(value, comments, formatted, json); return new ConfigRenderOptions(value, comments, formatted, json, indentation);
} }
/** /**
@ -122,7 +126,7 @@ public final class ConfigRenderOptions {
if (value == formatted) if (value == formatted)
return this; return this;
else else
return new ConfigRenderOptions(originComments, comments, value, json); return new ConfigRenderOptions(originComments, comments, value, json, indentation);
} }
/** /**
@ -150,7 +154,7 @@ public final class ConfigRenderOptions {
if (value == json) if (value == json)
return this; return this;
else else
return new ConfigRenderOptions(originComments, comments, formatted, value); return new ConfigRenderOptions(originComments, comments, formatted, value, indentation);
} }
/** /**
@ -163,6 +167,29 @@ public final class ConfigRenderOptions {
return json; return json;
} }
/**
* Returns options with indentation changed. The default value is 4 spaces.
*
* @param value
* characters to use for one level of indentation
* @return options with requested indentation setting
*/
public ConfigRenderOptions setIndentation(String value) {
if (value.equals(indentation))
return this;
else
return new ConfigRenderOptions(originComments, comments, formatted, json, value);
}
/**
* Returns indentation characters.
*
* @return characters used for one level of indentation
*/
public String getIndentation() {
return indentation;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder("ConfigRenderOptions("); StringBuilder sb = new StringBuilder("ConfigRenderOptions(");

View File

@ -3,18 +3,9 @@
*/ */
package com.typesafe.config.impl; package com.typesafe.config.impl;
import java.util.ArrayList; import com.typesafe.config.*;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.typesafe.config.ConfigException; import java.util.*;
import com.typesafe.config.ConfigMergeable;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.ConfigOrigin;
import com.typesafe.config.ConfigRenderOptions;
import com.typesafe.config.ConfigValue;
/** /**
* *
@ -322,7 +313,7 @@ abstract class AbstractConfigValue implements ConfigValue, MergeableValue {
if (options.getFormatted()) { if (options.getFormatted()) {
int remaining = indent; int remaining = indent;
while (remaining > 0) { while (remaining > 0) {
sb.append(" "); sb.append(options.getIndentation());
--remaining; --remaining;
} }
} }