Class LimitedStringBuilder

java.lang.Object
org.machanism.machai.ai.tools.LimitedStringBuilder

public class LimitedStringBuilder extends Object
A StringBuilder-like helper that retains only the last maxSize characters.

This utility is typically used when capturing potentially unbounded output (for example, process stdout/stderr) while keeping a deterministic upper bound on memory usage.

  • Constructor Details

    • LimitedStringBuilder

      public LimitedStringBuilder(int maxSize)
      Creates a builder that keeps at most maxSize characters.
      Parameters:
      maxSize - maximum number of characters to retain; must be positive
      Throws:
      IllegalArgumentException - if maxSize is not positive
  • Method Details

    • append

      public LimitedStringBuilder append(String text)
      Appends text to this builder, trimming any excess characters from the start to stay within the configured maximum size.
      Parameters:
      text - text to append; ignored if null
      Returns:
      this instance for fluent chaining
    • getLastText

      public String getLastText()
      Returns the retained content.

      If truncation occurred, a prefix is added to indicate that earlier content was discarded.

      Returns:
      retained text (possibly with a truncation prefix)
    • length

      public int length()
      Returns the number of characters currently retained.
      Returns:
      retained length
    • clear

      public void clear()
      Clears the retained content.