org.sadun.util
Class JustifierWriter

java.lang.Object
  extended byjava.io.Writer
      extended byjava.io.FilterWriter
          extended byorg.sadun.util.JustifierWriter

public class JustifierWriter
extends java.io.FilterWriter

A Writer to justify text.

This writer right-justifies the characters sent to it to a given line length. It interprets the characters as a stream of space-separated words and when a word doesn't fit in the given length, the line is justified and sent to the wrapped writer.

Version:
1.03
Author:
Cristiano Sadun

Field Summary
 
Fields inherited from class java.io.FilterWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
JustifierWriter(java.io.Writer out)
          Create a justifier wrapping the given writer with the default linge length
JustifierWriter(java.io.Writer out, int lineLenght)
          Create a justifier wraping the given writer and using the given line length
 
Method Summary
 int getLineLength()
          Return the line length currently employed by this writer.
 boolean isAutoFlush()
          Return true if the writer automatically flushes the wrapped writer when a line is written.
 boolean isJustifyOnNewLine()
          Return true if the writer applies justification on explicit newlines.
 boolean isPadOnNewLine()
          Return true if the writer pads output with space on a new line.
 boolean isPreserveInitialSpaces()
          Return true if the writer preserves intial spaces.
static java.lang.String justify(java.lang.String s, int lineLength)
          Return the given string, right-justified to the given line length, without preserving initial spaces.
static java.lang.String justify(java.lang.String s, int lineLength, boolean preserveInitialSpaces)
          Return the given string, right-justified to the given line length, optionally preserving initial spaces.
static void main(java.lang.String[] args)
           
 void setAutoFlush(boolean autoFlush)
          Set whether or not the writer automatically flushes the wrapped writer when a line is written.
 void setJustifyOnNewLine(boolean justifyOnNewLine)
          Set whether or not the writer applies justification on explicit newlines.
 void setLineLength(int lineLength)
          Set the line length currently employed by this writer.
 void setPadOnNewLine(boolean padOnNewLine)
          Set whether or not the writer pads output with spaces on a new line.
 void setPreserveInitialSpaces(boolean preserveInitialSpaces)
          Set whether or not the writer preserves intial spaces.
 void write(char[] cbuf, int off, int len)
          Write the given characters.
 void write(java.lang.String str, int off, int len)
          Write the given string.
 
Methods inherited from class java.io.FilterWriter
close, flush, write
 
Methods inherited from class java.io.Writer
write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JustifierWriter

public JustifierWriter(java.io.Writer out)
Create a justifier wrapping the given writer with the default linge length

Parameters:
out - the wrapped writer

JustifierWriter

public JustifierWriter(java.io.Writer out,
                       int lineLenght)
Create a justifier wraping the given writer and using the given line length

Parameters:
out - the wrapped writer
lineLenght - the desired line length
Method Detail

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Write the given characters. If the characters contain a newline, the line will be justified if justifyOnNewLine is true and the embedded writer will be flushed if autoFlush is true.

If the words exceed the given line lenght the line is justified and sent to wrapped writer.

Throws:
java.io.IOException

write

public void write(java.lang.String str,
                  int off,
                  int len)
           throws java.io.IOException
Write the given string. If the string contains a newline, the line will be justified if justifyOnNewLine is true and the embedded writer will be flushed if autoFlush is true.

If the words exceed the given line lenght the line is justified and sent to wrapped writer.

Throws:
java.io.IOException

justify

public static java.lang.String justify(java.lang.String s,
                                       int lineLength)
Return the given string, right-justified to the given line length, without preserving initial spaces. A single word will not be justified.

Parameters:
s - the string to justified
lineLength - the desired line lenght.
Returns:
a string with the same contents as the given one, but right-justified

justify

public static java.lang.String justify(java.lang.String s,
                                       int lineLength,
                                       boolean preserveInitialSpaces)
Return the given string, right-justified to the given line length, optionally preserving initial spaces. A single word will not be justified.

Parameters:
s - the string to justified
lineLength - the desired line lenght.
preserveInitialSpaces - if true, initial spaces will be preserved
Returns:
a string with the same contents as the given one, but right-justified

getLineLength

public int getLineLength()
Return the line length currently employed by this writer.

Returns:
the line length currently employed by this writer.

setLineLength

public void setLineLength(int lineLength)
Set the line length currently employed by this writer. This will affect only the current line and the successive ones.

Parameters:
lineLength - the new line length

isJustifyOnNewLine

public boolean isJustifyOnNewLine()
Return true if the writer applies justification on explicit newlines.

Returns:
true if the writer applies justification on explicit newlines.

setJustifyOnNewLine

public void setJustifyOnNewLine(boolean justifyOnNewLine)
Set whether or not the writer applies justification on explicit newlines.

Parameters:
justifyOnNewLine - if true, the writer applies justification on explicit newlines.

isPreserveInitialSpaces

public boolean isPreserveInitialSpaces()
Return true if the writer preserves intial spaces.

Returns:
true if the writer preserves intial spaces.

setPreserveInitialSpaces

public void setPreserveInitialSpaces(boolean preserveInitialSpaces)
Set whether or not the writer preserves intial spaces.

Parameters:
preserveInitialSpaces - if true, the writer preserves intial spaces.

isPadOnNewLine

public boolean isPadOnNewLine()
Return true if the writer pads output with space on a new line.

Returns:
true if the writer pads output with space on a new line.

setPadOnNewLine

public void setPadOnNewLine(boolean padOnNewLine)
Set whether or not the writer pads output with spaces on a new line.

Parameters:
padOnNewLine - if true, the writer pads output with spaces on a new line.

isAutoFlush

public boolean isAutoFlush()
Return true if the writer automatically flushes the wrapped writer when a line is written.

Returns:
true if the writer automatically flushes the wrapped writer when a line is written.

setAutoFlush

public void setAutoFlush(boolean autoFlush)
Set whether or not the writer automatically flushes the wrapped writer when a line is written.

Parameters:
autoFlush - if true, the writer automatically flushes the wrapped writer when a line is written.

main

public static void main(java.lang.String[] args)