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