Class LineExtensionBuilder

java.lang.Object
io.github.computerdaddyguy.jfiletreeprettyprinter.LineExtensionBuilder

@NullMarked public class LineExtensionBuilder extends Object
A builder for constructing functions that provide optional line extensions (such as comments or formatting markers) when pretty-printing file trees.

A LineExtensionBuilder allows you to add rules in the form of Function objects that map a Path to an extension string. When the resulting function is applied to a path, rules are evaluated in insertion order, and the first non-null result is used.

  • null means no extension (line is printed normally).
  • An empty string means "force line break" in compact directory chains.
  • Any non-empty string is appended after the path (e.g. a comment).

Example usage:


 var lineExtension = LineExtensionBuilder.newInstance()
     .add(PathMatchers.hasName("README.md"), " // Project documentation")
     .addLineBreak(PathMatchers.hasRelativePathMatchingGlob(root, "src/main/java"))
     .build();
 
The returned Function<Path, String> can then be passed to PrettyPrintOptions.withLineExtension(Function).
See Also:
  • Method Details

    • build

      public Function<Path,String> build()
      Builds the final function mapping a Path to an extension string.

      The function applies the registered rules in insertion order. The first rule returning a non-null value determines the extension. If none match, the function returns null.

      Returns:
      a function mapping paths to extensions
    • add

      public LineExtensionBuilder add(Function<Path,String> lineExtension)
      Adds a custom line extension rule.

      The function should return either:

      • null to indicate "no extension".
      • an empty string to force a line break.
      • a non-empty string to append after the path.
      Parameters:
      lineExtension - a function mapping paths to extensions (non-null)
      Returns:
      this builder (for chaining)
      Throws:
      NullPointerException - if lineExtension is null
    • add

      public LineExtensionBuilder add(PathMatcher pathMatcher, String extension)
      Adds a rule that appends the given extension when the matcher matches.

      If the matcher does not match, the rule returns null.

      Parameters:
      pathMatcher - the matcher to test paths against (non-null)
      extension - the extension string to return when matched
      Returns:
      this builder (for chaining)
      Throws:
      NullPointerException - if pathMatcher is null
    • addLineBreak

      public LineExtensionBuilder addLineBreak(PathMatcher pathMatcher)
      Adds a rule that forces a line break (instead of appending text) whenever the given matcher matches.
      Parameters:
      pathMatcher - the matcher to test paths against (non-null)
      Returns:
      this builder (for chaining)