001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 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;
021    
022    import com.google.common.collect.Lists;
023    import org.sonar.api.batch.fs.internal.PathPattern;
024    
025    import java.io.File;
026    import java.util.Arrays;
027    import java.util.Collection;
028    import java.util.List;
029    
030    /**
031     * Factory of {@link org.sonar.api.batch.fs.FilePredicate}
032     *
033     * @since 4.2
034     */
035    public class FilePredicates {
036      private static final FilePredicate ALWAYS_TRUE = new AlwaysTruePredicate();
037      private static final FilePredicate ALWAYS_FALSE = not(ALWAYS_TRUE);
038    
039      private FilePredicates() {
040        // only static stuff
041      }
042    
043      /**
044       * Returns a predicate that always evaluates to true
045       */
046      public static FilePredicate all() {
047        return ALWAYS_TRUE;
048      }
049    
050      /**
051       * Returns a predicate that always evaluates to false
052       */
053      public static FilePredicate none() {
054        return ALWAYS_FALSE;
055      }
056    
057      /**
058       * Warning - not efficient because absolute path is not indexed yet.
059       */
060      public static FilePredicate hasAbsolutePath(String s) {
061        return new AbsolutePathPredicate(s);
062      }
063    
064      /**
065       * TODO document that non-normalized path and Windows-style path are supported
066       */
067      public static FilePredicate hasRelativePath(String s) {
068        return new RelativePathPredicate(s);
069      }
070    
071      public static FilePredicate matchesPathPattern(String inclusionPattern) {
072        return new PathPatternPredicate(PathPattern.create(inclusionPattern));
073      }
074    
075      public static FilePredicate matchesPathPatterns(String[] inclusionPatterns) {
076        if (inclusionPatterns.length == 0) {
077          return ALWAYS_TRUE;
078        }
079        FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length];
080        for (int i = 0; i < inclusionPatterns.length; i++) {
081          predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i]));
082        }
083        return or(predicates);
084      }
085    
086      public static FilePredicate doesNotMatchPathPattern(String exclusionPattern) {
087        return not(matchesPathPattern(exclusionPattern));
088      }
089    
090      public static FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) {
091        if (exclusionPatterns.length == 0) {
092          return ALWAYS_TRUE;
093        }
094        return not(matchesPathPatterns(exclusionPatterns));
095      }
096    
097      public static FilePredicate hasPath(String s) {
098        File file = new File(s);
099        if (file.isAbsolute()) {
100          return hasAbsolutePath(s);
101        }
102        return hasRelativePath(s);
103      }
104    
105      public static FilePredicate is(File ioFile) {
106        if (ioFile.isAbsolute()) {
107          return hasAbsolutePath(ioFile.getAbsolutePath());
108        }
109        return hasRelativePath(ioFile.getPath());
110      }
111    
112      public static FilePredicate hasLanguage(String language) {
113        return new LanguagePredicate(language);
114      }
115    
116      public static FilePredicate hasLanguages(Collection<String> languages) {
117        List<FilePredicate> list = Lists.newArrayList();
118        for (String language : languages) {
119          list.add(hasLanguage(language));
120        }
121        return or(list);
122      }
123    
124      public static FilePredicate hasStatus(InputFile.Status status) {
125        return new StatusPredicate(status);
126      }
127    
128      public static FilePredicate hasType(InputFile.Type type) {
129        return new TypePredicate(type);
130      }
131    
132      public static FilePredicate not(FilePredicate p) {
133        return new NotPredicate(p);
134      }
135    
136      public static FilePredicate or(Collection<FilePredicate> or) {
137        return new OrPredicate(or);
138      }
139    
140      public static FilePredicate or(FilePredicate... or) {
141        return new OrPredicate(Arrays.asList(or));
142      }
143    
144      public static FilePredicate or(FilePredicate first, FilePredicate second) {
145        return new OrPredicate(Arrays.asList(first, second));
146      }
147    
148      public static FilePredicate and(Collection<FilePredicate> and) {
149        return new AndPredicate(and);
150      }
151    
152      public static FilePredicate and(FilePredicate... and) {
153        return new AndPredicate(Arrays.asList(and));
154      }
155    
156      public static FilePredicate and(FilePredicate first, FilePredicate second) {
157        return new AndPredicate(Arrays.asList(first, second));
158      }
159    
160      private static class AlwaysTruePredicate implements FilePredicate {
161        @Override
162        public boolean apply(InputFile inputFile) {
163          return true;
164        }
165      }
166    }