|
1 | 1 | package io.github.computerdaddyguy.jfiletreeprettyprinter; |
2 | 2 |
|
3 | 3 | import java.nio.file.Path; |
| 4 | +import java.util.Objects; |
4 | 5 | import java.util.function.Predicate; |
5 | 6 | import java.util.regex.Pattern; |
6 | 7 | import org.jspecify.annotations.NullMarked; |
@@ -118,4 +119,96 @@ public static Predicate<Path> isFile() { |
118 | 119 | return PathUtils::isFile; |
119 | 120 | } |
120 | 121 |
|
| 122 | + // ---------- Hierarchy ---------- |
| 123 | + |
| 124 | + /** |
| 125 | + * Creates a predicate that evaluates the direct parent of a given path. |
| 126 | + * The returned predicate applies the provided {@code parentPredicate} to {@link Path#getParent()} of the tested path. |
| 127 | + * If tested path has no parent, the returned predicate will always return {@code false}. |
| 128 | + * |
| 129 | + * @param parentPredicate the predicate to apply on the parent path (must not be {@code null}) |
| 130 | + * |
| 131 | + * @return a predicate that returns {@code true} if the parent of the tested path matches the given predicate |
| 132 | + * |
| 133 | + * @throws NullPointerException if {@code parentPredicate} is {@code null} |
| 134 | + */ |
| 135 | + public static Predicate<Path> hasParentMatching(Predicate<Path> parentPredicate) { |
| 136 | + Objects.requireNonNull(parentPredicate, "parentPredicate is null"); |
| 137 | + return path -> PathUtils.hasParentMatching(path, parentPredicate); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Creates a predicate that evaluates all ancestors of a given path. |
| 142 | + * <p> |
| 143 | + * The returned predicate applies the provided {@code ancestorPredicate} |
| 144 | + * to each parent in the chain obtained via successive calls to {@link Path#getParent()}. |
| 145 | + * If any ancestor matches, the predicate returns {@code true}. |
| 146 | + * <p> |
| 147 | + * If the tested path has no parent, the returned predicate always returns {@code false}. |
| 148 | + * |
| 149 | + * @param ancestorPredicate the predicate to apply on each ancestor path (must not be {@code null}) |
| 150 | + * |
| 151 | + * @return a predicate that returns {@code true} if any ancestor of the tested path matches the given predicate |
| 152 | + * |
| 153 | + * @throws NullPointerException if {@code ancestorPredicate} is {@code null} |
| 154 | + */ |
| 155 | + public static Predicate<Path> hasAncestorMatching(Predicate<Path> ancestorPredicate) { |
| 156 | + Objects.requireNonNull(ancestorPredicate, "ancestorPredicate is null"); |
| 157 | + return path -> PathUtils.hasAncestorMatching(path, ancestorPredicate); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Creates a predicate that evaluates the direct children of a given path. |
| 162 | + * <p> |
| 163 | + * The returned predicate applies the provided {@code childPredicate} to each |
| 164 | + * direct child of the tested path. The predicate returns {@code true} if at least |
| 165 | + * one child matches. If the path is not a directory, the predicate always returns {@code false}. |
| 166 | + * |
| 167 | + * @param childPredicate the predicate to apply on each direct child |
| 168 | + * |
| 169 | + * @return a predicate that returns {@code true} if at least one direct child matches |
| 170 | + * |
| 171 | + * @throws NullPointerException if {@code childPredicate} is {@code null} |
| 172 | + */ |
| 173 | + public static Predicate<Path> hasDirectChildMatching(Predicate<Path> childPredicate) { |
| 174 | + Objects.requireNonNull(childPredicate, "childPredicate is null"); |
| 175 | + return path -> PathUtils.hasDirectChildMatching(path, childPredicate); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Creates a predicate that evaluates all descendants (children at any depth) of a given path. |
| 180 | + * <p> |
| 181 | + * The returned predicate applies the provided {@code descendantPredicate} recursively to each |
| 182 | + * child and sub-child. It returns {@code true} if at least one descendant matches. |
| 183 | + * |
| 184 | + * @param descendantPredicate the predicate to apply on each descendant |
| 185 | + * |
| 186 | + * @return a predicate that returns {@code true} if at least one descendant matches |
| 187 | + * |
| 188 | + * @throws NullPointerException if {@code descendantPredicate} is {@code null} |
| 189 | + */ |
| 190 | + public static Predicate<Path> hasDescendantMatching(Predicate<Path> descendantPredicate) { |
| 191 | + Objects.requireNonNull(descendantPredicate, "descendantPredicate is null"); |
| 192 | + return path -> PathUtils.hasDescendantMatching(path, descendantPredicate); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Creates a predicate that evaluates the siblings of a given path. |
| 197 | + * <p> |
| 198 | + * The returned predicate applies the provided {@code siblingPredicate} to each |
| 199 | + * sibling of the tested path (other files/directories in the same parent). |
| 200 | + * The predicate returns {@code true} if at least one sibling matches. |
| 201 | + * If the tested path has no parent, the predicate always returns {@code false}. |
| 202 | + * |
| 203 | + * @param siblingPredicate the predicate to apply on each sibling |
| 204 | + * |
| 205 | + * @return a predicate that returns {@code true} if at least one sibling matches |
| 206 | + * |
| 207 | + * @throws NullPointerException if {@code siblingPredicate} is {@code null} |
| 208 | + */ |
| 209 | + public static Predicate<Path> hasSiblingMatching(Predicate<Path> siblingPredicate) { |
| 210 | + Objects.requireNonNull(siblingPredicate, "siblingPredicate is null"); |
| 211 | + return path -> PathUtils.hasSiblingMatching(path, siblingPredicate); |
| 212 | + } |
| 213 | + |
121 | 214 | } |
0 commit comments