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.scan.filesystem;
021    
022    import com.google.common.base.Function;
023    import com.google.common.collect.ArrayListMultimap;
024    import com.google.common.collect.Collections2;
025    import com.google.common.collect.ListMultimap;
026    import com.google.common.collect.Sets;
027    import org.apache.commons.lang.builder.EqualsBuilder;
028    
029    import javax.annotation.Nullable;
030    
031    import java.io.FileFilter;
032    import java.util.Arrays;
033    import java.util.Collection;
034    import java.util.Map;
035    import java.util.Set;
036    
037    /**
038     * @since 3.5
039     * @deprecated in 4.2. Replaced by {@link org.sonar.api.batch.fs.FileSystem} and
040     * {@link org.sonar.api.batch.fs.FilePredicate}
041     */
042    @Deprecated
043    public class FileQuery {
044    
045      private final ListMultimap<String, String> attributes = ArrayListMultimap.create();
046      private final Set<String> inclusions = Sets.newHashSet();
047      private final Set<String> exclusions = Sets.newHashSet();
048    
049      public static FileQuery on(FileType... types) {
050        FileQuery query = new FileQuery();
051        for (FileType type : types) {
052          query.on("TYPE", type.typeValue());
053        }
054        return query;
055      }
056    
057      public static FileQuery onSource() {
058        return onMain();
059      }
060    
061      /**
062       * @since 4.2
063       */
064      public static FileQuery onMain() {
065        FileQuery query = new FileQuery();
066        return query.on("TYPE", "MAIN");
067      }
068    
069      public static FileQuery onTest() {
070        FileQuery query = new FileQuery();
071        return query.on("TYPE", "TEST");
072      }
073    
074      private FileQuery() {
075      }
076    
077      public FileQuery on(String attribute, String... values) {
078        for (String value : values) {
079          attributes.put(attribute, value);
080        }
081        return this;
082      }
083    
084      public Map<String, Collection<String>> attributes() {
085        return attributes.asMap();
086      }
087    
088      public Collection<FileType> types() {
089        return Collections2.transform(attributes.get("TYPE"), new Function<String, FileType>() {
090          @Override
091          public FileType apply(@Nullable String input) {
092            return input != null ? FileType.valueOf(input) : null;
093          }
094        });
095      }
096    
097      public Collection<String> typeAttributes() {
098        return attributes.get("TYPE");
099      }
100    
101      public Collection<String> languages() {
102        return attributes.get("LANG");
103      }
104    
105      public FileQuery onLanguage(String... languages) {
106        return on("LANG", languages);
107      }
108    
109      public Collection<String> inclusions() {
110        return inclusions;
111      }
112    
113      public FileQuery withInclusions(String... inclusions) {
114        this.inclusions.addAll(Arrays.asList(inclusions));
115        return this;
116      }
117    
118      public Collection<String> exclusions() {
119        return exclusions;
120      }
121    
122      public FileQuery withExclusions(String... exclusions) {
123        this.exclusions.addAll(Arrays.asList(exclusions));
124        return this;
125      }
126    
127      public Collection<FileFilter> filters() {
128        throw new UnsupportedOperationException("TODO");
129      }
130    
131      @Override
132      public boolean equals(Object obj) {
133        if (obj == null) {
134          return false;
135        }
136        if (obj == this) {
137          return true;
138        }
139        if (obj.getClass() != getClass()) {
140          return false;
141        }
142        FileQuery rhs = (FileQuery) obj;
143        return new EqualsBuilder()
144          .append(attributes, rhs.attributes)
145          .append(exclusions, rhs.exclusions)
146          .append(inclusions, rhs.inclusions)
147          .isEquals();
148      }
149    
150    }