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;
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 setLanguage(@Nullable String language) {
063        this.language = language;
064        return this;
065      }
066    
067      public NewActiveRule setParam(String key, @Nullable String value) {
068        // possible improvement : check that the param key exists in rule definition
069        if (value == null) {
070          params.remove(key);
071        } else {
072          params.put(key, value);
073        }
074        return this;
075      }
076    
077      public Map<String, String> params() {
078        return params;
079      }
080    
081      public ActiveRulesBuilder activate() {
082        builder.activate(this);
083        return builder;
084      }
085    }