Printer now supports any appendable + separated lists
This commit is contained in:
@@ -18,25 +18,38 @@ package org.jetbrains.jet.utils;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class Printer {
|
public class Printer {
|
||||||
private static final String INDENTATION_UNIT = " ";
|
private static final String INDENTATION_UNIT = " ";
|
||||||
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
|
||||||
|
|
||||||
private final StringBuilder out;
|
private final Appendable out;
|
||||||
private final int maxBlankLines;
|
private final int maxBlankLines;
|
||||||
|
|
||||||
private String indent = "";
|
private String indent = "";
|
||||||
private int blankLineCountIncludingCurrent = 0;
|
private int blankLineCountIncludingCurrent = 0;
|
||||||
|
|
||||||
public Printer(@NotNull StringBuilder out) {
|
public Printer(@NotNull Appendable out) {
|
||||||
this(out, Integer.MAX_VALUE);
|
this(out, Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Printer(@NotNull StringBuilder out, int maxBlankLines) {
|
public Printer(@NotNull Appendable out, int maxBlankLines) {
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.maxBlankLines = maxBlankLines;
|
this.maxBlankLines = maxBlankLines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void append(Object o) {
|
||||||
|
try {
|
||||||
|
out.append(o.toString());
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Printer println(Object... objects) {
|
public Printer println(Object... objects) {
|
||||||
print(objects);
|
print(objects);
|
||||||
@@ -48,13 +61,13 @@ public class Printer {
|
|||||||
private void printLineSeparator() {
|
private void printLineSeparator() {
|
||||||
if (blankLineCountIncludingCurrent <= maxBlankLines) {
|
if (blankLineCountIncludingCurrent <= maxBlankLines) {
|
||||||
blankLineCountIncludingCurrent++;
|
blankLineCountIncludingCurrent++;
|
||||||
out.append(LINE_SEPARATOR);
|
append(LINE_SEPARATOR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Printer print(Object... objects) {
|
public Printer print(Object... objects) {
|
||||||
out.append(indent);
|
append(indent);
|
||||||
printWithNoIndent(objects);
|
printWithNoIndent(objects);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@@ -64,7 +77,7 @@ public class Printer {
|
|||||||
public Printer printWithNoIndent(Object... objects) {
|
public Printer printWithNoIndent(Object... objects) {
|
||||||
for (Object object : objects) {
|
for (Object object : objects) {
|
||||||
blankLineCountIncludingCurrent = 0;
|
blankLineCountIncludingCurrent = 0;
|
||||||
out.append(object);
|
append(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@@ -95,4 +108,26 @@ public class Printer {
|
|||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Printer separated(Object separator, Object... items) {
|
||||||
|
for (int i = 0; i < items.length; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
printlnWithNoIndent(separator);
|
||||||
|
}
|
||||||
|
printlnWithNoIndent(items[i]);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Printer separated(Object separator, Collection<?> items) {
|
||||||
|
for (Iterator<?> iterator = items.iterator(); iterator.hasNext(); ) {
|
||||||
|
printlnWithNoIndent(iterator.next());
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
printlnWithNoIndent(separator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user