Class PathSortBuilder
java.lang.Object
io.github.computerdaddyguy.jfiletreeprettyprinter.PathSortBuilder
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:
HIGHEST_PRECEDENCE(Integer.MIN_VALUE) — top priorityDEFAULT_PRECEDENCE(0) — default orderLOWEST_PRECEDENCE(Integer.MAX_VALUE) — last priority
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault precedence (neutral value).static final intHighest possible precedence — items appear first.static final intLowest possible precedence — items appear last. -
Method Summary
Modifier and TypeMethodDescriptionadd(PathMatcher pathMatcher, int order) Adds a rule that assigns a precedence value to all paths matching the specifiedPathMatcher.add(ToIntFunction<Path> order) Adds a custom rule function defining a precedence for a path.addFirst(PathMatcher pathMatcher) Adds a rule that forces matching paths to appear first in the sort order.addLast(PathMatcher pathMatcher) Adds a rule that forces matching paths to appear last in the sort order.build()Builds the finalComparator<Path>based on the configured rules.
-
Field Details
-
HIGHEST_PRECEDENCE
public static final int HIGHEST_PRECEDENCEHighest possible precedence — items appear first.- See Also:
-
DEFAULT_PRECEDENCE
public static final int DEFAULT_PRECEDENCEDefault precedence (neutral value).- See Also:
-
LOWEST_PRECEDENCE
public static final int LOWEST_PRECEDENCELowest possible precedence — items appear last.- See Also:
-
-
Method Details
-
build
Builds the finalComparator<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
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- iforderis null
-
add
Adds a rule that assigns a precedence value to all paths matching the specifiedPathMatcher.- Parameters:
pathMatcher- the matcher to test pathsorder- the precedence value to assign- Returns:
- this builder for chaining
- Throws:
NullPointerException- ifpathMatcheris null
-
addFirst
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
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
-