Class PathSortBuilder

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

@NullMarked public class PathSortBuilder extends Object
A builder for creating a Comparator<Path> that defines a custom sorting order for file system paths based on rule precedence.

Each rule assigns an integer "precedence" value to paths. The first rule that matches a path determines its precedence. Paths are then sorted by ascending precedence value (lower values come first), followed by an alphabetical fallback comparison.

Predefined precedence constants:

Example usage:


 var customSort = PathSortBuilder.newInstance()
     .addFirst(PathMatchers.hasName("README.md"))     // always first
     .addLast(PathMatchers.hasName("target"))         // always last
     .add(path -> path.toString().contains("core") ? -10 : 0) // custom priority rule
     .build();

 var printer = FileTreePrettyPrinter.builder()
     .customizeOptions(options -> options.sort(customSort))
     .build();
 
  • Field Details

    • HIGHEST_PRECEDENCE

      public static final int HIGHEST_PRECEDENCE
      Highest possible precedence — items appear first.
      See Also:
    • DEFAULT_PRECEDENCE

      public static final int DEFAULT_PRECEDENCE
      Default precedence (neutral value).
      See Also:
    • LOWEST_PRECEDENCE

      public static final int LOWEST_PRECEDENCE
      Lowest possible precedence — items appear last.
      See Also:
  • Method Details

    • build

      public Comparator<Path> build()
      Builds the final Comparator<Path> based on the configured rules.

      Rules are tested in the order they were added. The first matching rule having a result different than DEFAULT_PRECEDENCE (meaning, 0) determines the precedence value for a given path. Paths are sorted by this precedence, and then alphabetically as a tiebreaker.

      Returns:
      a comparator defining the final path order
    • add

      public PathSortBuilder add(ToIntFunction<Path> order)
      Adds a custom rule function defining a precedence for a path.
      Parameters:
      order - a function returning a precedence value
      Returns:
      this builder for chaining
      Throws:
      NullPointerException - if order is null
    • add

      public PathSortBuilder add(PathMatcher pathMatcher, int order)
      Adds a rule that assigns a precedence value to all paths matching the specified PathMatcher.
      Parameters:
      pathMatcher - the matcher to test paths
      order - the precedence value to assign
      Returns:
      this builder for chaining
      Throws:
      NullPointerException - if pathMatcher is null
    • addFirst

      public PathSortBuilder addFirst(PathMatcher pathMatcher)
      Adds a rule that forces matching paths to appear first in the sort order.
      Parameters:
      pathMatcher - the matcher identifying high-priority paths
      Returns:
      this builder for chaining
    • addLast

      public PathSortBuilder addLast(PathMatcher pathMatcher)
      Adds a rule that forces matching paths to appear last in the sort order.
      Parameters:
      pathMatcher - the matcher identifying low-priority paths
      Returns:
      this builder for chaining