Arch4J 1.1

org.arch4j.generator
Class CodeBuffer

java.lang.Object
  |
  +--org.arch4j.generator.CodeBuffer
All Implemented Interfaces:
GeneratorConstants

public class CodeBuffer
extends Object
implements GeneratorConstants

This class is used to cleanly format code into a StringBuffer. A typical use would be to format the contents of a method and then extract it as a String via the toString() method.

The code buffer maintains a current indentation level, so that repeated calls to append lines automatically get indented to the proper level. References to a tab character in this class may really represent a number of spaces instead.

Opening braces for a code block will either appear at the end of the current line or on a line by itself. This attribute can be modified after the code buffer has been created.

Version:
1.0
Author:
Ross E. Greinke

Field Summary
 
Fields inherited from interface org.arch4j.generator.GeneratorConstants
END_JAVADOC, FILE_DELIMITER, INDENT, LINE_DELIMITERS, MIDDLE_JAVADOC, NEWLINE, SINGLE_COMMENT, SPACE, START_JAVADOC
 
Constructor Summary
CodeBuffer()
          Creates a new CodeBuffer instance.
CodeBuffer(int initialIndentLevel)
          Creates a new CodeBuffer instance with the given indentation level.
 
Method Summary
 void append(Object obj)
          Append obj to the buffer.
 void append(String text)
          Append text to the buffer.
 void appendAndStartBlock(Object obj)
          Append obj to the buffer and start a block.
 void appendAndStartBlock(String text)
          Append text to the buffer.
 void appendBlankLine()
          Append the representation of a newline directly to the buffer.
 void appendBlankLines(int count)
          Append count newlines directly to the buffer.
 void appendLine(Object obj)
          Append obj to the buffer.
 void appendLine(Object obj, boolean includeNewline)
          Append obj to the buffer.
 void appendLine(String text)
          Append text to the buffer.
 void appendLine(String text, boolean includeNewline)
          Append text to the buffer.
 void appendLines(String multiLineText)
          Append the lines contained in multiLineText one at a time to the receiver.
 void appendMultiLineComment(String multiLineText)
          Append the lines contained in multiLineText one at a time to the receiver within a multi-line comment block.
 void appendMultiLineCommentLines(String multiLineText)
          Append the lines contained in multiLineText one at a time to the receiver within an already started multi-line comment block.
 void appendSingleLineComment(String commentText)
          Append the line contained in commentText to the receiver within a single-line comment block.
 void appendSingleLineCommentLines(String multiLineText)
          Append the lines contained in multiLineText one at a time to the receiver, each preceded by the single-line comment string.
 void appendSpace()
          Append the representation of a space directly to the buffer.
 void appendSpaces(int count)
          Append count spaces directly to the buffer.
 void appendTab()
          Append the representation of a tab directly to the buffer.
 void appendTabs()
          Append an appropriate number of tabs for the current indent level directly to the buffer.
 void appendTabs(int count)
          Append count tabs directly to the buffer.
 void braceOnSameLine()
          Opening braces will now appear at the end of the current line when a new block is started.
 void endBlock()
          End a code block.
 void endMultiLineComment()
          End a multi-line comment block.
 void newlineBeforeBrace()
          Opening braces will now appear on a line by themselves when a new block is started.
 void startBlock()
          Start a code block.
 void startBlock(Object obj)
          Start a code block.
 void startBlock(String text)
          Start a code block.
 void startMultiLineComment()
          Start a multi-line comment block.
 void tabIn()
          Increment the current indentation level.
 void tabIn(int count)
          Increment the current indentation level by count levels.
 void tabInAndAppendLine(Object obj)
          Increment the current indentation level and then append obj.
 void tabInAndAppendLine(Object obj, boolean includeNewline)
          Increment the current indentation level and then append obj.
 void tabInAndAppendLine(String text)
          Increment the current indentation level and then append text.
 void tabInAndAppendLine(String text, boolean includeNewline)
          Increment the current indentation level and then append text.
 void tabOut()
          Decrement the current indentation level.
 void tabOut(int count)
          Decrement the current indentation level by count levels.
 void tabOutAndAppendLine(Object obj)
          Decrement the current indentation level and then append obj.
 void tabOutAndAppendLine(Object obj, boolean includeNewline)
          Decrement the current indentation level and then append obj.
 void tabOutAndAppendLine(String text)
          Decrement the current indentation level and then append text.
 void tabOutAndAppendLine(String text, boolean includeNewline)
          Decrement the current indentation level and then append text.
 String toString()
          Converts the data in this code buffer to a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CodeBuffer

public CodeBuffer()
Creates a new CodeBuffer instance.


CodeBuffer

public CodeBuffer(int initialIndentLevel)
Creates a new CodeBuffer instance with the given indentation level.

Parameters:
initialIndentLevel - The initial indentation level.
Method Detail

newlineBeforeBrace

public void newlineBeforeBrace()
Opening braces will now appear on a line by themselves when a new block is started.


braceOnSameLine

public void braceOnSameLine()
Opening braces will now appear at the end of the current line when a new block is started.


toString

public String toString()
Converts the data in this code buffer to a String.

Overrides:
toString in class Object
Returns:
A string representation of the code buffer.

tabIn

public void tabIn()
Increment the current indentation level.


tabIn

public void tabIn(int count)
Increment the current indentation level by count levels.


tabOut

public void tabOut()
Decrement the current indentation level.


tabOut

public void tabOut(int count)
Decrement the current indentation level by count levels.


append

public void append(Object obj)
Append obj to the buffer. The object is converted to a string (via it's toString() method and then appended.

Parameters:
obj - The object to append to the buffer.
See Also:
append(java.lang.String)

append

public void append(String text)
Append text to the buffer. The text is not indented at the current indentation level; the text is passed through as is.

Parameters:
text - The text to append to the buffer.

appendLine

public void appendLine(Object obj)
Append obj to the buffer. The object is converted to a string (via it's toString() method and then appended.

Parameters:
obj - The object to append to the buffer.
See Also:
appendLine(java.lang.String)

appendLine

public void appendLine(String text)
Append text to the buffer. The text is indented at the current indentation level. A newline is also appended.

Parameters:
text - The text to append to the buffer.
See Also:
appendLine(java.lang.String, boolean)

appendLine

public void appendLine(Object obj,
                       boolean includeNewline)
Append obj to the buffer. The object is converted to a string (via it's toString() method and then appended. A newline is appended if includeNewline is true.

Parameters:
obj - The object to append to the buffer.
includeNewline - If true, a newline is also appended.
See Also:
appendLine(java.lang.String, boolean)

appendLine

public void appendLine(String text,
                       boolean includeNewline)
Append text to the buffer. The text is indented at the current indentation level. A newline is appended if includeNewline is true.

Parameters:
text - The text to append to the buffer.
includeNewline - If true, a newline is also appended.

appendLines

public void appendLines(String multiLineText)
Append the lines contained in multiLineText one at a time to the receiver. This allows a block of code formatted with another CodeBuffer to be added using the receiver's indentation level, but still preserving the other buffer's indentation levels.

Parameters:
multiLineText - A String containing multiple lines of text.

tabInAndAppendLine

public void tabInAndAppendLine(Object obj)
Increment the current indentation level and then append obj.

Parameters:
obj - The object to append to the buffer.
See Also:
tabInAndAppendLine(java.lang.String)

tabInAndAppendLine

public void tabInAndAppendLine(String text)
Increment the current indentation level and then append text.

Parameters:
text - The text to append to the buffer.
See Also:
tabIn(), appendLine(java.lang.String)

tabInAndAppendLine

public void tabInAndAppendLine(Object obj,
                               boolean includeNewline)
Increment the current indentation level and then append obj.

Parameters:
obj - The object to append to the buffer.
includeNewline - If true, a newline is also appended.
See Also:
tabInAndAppendLine(java.lang.String, boolean)

tabInAndAppendLine

public void tabInAndAppendLine(String text,
                               boolean includeNewline)
Increment the current indentation level and then append text.

Parameters:
text - The text to append to the buffer.
includeNewline - If true, a newline is also appended.
See Also:
tabIn(), appendLine(java.lang.String, boolean)

tabOutAndAppendLine

public void tabOutAndAppendLine(Object obj)
Decrement the current indentation level and then append obj.

Parameters:
obj - The object to append to the buffer.
See Also:
tabOutAndAppendLine(java.lang.String)

tabOutAndAppendLine

public void tabOutAndAppendLine(String text)
Decrement the current indentation level and then append text.

Parameters:
text - The text to append to the buffer.
See Also:
tabOut(), appendLine(java.lang.String)

tabOutAndAppendLine

public void tabOutAndAppendLine(Object obj,
                                boolean includeNewline)
Decrement the current indentation level and then append obj.

Parameters:
obj - The object to append to the buffer.
includeNewline - If true, a newline is also appended.
See Also:
tabOutAndAppendLine(java.lang.String, boolean)

tabOutAndAppendLine

public void tabOutAndAppendLine(String text,
                                boolean includeNewline)
Decrement the current indentation level and then append text.

Parameters:
text - The text to append to the buffer.
includeNewline - If true, a newline is also appended.
See Also:
tabOut(), appendLine(java.lang.String, boolean)

appendTab

public void appendTab()
Append the representation of a tab directly to the buffer.


appendTabs

public void appendTabs()
Append an appropriate number of tabs for the current indent level directly to the buffer.


appendTabs

public void appendTabs(int count)
Append count tabs directly to the buffer.

Note: The literal tab character may be represented by a number of spaces.

Parameters:
count - The number of tabs to append.

appendBlankLine

public void appendBlankLine()
Append the representation of a newline directly to the buffer.


appendBlankLines

public void appendBlankLines(int count)
Append count newlines directly to the buffer.

Parameters:
count - The number of newlines to append.

appendSpace

public void appendSpace()
Append the representation of a space directly to the buffer.


appendSpaces

public void appendSpaces(int count)
Append count spaces directly to the buffer.

Parameters:
count - The number of spaces to append.

startBlock

public void startBlock(Object obj)
Start a code block.

Parameters:
obj - The object to append before the opening brace.
See Also:
startBlock(java.lang.String)

startBlock

public void startBlock(String text)
Start a code block. The text will appear before the opening brace. The brace is added automatically, and uses the current setting for whether the brace is on it's own line or not. Starting a block increments the indentation level.

For example, an if statement can be coded as:

 buffer.startBlock("if (aBoolean)");
 buffer.appendLine("doSomething();");
 buffer.endBlock();
 

Parameters:
text - The text to append before the opening brace.

appendAndStartBlock

public void appendAndStartBlock(Object obj)
Append obj to the buffer and start a block.

Parameters:
obj - The object to append before the opening brace.
See Also:
appendAndStartBlock(java.lang.String)

appendAndStartBlock

public void appendAndStartBlock(String text)
Append text to the buffer. The text is not indented at the current indentation level; the text is passed through as is, completing a previously started line. Then start a code block.

Parameters:
text - The text to append to the buffer.
See Also:
startBlock(java.lang.String)

startBlock

public void startBlock()
Start a code block. This method is useful for the beginning of a method or a class. The indentation level is also incremented.


endBlock

public void endBlock()
End a code block. The indentation level is decremented and a closing brace is appended to the buffer.


startMultiLineComment

public void startMultiLineComment()
Start a multi-line comment block. Additional comment lines can be added incrementally.

See Also:
appendMultiLineCommentLines(java.lang.String)

appendMultiLineComment

public void appendMultiLineComment(String multiLineText)
Append the lines contained in multiLineText one at a time to the receiver within a multi-line comment block. This method appends a complete comment block to the code buffer. If the text only contains a single line, a space-saving comment block is generated on a single line.

Parameters:
multiLineText - A String containing multiple lines of comment text.
See Also:
startMultiLineComment(), appendMultiLineCommentLines(java.lang.String), endMultiLineComment()

appendMultiLineCommentLines

public void appendMultiLineCommentLines(String multiLineText)
Append the lines contained in multiLineText one at a time to the receiver within an already started multi-line comment block.

Parameters:
multiLineText - A String containing multiple lines of comment text.
See Also:
startMultiLineComment(), endMultiLineComment()

endMultiLineComment

public void endMultiLineComment()
End a multi-line comment block.


appendSingleLineComment

public void appendSingleLineComment(String commentText)
Append the line contained in commentText to the receiver within a single-line comment block.

Parameters:
commentText - A String to append as a single-line comment.

appendSingleLineCommentLines

public void appendSingleLineCommentLines(String multiLineText)
Append the lines contained in multiLineText one at a time to the receiver, each preceded by the single-line comment string.

Parameters:
multiLineText - A String containing multiple lines of comment text.

Arch4J 1.1

Copyright © 2000-2004 SpiderLogic, a service of Wipfli Ullrich Bertelson LLP.