public abstract class CodeWriter
extends java.lang.Object
implements java.lang.AutoCloseable
CodeWriter
is a pretty-printing engine. It formats
structured text onto an output stream o
in the minimum number
of lines, while keeping the width of the output within width
characters if possible. Newlines occur in the output only at places where a
line break is permitted by the use of allowBreak
and
unifiedBreak
.
Line breaks can have different levels, which is useful for implementing
things like "miser mode" layout.Constructor and Description |
---|
CodeWriter() |
Modifier and Type | Method and Description |
---|---|
void |
allowBreak(int n)
The most common use of "allowBreak": level 1, with an alternative of a
single space.
|
abstract void |
allowBreak(int n,
int level,
java.lang.String alt,
int altlen)
Insert a break (an optional newline).
|
void |
allowBreak(int n,
java.lang.String alt) |
abstract void |
begin(int n)
Start a new block with a relative indentation of
n
characters. |
abstract void |
close()
Flush all formatted text, reset formatter state, and
close the underlying writer.
|
abstract void |
end()
Terminate the most recent outstanding
begin . |
abstract boolean |
flush()
Flush all formatted text to the underlying writer.
|
abstract boolean |
flush(boolean format)
Like
flush , but passing format=false
causes output to be generated in the fastest way possible, with
all breaks broken. |
void |
newline()
Force a newline.
|
void |
newline(int n)
Like newline(), but forces a newline with a specified indentation.
|
abstract void |
newline(int n,
int level)
newline with a specified indentation and level.
|
abstract java.lang.String |
toString()
Return a readable representation of all the structured input given to
the CodeWriter since the last flush.
|
void |
unifiedBreak(int n)
The most common use of "unifiedBreak": level 1, with an alternative of a
single space.
|
abstract void |
unifiedBreak(int n,
int level,
java.lang.String alt,
int altlen)
Insert a unified break.
|
abstract void |
write(java.lang.String s)
Print the string
s verbatim on the output stream. |
abstract void |
write(java.lang.String s,
int length)
Print the string
s on the output stream. |
public abstract void write(java.lang.String s)
s
verbatim on the output stream.s
- the string to print.public abstract void write(java.lang.String s, int length)
s
on the output stream. Pretend that it
has width length
even if it has a different number of
characters. This is useful when the string contains escape sequences,
HTML character entity references, etc.s
- length
- public abstract void begin(int n)
n
characters.public abstract void end()
begin
.public abstract void allowBreak(int n, int level, java.lang.String alt, int altlen)
n
- indentation relative to the current block if the newline is
inserted. Requires: n >= 0level
- the level of the break. Requires: level >= 0alt
- if no newline is inserted, the string alt
is
output instead. Requires: alt != nullaltlen
- the length of 'alt' in characterspublic abstract void unifiedBreak(int n, int level, java.lang.String alt, int altlen)
allowBreak
, but unified breaks should also break if any break of
the same level in the same block is broken, whereas ordinary breaks do
not necessarily break in this case. That is, unified breaks act as if
they were slightly lower level than other breaks of the same level
(including other unified breaks!).n
- the relative indentationlevel
- the level of the breakalt
- the alternative textaltlen
- the length in characters that 'alt' will be treated as taking up.allowBreak(int, int, java.lang.String, int)
public void unifiedBreak(int n)
n
- the indentation relative to the current block.public void allowBreak(int n)
n
- the indentation relative to the current block.public void allowBreak(int n, java.lang.String alt)
public void newline()
public void newline(int n)
public abstract void newline(int n, int level)
public abstract boolean flush() throws java.io.IOException
java.io.IOException
public abstract void close() throws java.io.IOException
close
in interface java.lang.AutoCloseable
java.io.IOException
public abstract boolean flush(boolean format) throws java.io.IOException
flush
, but passing format=false
causes output to be generated in the fastest way possible, with
all breaks broken.format
- whether to pretty-print the outputjava.io.IOException
public abstract java.lang.String toString()
toString
in class java.lang.Object