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      @Override
048      public FilePredicate all() {
049        return TruePredicate.TRUE;
050      }
051    
052      /**
053       * Returns a predicate that always evaluates to false
054       */
055      @Override
056      public FilePredicate none() {
057        return FalsePredicate.FALSE;
058      }
059    
060      /**
061       * Warning - not efficient because absolute path is not indexed yet.
062       */
063      @Override
064      public FilePredicate hasAbsolutePath(String s) {
065        return new AbsolutePathPredicate(s);
066      }
067    
068      /**
069       * TODO document that non-normalized path and Windows-style path are supported
070       */
071      @Override
072      public FilePredicate hasRelativePath(String s) {
073        return new RelativePathPredicate(s);
074      }
075    
076      @Override
077      public FilePredicate matchesPathPattern(String inclusionPattern) {
078        return new PathPatternPredicate(PathPattern.create(inclusionPattern));
079      }
080    
081      @Override
082      public FilePredicate matchesPathPatterns(String[] inclusionPatterns) {
083        if (inclusionPatterns.length == 0) {
084          return TruePredicate.TRUE;
085        }
086        FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length];
087        for (int i = 0; i < inclusionPatterns.length; i++) {
088          predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i]));
089        }
090        return or(predicates);
091      }
092    
093      @Override
094      public FilePredicate doesNotMatchPathPattern(String exclusionPattern) {
095        return not(matchesPathPattern(exclusionPattern));
096      }
097    
098      @Override
099      public FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) {
100        if (exclusionPatterns.length == 0) {
101          return TruePredicate.TRUE;
102        }
103        return not(matchesPathPatterns(exclusionPatterns));
104      }
105    
106      @Override
107      public FilePredicate hasPath(String s) {
108        File file = new File(s);
109        if (file.isAbsolute()) {
110          return hasAbsolutePath(s);
111        }
112        return hasRelativePath(s);
113      }
114    
115      @Override
116      public FilePredicate is(File ioFile) {
117        if (ioFile.isAbsolute()) {
118          return hasAbsolutePath(ioFile.getAbsolutePath());
119        }
120        return hasRelativePath(ioFile.getPath());
121      }
122    
123      @Override
124      public FilePredicate hasLanguage(String language) {
125        return new LanguagePredicate(language);
126      }
127    
128      @Override
129      public FilePredicate hasLanguages(Collection<String> languages) {
130        List<FilePredicate> list = new ArrayList<FilePredicate>();
131        for (String language : languages) {
132          list.add(hasLanguage(language));
133        }
134        return or(list);
135      }
136    
137      @Override
138      public FilePredicate hasLanguages(String... languages) {
139        List<FilePredicate> list = new ArrayList<FilePredicate>();
140        for (String language : languages) {
141          list.add(hasLanguage(language));
142        }
143        return or(list);
144      }
145    
146      @Override
147      public FilePredicate hasStatus(InputFile.Status status) {
148        return new StatusPredicate(status);
149      }
150    
151      @Override
152      public FilePredicate hasType(InputFile.Type type) {
153        return new TypePredicate(type);
154      }
155    
156      @Override
157      public FilePredicate not(FilePredicate p) {
158        return new NotPredicate(p);
159      }
160    
161      @Override
162      public FilePredicate or(Collection<FilePredicate> or) {
163        return new OrPredicate(or);
164      }
165    
166      @Override
167      public FilePredicate or(FilePredicate... or) {
168        return new OrPredicate(Arrays.asList(or));
169      }
170    
171      @Override
172      public FilePredicate or(FilePredicate first, FilePredicate second) {
173        return new OrPredicate(Arrays.asList(first, second));
174      }
175    
176      @Override
177      public FilePredicate and(Collection<FilePredicate> and) {
178        return new AndPredicate(and);
179      }
180    
181      @Override
182      public FilePredicate and(FilePredicate... and) {
183        return new AndPredicate(Arrays.asList(and));
184      }
185    
186      @Override
187      public FilePredicate and(FilePredicate first, FilePredicate second) {
188        return new AndPredicate(Arrays.asList(first, second));
189      }
190    }