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 */
020package org.sonar.api.batch.fs.internal;
021
022import com.google.common.collect.Lists;
023import org.sonar.api.batch.fs.FilePredicate;
024import org.sonar.api.batch.fs.FilePredicates;
025import org.sonar.api.batch.fs.InputFile;
026
027import java.io.File;
028import java.util.Arrays;
029import java.util.Collection;
030import java.util.List;
031
032/**
033 * Factory of {@link org.sonar.api.batch.fs.FilePredicate}
034 *
035 * @since 4.2
036 */
037public 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 = Lists.newArrayList();
119    for (String language : languages) {
120      list.add(hasLanguage(language));
121    }
122    return or(list);
123  }
124
125  public FilePredicate hasStatus(InputFile.Status status) {
126    return new StatusPredicate(status);
127  }
128
129  public FilePredicate hasType(InputFile.Type type) {
130    return new TypePredicate(type);
131  }
132
133  public FilePredicate not(FilePredicate p) {
134    return new NotPredicate(p);
135  }
136
137  public FilePredicate or(Collection<FilePredicate> or) {
138    return new OrPredicate(or);
139  }
140
141  public FilePredicate or(FilePredicate... or) {
142    return new OrPredicate(Arrays.asList(or));
143  }
144
145  public FilePredicate or(FilePredicate first, FilePredicate second) {
146    return new OrPredicate(Arrays.asList(first, second));
147  }
148
149  public FilePredicate and(Collection<FilePredicate> and) {
150    return new AndPredicate(and);
151  }
152
153  public FilePredicate and(FilePredicate... and) {
154    return new AndPredicate(Arrays.asList(and));
155  }
156
157  public FilePredicate and(FilePredicate first, FilePredicate second) {
158    return new AndPredicate(Arrays.asList(first, second));
159  }
160}