cs132.util
Class IndentPrinter

java.lang.Object
  extended by java.io.Writer
      extended by cs132.util.IndentPrinter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public final class IndentPrinter
extends Writer
implements Appendable

Printer that manages indentation. Helps when pretty-printing a hierarchy. For example:

 IndentPrinter p = new IndentPrinter(out, "  ");
 p.println("Root 1");
 p.indent();
 p.println("Child 1");
 p.println("Child 2");
 p.dedent();
 p.println("Root 2");
 

Yields:

 Root 1
   Child 1
   Child 2
 Root 2
 

This class never buffers any data, so you don't need to use flush() to force anything out. Calling flush() will just flush on the underlying Writer.


Field Summary
static String DefaultIndent
          Two spaces.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
IndentPrinter(Writer out)
          Equivalent to: IndentPrinter(out, IndentPrinter.DefaultIndent).
IndentPrinter(Writer out, String indent)
          Equivalent to: IndentPrinter(out, indent, "").
IndentPrinter(Writer out, String indent, String prefix)
          Equivalent to: IndentPrinter(out, indent, prefix, true).
IndentPrinter(Writer out, String indent, String prefix, boolean freshLine)
           
 
Method Summary
 IndentPrinter append(char c)
           
 IndentPrinter append(CharSequence v)
           
 IndentPrinter append(CharSequence v, int start, int end)
           
 void close()
          Closes the underlying stream.
 void dedent()
          Decrease the indentation level by one.
 void dedent(int numIndents)
          Decrease the indentation level by 'numIndents' indentations.
 void flush()
          Flushes the underlying stream.
 void indent()
          Increase the indentation level by one.
 void indent(int numIndents)
          Increase the indentation level by 'numIndents' indentations.
 void print(boolean v)
           
 void print(char v)
           
 void print(char[] v)
           
 void print(CharSequence v)
           
 void print(double v)
           
 void print(float v)
           
 void print(int v)
           
 void print(long v)
           
 void print(Object v)
           
 void print(String v)
           
 void println()
           
 void println(boolean v)
           
 void println(char v)
           
 void println(char[] v)
           
 void println(CharSequence v)
           
 void println(double v)
           
 void println(float v)
           
 void println(int v)
           
 void println(long v)
           
 void println(Object v)
           
 void println(String v)
           
 void write(char c)
           
 void write(char[] buf)
           
 void write(char[] buf, int off, int len)
           
 void write(String s)
           
 void write(String s, int off, int len)
           
 
Methods inherited from class java.io.Writer
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DefaultIndent

public static final String DefaultIndent
Two spaces.

See Also:
Constant Field Values
Constructor Detail

IndentPrinter

public IndentPrinter(Writer out,
                     String indent,
                     String prefix,
                     boolean freshLine)
Parameters:
indent - The string to add to the prefix for each additional indent level.
prefix - The indentation to prefix to the beginning of each line.
freshLine - If we're starting on a "fresh" line, we need to add indentation to the very first thing printed. If false, then we'll only start adding indentation after the first 'println'.

IndentPrinter

public IndentPrinter(Writer out,
                     String indent,
                     String prefix)
Equivalent to: IndentPrinter(out, indent, prefix, true).


IndentPrinter

public IndentPrinter(Writer out,
                     String indent)
Equivalent to: IndentPrinter(out, indent, "").


IndentPrinter

public IndentPrinter(Writer out)
Equivalent to: IndentPrinter(out, IndentPrinter.DefaultIndent).

Method Detail

indent

public void indent(int numIndents)
Increase the indentation level by 'numIndents' indentations.


indent

public void indent()
Increase the indentation level by one.


dedent

public void dedent(int numIndents)
Decrease the indentation level by 'numIndents' indentations.


dedent

public void dedent()
Decrease the indentation level by one.


print

public void print(String v)
           throws IOException
Throws:
IOException

print

public void print(char[] v)
           throws IOException
Throws:
IOException

print

public void print(Object v)
           throws IOException
Throws:
IOException

print

public void print(char v)
           throws IOException
Throws:
IOException

print

public void print(int v)
           throws IOException
Throws:
IOException

print

public void print(long v)
           throws IOException
Throws:
IOException

print

public void print(double v)
           throws IOException
Throws:
IOException

print

public void print(float v)
           throws IOException
Throws:
IOException

print

public void print(boolean v)
           throws IOException
Throws:
IOException

print

public void print(CharSequence v)
           throws IOException
Throws:
IOException

append

public IndentPrinter append(CharSequence v)
                     throws IOException
Specified by:
append in interface Appendable
Overrides:
append in class Writer
Throws:
IOException

append

public IndentPrinter append(CharSequence v,
                            int start,
                            int end)
                     throws IOException
Specified by:
append in interface Appendable
Overrides:
append in class Writer
Throws:
IOException

append

public IndentPrinter append(char c)
                     throws IOException
Specified by:
append in interface Appendable
Overrides:
append in class Writer
Throws:
IOException

println

public void println(String v)
             throws IOException
Throws:
IOException

println

public void println(char[] v)
             throws IOException
Throws:
IOException

println

public void println(Object v)
             throws IOException
Throws:
IOException

println

public void println(char v)
             throws IOException
Throws:
IOException

println

public void println(int v)
             throws IOException
Throws:
IOException

println

public void println(long v)
             throws IOException
Throws:
IOException

println

public void println(double v)
             throws IOException
Throws:
IOException

println

public void println(float v)
             throws IOException
Throws:
IOException

println

public void println(boolean v)
             throws IOException
Throws:
IOException

println

public void println(CharSequence v)
             throws IOException
Throws:
IOException

println

public void println()
             throws IOException
Throws:
IOException

write

public void write(char[] buf)
           throws IOException
Overrides:
write in class Writer
Throws:
IOException

write

public void write(char[] buf,
                  int off,
                  int len)
           throws IOException
Specified by:
write in class Writer
Throws:
IOException

write

public void write(char c)
           throws IOException
Throws:
IOException

write

public void write(String s)
           throws IOException
Overrides:
write in class Writer
Throws:
IOException

write

public void write(String s,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class Writer
Throws:
IOException

flush

public void flush()
           throws IOException
Flushes the underlying stream. Doesn't do anything to the IndentPrinter's own state (i.e. you don't need to flush the IndentPrinter if you flush the underlying stream yourself).

Specified by:
flush in interface Flushable
Specified by:
flush in class Writer
Throws:
IOException

close

public void close()
           throws IOException
Closes the underlying stream. Doesn't do anything to the IndentPrinter's own state (i.e. you don't need to close the IndentPrinter if you close the underlying stream yourself).

Specified by:
close in interface Closeable
Specified by:
close in class Writer
Throws:
IOException