001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.api.batch.fs.internal;
021
022 import org.sonar.api.batch.fs.FilePredicate;
023 import org.sonar.api.batch.fs.FilePredicates;
024 import org.sonar.api.batch.fs.InputFile;
025
026 import java.io.File;
027 import java.util.ArrayList;
028 import java.util.Arrays;
029 import java.util.Collection;
030 import java.util.List;
031
032 /**
033 * Factory of {@link org.sonar.api.batch.fs.FilePredicate}
034 *
035 * @since 4.2
036 */
037 public class DefaultFilePredicates implements FilePredicates {
038 /**
039 * Client code should use {@link org.sonar.api.batch.fs.FileSystem#predicates()} to get an instance
040 */
041 DefaultFilePredicates() {
042 }
043
044 /**
045 * Returns a predicate that always evaluates to true
046 */
047 public FilePredicate all() {
048 return TruePredicate.TRUE;
049 }
050
051 /**
052 * Returns a predicate that always evaluates to false
053 */
054 public FilePredicate none() {
055 return FalsePredicate.FALSE;
056 }
057
058 /**
059 * Warning - not efficient because absolute path is not indexed yet.
060 */
061 public FilePredicate hasAbsolutePath(String s) {
062 return new AbsolutePathPredicate(s);
063 }
064
065 /**
066 * TODO document that non-normalized path and Windows-style path are supported
067 */
068 public FilePredicate hasRelativePath(String s) {
069 return new RelativePathPredicate(s);
070 }
071
072 public FilePredicate matchesPathPattern(String inclusionPattern) {
073 return new PathPatternPredicate(PathPattern.create(inclusionPattern));
074 }
075
076 public FilePredicate matchesPathPatterns(String[] inclusionPatterns) {
077 if (inclusionPatterns.length == 0) {
078 return TruePredicate.TRUE;
079 }
080 FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length];
081 for (int i = 0; i < inclusionPatterns.length; i++) {
082 predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i]));
083 }
084 return or(predicates);
085 }
086
087 public FilePredicate doesNotMatchPathPattern(String exclusionPattern) {
088 return not(matchesPathPattern(exclusionPattern));
089 }
090
091 public FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) {
092 if (exclusionPatterns.length == 0) {
093 return TruePredicate.TRUE;
094 }
095 return not(matchesPathPatterns(exclusionPatterns));
096 }
097
098 public FilePredicate hasPath(String s) {
099 File file = new File(s);
100 if (file.isAbsolute()) {
101 return hasAbsolutePath(s);
102 }
103 return hasRelativePath(s);
104 }
105
106 public FilePredicate is(File ioFile) {
107 if (ioFile.isAbsolute()) {
108 return hasAbsolutePath(ioFile.getAbsolutePath());
109 }
110 return hasRelativePath(ioFile.getPath());
111 }
112
113 public FilePredicate hasLanguage(String language) {
114 return new LanguagePredicate(language);
115 }
116
117 public FilePredicate hasLanguages(Collection<String> languages) {
118 List<FilePredicate> list = new ArrayList<FilePredicate>();
119 for (String language : languages) {
120 list.add(hasLanguage(language));
121 }
122 return or(list);
123 }
124
125 public FilePredicate hasLanguages(String... languages) {
126 List<FilePredicate> list = new ArrayList<FilePredicate>();
127 for (String language : languages) {
128 list.add(hasLanguage(language));
129 }
130 return or(list);
131 }
132
133 public FilePredicate hasStatus(InputFile.Status status) {
134 return new StatusPredicate(status);
135 }
136
137 public FilePredicate hasType(InputFile.Type type) {
138 return new TypePredicate(type);
139 }
140
141 public FilePredicate not(FilePredicate p) {
142 return new NotPredicate(p);
143 }
144
145 public FilePredicate or(Collection<FilePredicate> or) {
146 return new OrPredicate(or);
147 }
148
149 public FilePredicate or(FilePredicate... or) {
150 return new OrPredicate(Arrays.asList(or));
151 }
152
153 public FilePredicate or(FilePredicate first, FilePredicate second) {
154 return new OrPredicate(Arrays.asList(first, second));
155 }
156
157 public FilePredicate and(Collection<FilePredicate> and) {
158 return new AndPredicate(and);
159 }
160
161 public FilePredicate and(FilePredicate... and) {
162 return new AndPredicate(Arrays.asList(and));
163 }
164
165 public FilePredicate and(FilePredicate first, FilePredicate second) {
166 return new AndPredicate(Arrays.asList(first, second));
167 }
168 }