|
| 1 | +package io.github.computerdaddyguy.jfiletreeprettyprinter; |
| 2 | + |
| 3 | +import java.nio.file.Path; |
| 4 | +import java.nio.file.PathMatcher; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.function.Function; |
| 9 | +import org.jspecify.annotations.NullMarked; |
| 10 | + |
| 11 | +/** |
| 12 | + * A builder for constructing functions that provide optional line extensions |
| 13 | + * (such as comments or formatting markers) when pretty-printing file trees. |
| 14 | + * <p> |
| 15 | + * A {@code LineExtensionBuilder} allows you to add rules in the form of |
| 16 | + * {@link Function} objects that map a {@link Path} to an extension string. |
| 17 | + * When the resulting function is applied to a path, rules are evaluated |
| 18 | + * in insertion order, and the first non-{@code null} result is used. |
| 19 | + * <ul> |
| 20 | + * <li>{@code null} means no extension (line is printed normally).</li> |
| 21 | + * <li>An empty string means "force line break" in compact directory chains.</li> |
| 22 | + * <li>Any non-empty string is appended after the path (e.g. a comment).</li> |
| 23 | + * </ul> |
| 24 | + * |
| 25 | + * <p>Example usage: |
| 26 | + * <pre>{@code |
| 27 | + * var lineExtension = LineExtensionBuilder.newInstance() |
| 28 | + * .add(PathMatchers.hasName("README.md"), " // Project documentation") |
| 29 | + * .addLineBreak(PathMatchers.hasRelativePathMatchingGlob(root, "src/main/java")) |
| 30 | + * .build(); |
| 31 | + * }</pre> |
| 32 | + * |
| 33 | + * The returned {@code Function<Path, String>} can then be passed to |
| 34 | + * {@link PrettyPrintOptions#withLineExtension(Function)}. |
| 35 | + * |
| 36 | + * @see PrettyPrintOptions |
| 37 | + */ |
| 38 | +@NullMarked |
| 39 | +public class LineExtensionBuilder { |
| 40 | + |
| 41 | + private static final String NO_EXTENSION = null; |
| 42 | + private static final String LINE_BREAK_EXTENSION = ""; |
| 43 | + |
| 44 | + private List<Function<Path, String>> extensions; |
| 45 | + |
| 46 | + private LineExtensionBuilder() { |
| 47 | + this.extensions = new ArrayList<>(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns a new {@link LineExtensionBuilder}. |
| 52 | + * |
| 53 | + * @return a fresh builder instance |
| 54 | + */ |
| 55 | + public static LineExtensionBuilder newInstance() { |
| 56 | + return new LineExtensionBuilder(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Builds the final function mapping a {@link Path} to an extension string. |
| 61 | + * <p> |
| 62 | + * The function applies the registered rules in insertion order. |
| 63 | + * The first rule returning a non-{@code null} value determines the extension. |
| 64 | + * If none match, the function returns {@code null}. |
| 65 | + * |
| 66 | + * @return a function mapping paths to extensions |
| 67 | + */ |
| 68 | + public Function<Path, String> build() { |
| 69 | + var immutExtensions = List.copyOf(extensions); |
| 70 | + return path -> { |
| 71 | + String result = NO_EXTENSION; |
| 72 | + for (var rule : immutExtensions) { |
| 73 | + result = rule.apply(path); |
| 74 | + if (result != NO_EXTENSION) { |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + return result; |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Adds a custom line extension rule. |
| 84 | + * <p> |
| 85 | + * The function should return either: |
| 86 | + * <ul> |
| 87 | + * <li>{@code null} to indicate "no extension".</li> |
| 88 | + * <li>an empty string to force a line break.</li> |
| 89 | + * <li>a non-empty string to append after the path.</li> |
| 90 | + * </ul> |
| 91 | + * |
| 92 | + * @param lineExtension a function mapping paths to extensions (non-null) |
| 93 | + * @return this builder (for chaining) |
| 94 | + * @throws NullPointerException if {@code lineExtension} is null |
| 95 | + */ |
| 96 | + public LineExtensionBuilder add(Function<Path, String> lineExtension) { |
| 97 | + Objects.requireNonNull(lineExtension, "lineExtension is null"); |
| 98 | + this.extensions.add(lineExtension); |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Adds a rule that appends the given extension when the matcher matches. |
| 104 | + * <p> |
| 105 | + * If the matcher does not match, the rule returns {@code null}. |
| 106 | + * |
| 107 | + * @param pathMatcher the matcher to test paths against (non-null) |
| 108 | + * @param extension the extension string to return when matched |
| 109 | + * @return this builder (for chaining) |
| 110 | + * @throws NullPointerException if {@code pathMatcher} is null |
| 111 | + */ |
| 112 | + public LineExtensionBuilder add(PathMatcher pathMatcher, String extension) { |
| 113 | + Objects.requireNonNull(pathMatcher, "pathMatcher is null"); |
| 114 | + return add(path -> pathMatcher.matches(path) ? extension : NO_EXTENSION); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Adds a rule that forces a line break (instead of appending text) |
| 119 | + * whenever the given matcher matches. |
| 120 | + * |
| 121 | + * @param pathMatcher the matcher to test paths against (non-null) |
| 122 | + * @return this builder (for chaining) |
| 123 | + */ |
| 124 | + public LineExtensionBuilder addLineBreak(PathMatcher pathMatcher) { |
| 125 | + return add(pathMatcher, LINE_BREAK_EXTENSION); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments