Class PrependingStringBuilder

  • Direct Known Subclasses:
    ParentheticalStringBuilder

    public class PrependingStringBuilder
    extends IndentingStringBuilder
    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.