Class StringBuilderAppender

java.lang.Object
xyz.ronella.trivial.decorator.StringBuilderAppender

public class StringBuilderAppender extends Object
A decorator for StringBuilder that gives you a chance to add pre-append and post-append logic.
Since:
2019-12-01
  • Constructor Details

    • StringBuilderAppender

      public StringBuilderAppender(StringBuilder builder, Consumer<StringBuilder> defaultBeforeAppend, Consumer<StringBuilder> defaultAfterAppend)
      Decorate the StringBuilder to have a default pre-append and post-append logic.
      Parameters:
      builder - An instance of StringBuilder.
      defaultBeforeAppend - The logic to perform before an append.
      defaultAfterAppend - The logic to perform after an append.
    • StringBuilderAppender

      public StringBuilderAppender(String string, Consumer<StringBuilder> defaultBeforeAppend, Consumer<StringBuilder> defaultAfterAppend)
      Decorate the StringBuilder to have a default pre-append and post-append logic.
      Parameters:
      string - An instance of String.
      defaultBeforeAppend - The logic to perform before an append.
      defaultAfterAppend - The logic to perform after an append.
      Since:
      2.1.0
    • StringBuilderAppender

      public StringBuilderAppender(Consumer<StringBuilder> defaultBeforeAppend, Consumer<StringBuilder> defaultAfterAppend)
      Creates an instance of StringBuilderAppender with default StringBuilder.
      Parameters:
      defaultBeforeAppend - The logic to perform before an append.
      defaultAfterAppend - The logic to perform after an append.
      Since:
      2.1.0
    • StringBuilderAppender

      public StringBuilderAppender(StringBuilder builder, Consumer<StringBuilder> defaultBeforeAppend)
      Decorate the StringBuilder to have a default pre-append logic.
      Parameters:
      builder - An instance of StringBuilder.
      defaultBeforeAppend - The logic to perform before an append.
    • StringBuilderAppender

      public StringBuilderAppender(String string, Consumer<StringBuilder> defaultBeforeAppend)
      Decorate the StringBuilder to have a default pre-append logic.
      Parameters:
      string - An instance of String.
      defaultBeforeAppend - The logic to perform before an append.
      Since:
      2.1.0
    • StringBuilderAppender

      public StringBuilderAppender(Consumer<StringBuilder> defaultBeforeAppend)
      Creates an instance of StringBuilderAppender with default StringBuilder.
      Parameters:
      defaultBeforeAppend - The logic to perform before an append.
      Since:
      2.1.0
    • StringBuilderAppender

      public StringBuilderAppender(StringBuilder builder)
      Accepts a StringBuilder to decorate.
      Parameters:
      builder - An instance of StringBuilder.
    • StringBuilderAppender

      public StringBuilderAppender(String string)
      Accepts a StringBuilder to decorate.
      Parameters:
      string - An instance of String.
      Since:
      2.1.0
    • StringBuilderAppender

      public StringBuilderAppender()
      Creates an instance of StringBuilderAppender with default StringBuilder.
      Since:
      2.1.0
  • Method Details

    • threadSafe

      public StringBuilderAppender threadSafe()
      Make the appending task thread safe.
      Returns:
      An instance of StringBuilderAppender.
    • append

      public StringBuilderAppender append(String text, Consumer<StringBuilder> beforeAppend, Consumer<StringBuilder> afterAppend)
      Perform an append operation with pre-append and post-append logic. This will override the default pre-append and post-append logic.
      Parameters:
      text - The text to be appended.
      beforeAppend - The logic to perform before an append.
      afterAppend - The logic to perform after an append.
      Returns:
      An instance of StringBuilderAppender.
    • append

      public StringBuilderAppender append(BooleanSupplier condition, String text, Consumer<StringBuilder> beforeAppend, Consumer<StringBuilder> afterAppend)
      Perform an append operation with pre-append and post-append logic. This will override the default pre-append and post-append logic.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      text - The text to be appended.
      beforeAppend - The logic to perform before an append.
      afterAppend - The logic to perform after an append.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(String text, Consumer<StringBuilder> beforeAppend)
      Perform an append operation with pre-append logic. This will override the default pre-append logic.
      Parameters:
      text - The text to be appended.
      beforeAppend - The logic to perform before an append.
      Returns:
      An instance of StringBuilderAppender.
    • append

      public StringBuilderAppender append(BooleanSupplier condition, String text, Consumer<StringBuilder> beforeAppend)
      Perform an append operation with pre-append logic. This will override the default pre-append logic.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      text - The text to be appended.
      beforeAppend - The logic to perform before an append.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(String text)
      Perform a normal append without any pre-append or post-append logic.
      Parameters:
      text - The text to be appended.
      Returns:
      An instance of StringBuilderAppender.
    • append

      public StringBuilderAppender append(BooleanSupplier condition, String text)
      Perform a normal append without any pre-append or post-append logic.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      text - The text to be appended.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • toString

      public String toString()
      String representation of the internal StringBuilder that the decorator is holding. The one that passed in the constructor or the one generated automatically.
      Overrides:
      toString in class Object
      Returns:
      The string representation.
      Since:
      2.0.0
    • getStringBuilder

      public StringBuilder getStringBuilder()
      Access the internal StringBuilder that the decorator is holding.
      Returns:
      An instance of StringBuilder
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(Consumer<StringBuilder> updateLogic, Consumer<StringBuilder> beforeAppend, Consumer<StringBuilder> afterAppend)
      Ability to append using your own custom logic that this decorator cannot handle.
      Parameters:
      updateLogic - Must hold the custom logic for appending.
      beforeAppend - The logic to perform before an append.
      afterAppend - The logic to perform after an append.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(BooleanSupplier condition, Consumer<StringBuilder> updateLogic, Consumer<StringBuilder> beforeAppend, Consumer<StringBuilder> afterAppend)
      Ability to append using your own custom logic that this decorator cannot handle.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      updateLogic - Must hold the custom logic for appending.
      beforeAppend - The logic to perform before an append.
      afterAppend - The logic to perform after an append.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(Consumer<StringBuilder> updateLogic, Consumer<StringBuilder> beforeAppend)
      Ability to append using your own custom logic that this decorator cannot handle.
      Parameters:
      updateLogic - Must hold the custom logic for appending.
      beforeAppend - The logic to perform before an append.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(BooleanSupplier condition, Consumer<StringBuilder> updateLogic, Consumer<StringBuilder> beforeAppend)
      Ability to append using your own custom logic that this decorator cannot handle.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      updateLogic - Must hold the custom logic for appending.
      beforeAppend - The logic to perform before an append.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(Consumer<StringBuilder> updateLogic)
      Ability to append using your own custom logic that this decorator cannot handle.
      Parameters:
      updateLogic - Must hold the custom logic for appending.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(BooleanSupplier condition, Consumer<StringBuilder> updateLogic)
      Ability to append using your own custom logic that this decorator cannot handle.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      updateLogic - Must hold the custom logic for appending.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.0.0
    • append

      public StringBuilderAppender append(Consumer<StringBuilder> beforeAppend, Consumer<StringBuilder> afterAppend, String... texts)
      Perform an append operation with pre-append and post-append logic. This will soverride the default pre-append and post-append logic.
      Parameters:
      beforeAppend - The logic to perform before an append.
      afterAppend - The logic to perform after an append.
      texts - The array of texts to be appended.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.1.0
    • append

      public StringBuilderAppender append(Consumer<StringBuilder> beforeAppend, String... texts)
      Perform an append operation with pre-append logic. This will override the default pre-append logic.
      Parameters:
      beforeAppend - The logic to perform before an append.
      texts - The array of texts to be appended.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.1.0
    • append

      public StringBuilderAppender append(String... texts)
      Perform a normal append without any pre-append or post-append logic.
      Parameters:
      texts - The array of texts to be appended.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.1.0
    • append

      public StringBuilderAppender append(BooleanSupplier condition, Consumer<StringBuilder> beforeAppend, Consumer<StringBuilder> afterAppend, String... texts)
      Perform an append operation with pre-append and post-append logic. This will override the default pre-append and post-append logic.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      beforeAppend - The logic to perform before an append.
      afterAppend - The logic to perform after an append.
      texts - The array of texts to be appended.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.1.0
    • append

      public StringBuilderAppender append(BooleanSupplier condition, Consumer<StringBuilder> beforeAppend, String... texts)
      Perform an append operation with pre-append logic. This will override the default pre-append logic.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      beforeAppend - The logic to perform before an append.
      texts - The array of texts to be appended.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.1.0
    • append

      public StringBuilderAppender append(BooleanSupplier condition, String... texts)
      Perform a normal append without any pre-append or post-append logic.
      Parameters:
      condition - An implementation of BooleanSupplier that must return true to perform any append.
      texts - The array of texts to be appended.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.1.0
    • clear

      public StringBuilderAppender clear()
      Clears the content of the target string builder
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.7.0
    • replace

      public StringBuilderAppender replace(CharSequence target, CharSequence replacement)
      Replace part of StringBuilder
      Parameters:
      target - The target string to replacement.
      replacement - The replacement of the target.
      Returns:
      An instance of StringBuilderAppender.
      Since:
      2.8.0