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.ObjectUtils;
023    import org.apache.commons.lang.StringUtils;
024    import org.sonar.api.batch.debt.DebtRemediationFunction;
025    import org.sonar.api.rule.RuleKey;
026    import org.sonar.api.rule.RuleStatus;
027    import org.sonar.api.rule.Severity;
028    
029    import javax.annotation.Nullable;
030    
031    import java.util.HashMap;
032    import java.util.Map;
033    
034    public class NewRule {
035    
036      private static final String DEFAULT_SEVERITY = Severity.defaultSeverity();
037    
038      final RuleKey key;
039      Integer id;
040      String name, description, severity = DEFAULT_SEVERITY, internalKey, debtSubCharacteristic;
041      DebtRemediationFunction debtRemediationFunction;
042      RuleStatus status = RuleStatus.defaultStatus();
043      Map<String, NewRuleParam> params = new HashMap<String, NewRuleParam>();
044    
045      NewRule(RuleKey key) {
046        this.key = key;
047      }
048    
049      public NewRule setId(@Nullable Integer id) {
050        this.id = id;
051        return this;
052      }
053    
054      public NewRule setDescription(@Nullable String description) {
055        this.description = description;
056        return this;
057      }
058    
059      public NewRule setName(@Nullable String s) {
060        this.name = s;
061        return this;
062      }
063    
064      public NewRule setSeverity(@Nullable String severity) {
065        this.severity = StringUtils.defaultIfBlank(severity, DEFAULT_SEVERITY);
066        return this;
067      }
068    
069      public NewRule setStatus(@Nullable RuleStatus s) {
070        this.status = (RuleStatus) ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
071        return this;
072      }
073    
074      public NewRule setInternalKey(@Nullable String s) {
075        this.internalKey = s;
076        return this;
077      }
078    
079      public NewRule setDebtSubCharacteristic(@Nullable String c) {
080        this.debtSubCharacteristic = c;
081        return this;
082      }
083    
084      public NewRule setDebtRemediationFunction(@Nullable DebtRemediationFunction f) {
085        this.debtRemediationFunction = f;
086        return this;
087      }
088    
089      public NewRuleParam addParam(String paramKey) {
090        if (params.containsKey(paramKey)) {
091          throw new IllegalStateException(String.format("Parameter '%s' already exists on rule '%s'", paramKey, key));
092        }
093        NewRuleParam param = new NewRuleParam(paramKey);
094        params.put(paramKey, param);
095        return param;
096      }
097    }