minor: allow specifying indentation unit in Printer
This commit is contained in:
committed by
Dmitry Petrov
parent
1600037625
commit
520a3133bc
@@ -23,13 +23,14 @@ import java.util.Collection;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class Printer {
|
public class Printer {
|
||||||
private static final String INDENTATION_UNIT = " ";
|
private static final String DEFAULT_INDENTATION_UNIT = " ";
|
||||||
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||||
|
|
||||||
protected final Appendable out;
|
protected final Appendable out;
|
||||||
private final int maxBlankLines;
|
private final int maxBlankLines;
|
||||||
|
|
||||||
private String indent = "";
|
private String indent = "";
|
||||||
|
private final String indentUnit;
|
||||||
private int blankLineCountIncludingCurrent = 0;
|
private int blankLineCountIncludingCurrent = 0;
|
||||||
private boolean withholdIndentOnce = false;
|
private boolean withholdIndentOnce = false;
|
||||||
|
|
||||||
@@ -37,9 +38,18 @@ public class Printer {
|
|||||||
this(out, Integer.MAX_VALUE);
|
this(out, Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Printer(@NotNull Appendable out, @NotNull String indentUnit) {
|
||||||
|
this(out, Integer.MAX_VALUE, indentUnit);
|
||||||
|
}
|
||||||
|
|
||||||
public Printer(@NotNull Appendable out, int maxBlankLines) {
|
public Printer(@NotNull Appendable out, int maxBlankLines) {
|
||||||
|
this(out, maxBlankLines, DEFAULT_INDENTATION_UNIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Printer(@NotNull Appendable out, int maxBlankLines, @NotNull String indentUnit) {
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.maxBlankLines = maxBlankLines;
|
this.maxBlankLines = maxBlankLines;
|
||||||
|
this.indentUnit = indentUnit;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void append(Object o) {
|
private void append(Object o) {
|
||||||
@@ -109,18 +119,18 @@ public class Printer {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Printer pushIndent() {
|
public Printer pushIndent() {
|
||||||
indent += INDENTATION_UNIT;
|
indent += indentUnit;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Printer popIndent() {
|
public Printer popIndent() {
|
||||||
if (indent.length() < INDENTATION_UNIT.length()) {
|
if (indent.length() < indentUnit.length()) {
|
||||||
throw new IllegalStateException("No indentation to pop");
|
throw new IllegalStateException("No indentation to pop");
|
||||||
}
|
}
|
||||||
|
|
||||||
indent = indent.substring(INDENTATION_UNIT.length());
|
indent = indent.substring(indentUnit.length());
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user