001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 org.sonar.api.batch.fs.FilePredicate; 023import org.sonar.api.batch.fs.FilePredicates; 024import org.sonar.api.batch.fs.InputFile; 025 026import java.io.File; 027import java.nio.file.Path; 028import java.util.ArrayList; 029import java.util.Arrays; 030import java.util.Collection; 031import java.util.List; 032 033/** 034 * Factory of {@link org.sonar.api.batch.fs.FilePredicate} 035 * 036 * @since 4.2 037 */ 038public class DefaultFilePredicates implements FilePredicates { 039 040 private final Path baseDir; 041 042 /** 043 * Client code should use {@link org.sonar.api.batch.fs.FileSystem#predicates()} to get an instance 044 */ 045 DefaultFilePredicates(Path baseDir) { 046 this.baseDir = baseDir; 047 } 048 049 /** 050 * Returns a predicate that always evaluates to true 051 */ 052 @Override 053 public FilePredicate all() { 054 return TruePredicate.TRUE; 055 } 056 057 /** 058 * Returns a predicate that always evaluates to false 059 */ 060 @Override 061 public FilePredicate none() { 062 return FalsePredicate.FALSE; 063 } 064 065 @Override 066 public FilePredicate hasAbsolutePath(String s) { 067 return new AbsolutePathPredicate(s, baseDir); 068 } 069 070 /** 071 * non-normalized path and Windows-style path are supported 072 */ 073 @Override 074 public FilePredicate hasRelativePath(String s) { 075 return new RelativePathPredicate(s); 076 } 077 078 @Override 079 public FilePredicate matchesPathPattern(String inclusionPattern) { 080 return new PathPatternPredicate(PathPattern.create(inclusionPattern)); 081 } 082 083 @Override 084 public FilePredicate matchesPathPatterns(String[] inclusionPatterns) { 085 if (inclusionPatterns.length == 0) { 086 return TruePredicate.TRUE; 087 } 088 FilePredicate[] predicates = new FilePredicate[inclusionPatterns.length]; 089 for (int i = 0; i < inclusionPatterns.length; i++) { 090 predicates[i] = new PathPatternPredicate(PathPattern.create(inclusionPatterns[i])); 091 } 092 return or(predicates); 093 } 094 095 @Override 096 public FilePredicate doesNotMatchPathPattern(String exclusionPattern) { 097 return not(matchesPathPattern(exclusionPattern)); 098 } 099 100 @Override 101 public FilePredicate doesNotMatchPathPatterns(String[] exclusionPatterns) { 102 if (exclusionPatterns.length == 0) { 103 return TruePredicate.TRUE; 104 } 105 return not(matchesPathPatterns(exclusionPatterns)); 106 } 107 108 @Override 109 public FilePredicate hasPath(String s) { 110 File file = new File(s); 111 if (file.isAbsolute()) { 112 return hasAbsolutePath(s); 113 } 114 return hasRelativePath(s); 115 } 116 117 @Override 118 public FilePredicate is(File ioFile) { 119 if (ioFile.isAbsolute()) { 120 return hasAbsolutePath(ioFile.getAbsolutePath()); 121 } 122 return hasRelativePath(ioFile.getPath()); 123 } 124 125 @Override 126 public FilePredicate hasLanguage(String language) { 127 return new LanguagePredicate(language); 128 } 129 130 @Override 131 public FilePredicate hasLanguages(Collection<String> languages) { 132 List<FilePredicate> list = new ArrayList<>(); 133 for (String language : languages) { 134 list.add(hasLanguage(language)); 135 } 136 return or(list); 137 } 138 139 @Override 140 public FilePredicate hasLanguages(String... languages) { 141 List<FilePredicate> list = new ArrayList<>(); 142 for (String language : languages) { 143 list.add(hasLanguage(language)); 144 } 145 return or(list); 146 } 147 148 @Override 149 public FilePredicate hasStatus(InputFile.Status status) { 150 return new StatusPredicate(status); 151 } 152 153 @Override 154 public FilePredicate hasType(InputFile.Type type) { 155 return new TypePredicate(type); 156 } 157 158 @Override 159 public FilePredicate not(FilePredicate p) { 160 return new NotPredicate(p); 161 } 162 163 @Override 164 public FilePredicate or(Collection<FilePredicate> or) { 165 return OrPredicate.create(or); 166 } 167 168 @Override 169 public FilePredicate or(FilePredicate... or) { 170 return OrPredicate.create(Arrays.asList(or)); 171 } 172 173 @Override 174 public FilePredicate or(FilePredicate first, FilePredicate second) { 175 return OrPredicate.create(Arrays.asList(first, second)); 176 } 177 178 @Override 179 public FilePredicate and(Collection<FilePredicate> and) { 180 return AndPredicate.create(and); 181 } 182 183 @Override 184 public FilePredicate and(FilePredicate... and) { 185 return AndPredicate.create(Arrays.asList(and)); 186 } 187 188 @Override 189 public FilePredicate and(FilePredicate first, FilePredicate second) { 190 return AndPredicate.create(Arrays.asList(first, second)); 191 } 192}