001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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.sensor.internal;
021
022import java.util.Arrays;
023import java.util.Collection;
024import java.util.function.Predicate;
025import javax.annotation.Nullable;
026import org.sonar.api.batch.fs.InputFile;
027import org.sonar.api.batch.sensor.SensorDescriptor;
028import org.sonar.api.config.Configuration;
029
030import static java.util.Arrays.asList;
031
032public class DefaultSensorDescriptor implements SensorDescriptor {
033
034  private String name;
035  private String[] languages = new String[0];
036  private InputFile.Type type = null;
037  private String[] ruleRepositories = new String[0];
038  private boolean global = false;
039  private Predicate<Configuration> configurationPredicate;
040
041  public String name() {
042    return name;
043  }
044
045  public Collection<String> languages() {
046    return Arrays.asList(languages);
047  }
048
049  @Nullable
050  public InputFile.Type type() {
051    return type;
052  }
053
054  public Collection<String> ruleRepositories() {
055    return Arrays.asList(ruleRepositories);
056  }
057
058  public Predicate<Configuration> configurationPredicate() {
059    return configurationPredicate;
060  }
061
062  public boolean isGlobal() {
063    return global;
064  }
065
066  @Override
067  public DefaultSensorDescriptor name(String name) {
068    this.name = name;
069    return this;
070  }
071
072  @Override
073  public DefaultSensorDescriptor onlyOnLanguage(String languageKey) {
074    return onlyOnLanguages(languageKey);
075  }
076
077  @Override
078  public DefaultSensorDescriptor onlyOnLanguages(String... languageKeys) {
079    this.languages = languageKeys;
080    return this;
081  }
082
083  @Override
084  public DefaultSensorDescriptor onlyOnFileType(InputFile.Type type) {
085    this.type = type;
086    return this;
087  }
088
089  @Override
090  public DefaultSensorDescriptor createIssuesForRuleRepository(String... repositoryKey) {
091    return createIssuesForRuleRepositories(repositoryKey);
092  }
093
094  @Override
095  public DefaultSensorDescriptor createIssuesForRuleRepositories(String... repositoryKeys) {
096    this.ruleRepositories = repositoryKeys;
097    return this;
098  }
099
100  @Override
101  public DefaultSensorDescriptor requireProperty(String... propertyKey) {
102    return requireProperties(propertyKey);
103  }
104
105  @Override
106  public DefaultSensorDescriptor requireProperties(String... propertyKeys) {
107    this.configurationPredicate = config -> asList(propertyKeys).stream().allMatch(config::hasKey);
108    return this;
109  }
110
111  @Override
112  public SensorDescriptor global() {
113    this.global = true;
114    return this;
115  }
116
117  @Override
118  public SensorDescriptor onlyWhenConfiguration(Predicate<Configuration> configurationPredicate) {
119    this.configurationPredicate = configurationPredicate;
120    return this;
121  }
122
123}