TwoColumnOutputpublic final class TwoColumnOutput extends Object Class that takes a combined output destination and provides two
output writers, one of which ends up writing to the left column and
one which goes on the right. |
Fields Summary |
---|
private final Writer | outnon-null; underlying writer for final output | private final int | leftWidth> 0; the left column width | private final StringBuffer | leftBufnon-null; pending left column output | private final StringBuffer | rightBufnon-null; pending right column output | private final IndentingWriter | leftColumnnon-null; left column writer | private final IndentingWriter | rightColumnnon-null; right column writer |
Constructors Summary |
---|
public TwoColumnOutput(Writer out, int leftWidth, int rightWidth, String spacer)Constructs an instance.
if (out == null) {
throw new NullPointerException("out == null");
}
if (leftWidth < 1) {
throw new IllegalArgumentException("leftWidth < 1");
}
if (rightWidth < 1) {
throw new IllegalArgumentException("rightWidth < 1");
}
if (spacer == null) {
throw new NullPointerException("spacer == null");
}
StringWriter leftWriter = new StringWriter(1000);
StringWriter rightWriter = new StringWriter(1000);
this.out = out;
this.leftWidth = leftWidth;
this.leftBuf = leftWriter.getBuffer();
this.rightBuf = rightWriter.getBuffer();
this.leftColumn = new IndentingWriter(leftWriter, leftWidth);
this.rightColumn =
new IndentingWriter(rightWriter, rightWidth, spacer);
| public TwoColumnOutput(OutputStream out, int leftWidth, int rightWidth, String spacer)Constructs an instance.
this(new OutputStreamWriter(out), leftWidth, rightWidth, spacer);
|
Methods Summary |
---|
private static void | appendNewlineIfNecessary(java.lang.StringBuffer buf, java.io.Writer out)Appends a newline to the given buffer via the given writer, but
only if it isn't empty and doesn't already end with one.
int len = buf.length();
if ((len != 0) && (buf.charAt(len - 1) != '\n")) {
out.write('\n");
}
| public void | flush()Flushes the output. If there are more lines of pending output in one
column, then the other column will get filled with blank lines.
try {
appendNewlineIfNecessary(leftBuf, leftColumn);
appendNewlineIfNecessary(rightBuf, rightColumn);
outputFullLines();
flushLeft();
flushRight();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
| private void | flushLeft()Flushes the left column buffer, printing it and clearing the buffer.
If the buffer is already empty, this does nothing.
appendNewlineIfNecessary(leftBuf, leftColumn);
while (leftBuf.length() != 0) {
rightColumn.write('\n");
outputFullLines();
}
| private void | flushRight()Flushes the right column buffer, printing it and clearing the buffer.
If the buffer is already empty, this does nothing.
appendNewlineIfNecessary(rightBuf, rightColumn);
while (rightBuf.length() != 0) {
leftColumn.write('\n");
outputFullLines();
}
| public java.io.Writer | getLeft()Gets the writer to use to write to the left column.
return leftColumn;
| public java.io.Writer | getRight()Gets the writer to use to write to the right column.
return rightColumn;
| private void | outputFullLines()Outputs to the final destination as many full line pairs as
there are in the pending output, removing those lines from
their respective buffers. This method terminates when at
least one of the two column buffers is empty.
for (;;) {
int leftLen = leftBuf.indexOf("\n");
if (leftLen < 0) {
return;
}
int rightLen = rightBuf.indexOf("\n");
if (rightLen < 0) {
return;
}
if (leftLen != 0) {
out.write(leftBuf.substring(0, leftLen));
}
if (rightLen != 0) {
writeSpaces(out, leftWidth - leftLen);
out.write(rightBuf.substring(0, rightLen));
}
out.write('\n");
leftBuf.delete(0, leftLen + 1);
rightBuf.delete(0, rightLen + 1);
}
| public static java.lang.String | toString(java.lang.String s1, int width1, java.lang.String spacer, java.lang.String s2, int width2)Turns the given two strings (with widths) and spacer into a formatted
two-column string.
int len1 = s1.length();
int len2 = s2.length();
StringWriter sw = new StringWriter((len1 + len2) * 3);
TwoColumnOutput twoOut =
new TwoColumnOutput(sw, width1, width2, spacer);
try {
twoOut.getLeft().write(s1);
twoOut.getRight().write(s2);
} catch (IOException ex) {
throw new RuntimeException("shouldn't happen", ex);
}
twoOut.flush();
return sw.toString();
| private static void | writeSpaces(java.io.Writer out, int amt)Writes the given number of spaces to the given writer.
while (amt > 0) {
out.write(' ");
amt--;
}
|
|