3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.api.batch.fs.internal.predicates;
24 import java.nio.file.Path;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.List;
29 import org.sonar.api.batch.fs.FilePredicate;
30 import org.sonar.api.batch.fs.FilePredicates;
31 import org.sonar.api.batch.fs.InputFile;
32 import org.sonar.api.batch.fs.InputFile.Status;
33 import org.sonar.api.batch.fs.internal.PathPattern;
36 * Factory of {@link FilePredicate}
40 public class DefaultFilePredicates implements FilePredicates {
42 private final Path baseDir;
45 * Client code should use {@link org.sonar.api.batch.fs.FileSystem#predicates()} to get an instance
47 public DefaultFilePredicates(Path baseDir) {
48 this.baseDir = baseDir;
52 * Returns a predicate that always evaluates to true
55 public FilePredicate all() {
56 return TruePredicate.TRUE;
60 * Returns a predicate that always evaluates to false
63 public FilePredicate none() {
64 return FalsePredicate.FALSE;
68 public FilePredicate hasAbsolutePath(String s) {
69 return new AbsolutePathPredicate(s, baseDir);
73 * non-normalized path and Windows-style path are supported
76 public FilePredicate hasRelativePath(String s) {
77 return new RelativePathPredicate(s);
81 public FilePredicate hasFilename(String s) {
82 return new FilenamePredicate(s);
86 public FilePredicate hasExtension(String s) {
87 return new FileExtensionPredicate(s);
91 public FilePredicate hasURI(URI uri) {
92 return new URIPredicate(uri, baseDir);
96 public FilePredicate matchesPathPattern(String inclusionPattern) {
97 return new PathPatternPredicate(PathPattern.create(inclusionPattern));
101 public FilePredicate matchesPathPatterns(String[] inclusionPatterns) {
102 if (inclusionPatterns.length == 0) {
103 return TruePredicate.TRUE;
105 FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length];
106 for (int i = 0; i < inclusionPatterns.length; i++) {
107 predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i]));
109 return or(predicates);
113 public FilePredicate doesNotMatchPathPattern(String exclusionPattern) {
114 return not(matchesPathPattern(exclusionPattern));
118 public FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) {
119 if (exclusionPatterns.length == 0) {
120 return TruePredicate.TRUE;
122 return not(matchesPathPatterns(exclusionPatterns));
126 public FilePredicate hasPath(String s) {
127 File file = new File(s);
128 if (file.isAbsolute()) {
129 return hasAbsolutePath(s);
131 return hasRelativePath(s);
135 public FilePredicate is(File ioFile) {
136 return new IsPredicate(ioFile.toPath());
140 public FilePredicate hasLanguage(String language) {
141 return new LanguagePredicate(language);
145 public FilePredicate hasLanguages(Collection<String> languages) {
146 List<FilePredicate> list = new ArrayList<>();
147 for (String language : languages) {
148 list.add(hasLanguage(language));
154 public FilePredicate hasLanguages(String... languages) {
155 List<FilePredicate> list = new ArrayList<>();
156 for (String language : languages) {
157 list.add(hasLanguage(language));
163 public FilePredicate hasType(InputFile.Type type) {
164 return new TypePredicate(type);
168 public FilePredicate not(FilePredicate p) {
169 return new NotPredicate(p);
173 public FilePredicate or(Collection<FilePredicate> or) {
174 return OrPredicate.create(or);
178 public FilePredicate or(FilePredicate... or) {
179 return OrPredicate.create(Arrays.asList(or));
183 public FilePredicate or(FilePredicate first, FilePredicate second) {
184 return OrPredicate.create(Arrays.asList(first, second));
188 public FilePredicate and(Collection<FilePredicate> and) {
189 return AndPredicate.create(and);
193 public FilePredicate and(FilePredicate... and) {
194 return AndPredicate.create(Arrays.asList(and));
198 public FilePredicate and(FilePredicate first, FilePredicate second) {
199 return AndPredicate.create(Arrays.asList(first, second));
203 public FilePredicate hasStatus(Status status) {
204 return new StatusPredicate(status);
208 public FilePredicate hasAnyStatus() {
209 return new StatusPredicate(null);