Package com.mapr.utils
Class PrependingStringBuilder
java.lang.Object
com.mapr.utils.IndentingStringBuilder
com.mapr.utils.PrependingStringBuilder
- Direct Known Subclasses:
ParentheticalStringBuilder
An IndentingStringBuilder with a trigger: If any non-whitespace output is added,
the trigger will go off and prepend a provided string exactly once. Once the trigger
has gone off, the prepend string won't be written again.
One use for this is to virtually prepend a connecting string when it is unknown yet
whether or not that string will be needed. For example:
final IndentingStringBuilder isb = ...;
// emit a conditional expression to isb...;
final PrependingStringBuilder psb = new PrependingStringBuilder(isb, " and ");
possiblyAppendMoreConditions(psb);
In this example, we need to call a function that might append more conditions, after
we have already added some. But we don't know in advance whether the call will add
anything or not, and this function already does other work such that it doesn't
want to have a "prepend an 'and'" option. If possiblyAppendMoreConditions() doesn't
write anything, then the extra " and " isn't written.
-
Field Summary
Fields inherited from class com.mapr.utils.IndentingStringBuilder
sb -
Constructor Summary
ConstructorsConstructorDescriptionPrependingStringBuilder(IndentingStringBuilder isb, String prependString) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionaddComment(String comment) Add a c-style comment.append(char v) Append a char.append(long v) Append a long value, as text.Append a String.intlength()Return the length of the String written so far.nextLine()Emit a newline, along with any necessary indentation.Remove a level of indentation.Add a new level of indentation.toString()booleanDetermine whether or not the prepender has fired.
-
Constructor Details
-
PrependingStringBuilder
Constructor.- Parameters:
isb- the IndentingStringBuilder to write toprependString- the string to prepend
-
-
Method Details
-
toString
- Overrides:
toStringin classIndentingStringBuilder
-
length
public int length()Description copied from class:IndentingStringBuilderReturn the length of the String written so far.- Overrides:
lengthin classIndentingStringBuilder- Returns:
- the length of the String written so far
-
triggered
public boolean triggered()Determine whether or not the prepender has fired.- Returns:
-
append
Description copied from class:IndentingStringBuilderAppend a String.- Overrides:
appendin classIndentingStringBuilder- Parameters:
v- the string to append- Returns:
- the IndentingStringBuilder
-
append
Description copied from class:IndentingStringBuilderAppend a char.- Overrides:
appendin classIndentingStringBuilder- Parameters:
v- the char to append- Returns:
- the IndentingStringBuilder
-
append
Description copied from class:IndentingStringBuilderAppend a long value, as text.- Overrides:
appendin classIndentingStringBuilder- Parameters:
v- the long to append- Returns:
- the IndentingStringBuilder
-
nextLine
Description copied from class:IndentingStringBuilderEmit a newline, along with any necessary indentation.- Specified by:
nextLinein classIndentingStringBuilder- Returns:
- this
-
pushIndent
Description copied from class:IndentingStringBuilderAdd a new level of indentation. This has no effect until the next call toIndentingStringBuilder.nextLine().- Specified by:
pushIndentin classIndentingStringBuilder- Returns:
- this
-
popIndent
Description copied from class:IndentingStringBuilderRemove a level of indentation. This has no effect until the next call toIndentingStringBuilder.nextLine(). It is an error to unindent when there are no levels of indentation active; this is indicated with a call toIndentingStringBuilder.addComment(String)with a warning that the caller has unindented too much.- Specified by:
popIndentin classIndentingStringBuilder- Returns:
- this
-
addComment
Description copied from class:IndentingStringBuilderAdd a c-style comment. The provided text is emitted surrounded by "/o " and " o/" (where the o's are asterisks). The comment will only appear in "pretty text." For plain text, this emits a single blank.- Specified by:
addCommentin classIndentingStringBuilder- Parameters:
comment- the comment- Returns:
-