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.scan.filesystem;
021
022import com.google.common.base.Function;
023import com.google.common.collect.ArrayListMultimap;
024import com.google.common.collect.Collections2;
025import com.google.common.collect.ListMultimap;
026import com.google.common.collect.Sets;
027import org.sonar.api.scan.filesystem.internal.InputFile;
028
029import javax.annotation.Nullable;
030import java.io.FileFilter;
031import java.util.Arrays;
032import java.util.Collection;
033import java.util.Map;
034import java.util.Set;
035
036/**
037 * @since 3.5
038 */
039public class FileQuery {
040
041  public static FileQuery on(FileType... types) {
042    FileQuery query = new FileQuery();
043    for (FileType type : types) {
044      query.on(InputFile.ATTRIBUTE_TYPE, type.name());
045    }
046    return query;
047  }
048
049  public static FileQuery onSource() {
050    return on(FileType.SOURCE);
051  }
052
053  public static FileQuery onTest() {
054    return on(FileType.TEST);
055  }
056
057  private final ListMultimap<String, String> attributes = ArrayListMultimap.create();
058  private final Set<String> inclusions = Sets.newHashSet();
059  private final Set<String> exclusions = Sets.newHashSet();
060
061  private FileQuery() {
062  }
063
064  public FileQuery on(String attribute, String... values) {
065    for (String value : values) {
066      attributes.put(attribute, value);
067    }
068    return this;
069  }
070
071  public Map<String, Collection<String>> attributes() {
072    return attributes.asMap();
073  }
074
075  public Collection<FileType> types() {
076    return Collections2.transform(attributes.get(InputFile.ATTRIBUTE_TYPE), new Function<String, FileType>() {
077      @Override
078      public FileType apply(@Nullable String input) {
079        return input != null ? FileType.valueOf(input) : null;
080      }
081    });
082  }
083
084  public Collection<String> languages() {
085    return attributes.get(InputFile.ATTRIBUTE_LANGUAGE);
086  }
087
088  public FileQuery onLanguage(String... languages) {
089    return on(InputFile.ATTRIBUTE_LANGUAGE, languages);
090  }
091
092  public Collection<String> inclusions() {
093    return inclusions;
094  }
095
096  public FileQuery withInclusions(String... inclusions) {
097    this.inclusions.addAll(Arrays.asList(inclusions));
098    return this;
099  }
100
101  public Collection<String> exclusions() {
102    return exclusions;
103  }
104
105  public FileQuery withExclusions(String... exclusions) {
106    this.exclusions.addAll(Arrays.asList(exclusions));
107    return this;
108  }
109
110  public Collection<FileFilter> filters() {
111    throw new UnsupportedOperationException("TODO");
112  }
113
114  public FileQuery withFilters(FileFilter... filters) {
115    throw new UnsupportedOperationException("TODO");
116  }
117}
118