Claudenw commented on code in PR #424: URL: https://github.com/apache/creadur-rat/pull/424#discussion_r1930891593
########## apache-rat-core/src/main/java/org/apache/rat/document/DocumentNameMatcher.java: ########## @@ -219,4 +314,224 @@ public static DocumentNameMatcher and(final Collection<DocumentNameMatcher> matc public static DocumentNameMatcher and(final DocumentNameMatcher... matchers) { return and(Arrays.asList(matchers)); } + + /** + * A DocumentName predicate that uses MatchPatterns. + */ + public static final class MatchPatternsPredicate implements Predicate<DocumentName> { + /** The base directory for the pattern matches */ + private final DocumentName basedir; + /** The pattern matchers */ + private final MatchPatterns patterns; + + private MatchPatternsPredicate(final DocumentName basedir, final MatchPatterns patterns) { + this.basedir = basedir; + this.patterns = patterns; + } + + @Override + public boolean test(final DocumentName documentName) { + return patterns.matches(documentName.getName(), + tokenize(documentName.getName(), basedir.getDirectorySeparator()), + basedir.isCaseSensitive()); + } + + @Override + public String toString() { + return patterns.toString(); + } + } + + /** + * A DocumentName predicate that reverses another DocumentNameMatcher. + */ + public static final class NotPredicate implements Predicate<DocumentName> { + /** The document name matcher to reverse */ + private final DocumentNameMatcher nameMatcher; + + private NotPredicate(final DocumentNameMatcher nameMatcher) { + this.nameMatcher = nameMatcher; + } + + @Override + public boolean test(final DocumentName documentName) { + return !nameMatcher.matches(documentName); + } + + @Override + public String toString() { + return nameMatcher.predicate.toString(); + } + } + + /** + * A DocumentName predicate that uses FileFilter. + */ + public static final class FileFilterPredicate implements Predicate<DocumentName> { + /** The file filter */ + private final FileFilter fileFilter; + + private FileFilterPredicate(final FileFilter fileFilter) { + this.fileFilter = fileFilter; + } + + @Override + public boolean test(final DocumentName documentName) { + return fileFilter.accept(new File(documentName.getName())); + } + + @Override + public String toString() { + return fileFilter.toString(); + } + } + + interface CollectionPredicate extends Predicate<DocumentName> { + Iterable<DocumentNameMatcher> getMatchers(); + } + /** + * A marker interface to indicate this predicate contains a collection of matchers. + */ + abstract static class CollectionPredicateImpl implements CollectionPredicate { + /** The collection for matchers that make up this predicate */ + private final Iterable<DocumentNameMatcher> matchers; + + /** + * Constructs a collection predicate from the collection of matchers. + * @param matchers the colleciton of matchers to use. + */ + protected CollectionPredicateImpl(final Iterable<DocumentNameMatcher> matchers) { + this.matchers = matchers; + } + + /** + * Gets the internal matchers. + * @return an iterable over the internal matchers. + */ + public Iterable<DocumentNameMatcher> getMatchers() { + return matchers; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(this.getClass().getName()).append(": ").append(System.lineSeparator()); + for (DocumentNameMatcher matcher : matchers) { + builder.append(matcher.predicate.toString()).append(System.lineSeparator()); + } + return builder.toString(); + } + } + + /** + * An implementation of "and" logic across a collection of DocumentNameMatchers. + */ + // package private for testing access + static class And extends CollectionPredicateImpl { + And(final Iterable<DocumentNameMatcher> matchers) { + super(matchers); + } + + @Override + public boolean test(final DocumentName documentName) { + for (DocumentNameMatcher matcher : getMatchers()) { + if (!matcher.matches(documentName)) { + return false; + } + } + return true; + } + } + + /** + * An implementation of "or" logic across a collection of DocumentNameMatchers. + */ + // package private for testing access + static class Or extends CollectionPredicateImpl { + Or(final Iterable<DocumentNameMatcher> matchers) { + super(matchers); + } + + @Override + public boolean test(final DocumentName documentName) { + for (DocumentNameMatcher matcher : getMatchers()) { + if (matcher.matches(documentName)) { + return true; + } + } + return false; + } + } + + /** + * An implementation of "or" logic across a collection of DocumentNameMatchers. + */ + // package private for testing access + static class MatcherPredicate extends CollectionPredicateImpl { + MatcherPredicate(final Iterable<DocumentNameMatcher> matchers) { + super(matchers); + } + + @Override + public boolean test(final DocumentName documentName) { + Iterator<DocumentNameMatcher> iter = getMatchers().iterator(); + // included + if (iter.next().matches(documentName)) { Review Comment: It is, but I see how it is confusing and not a normal usage. I have removed the class and added an inline instance that makes clearer what the code is doing. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@creadur.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org