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.rule.internal;
021
022import org.apache.commons.lang.StringUtils;
023import org.sonar.api.rule.RuleKey;
024import org.sonar.api.rule.Severity;
025
026import javax.annotation.Nullable;
027
028import java.util.HashMap;
029import java.util.Map;
030
031/**
032 * @since 4.2
033 */
034public class NewActiveRule {
035  final RuleKey ruleKey;
036  String name;
037  String severity = Severity.defaultSeverity();
038  Map<String, String> params = new HashMap<>();
039  String internalKey;
040  String language;
041  String templateRuleKey;
042  private final ActiveRulesBuilder builder;
043
044  NewActiveRule(ActiveRulesBuilder builder, RuleKey ruleKey) {
045    this.builder = builder;
046    this.ruleKey = ruleKey;
047  }
048
049  public NewActiveRule setName(String name) {
050    this.name = name;
051    return this;
052  }
053
054  public NewActiveRule setSeverity(@Nullable String severity) {
055    this.severity = StringUtils.defaultIfBlank(severity, Severity.defaultSeverity());
056    return this;
057  }
058
059  public NewActiveRule setInternalKey(@Nullable String internalKey) {
060    this.internalKey = internalKey;
061    return this;
062  }
063
064  public NewActiveRule setTemplateRuleKey(@Nullable String templateRuleKey) {
065    this.templateRuleKey = templateRuleKey;
066    return this;
067  }
068
069  public NewActiveRule setLanguage(@Nullable String language) {
070    this.language = language;
071    return this;
072  }
073
074  public NewActiveRule setParam(String key, @Nullable String value) {
075    // possible improvement : check that the param key exists in rule definition
076    if (value == null) {
077      params.remove(key);
078    } else {
079      params.put(key, value);
080    }
081    return this;
082  }
083
084  public Map<String, String> params() {
085    return params;
086  }
087
088  public ActiveRulesBuilder activate() {
089    builder.activate(this);
090    return builder;
091  }
092}