001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 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.rule.RuleKey;
025    import org.sonar.api.rule.Severity;
026    import org.sonar.api.rule.RuleStatus;
027    
028    import javax.annotation.Nullable;
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    public class NewRule {
033    
034      private static final String DEFAULT_SEVERITY = Severity.defaultSeverity();
035    
036      final RuleKey key;
037      Integer id;
038      String name, description, severity = DEFAULT_SEVERITY, metadata;
039      RuleStatus status = RuleStatus.defaultStatus();
040      Map<String, NewRuleParam> params = new HashMap<String, NewRuleParam>();
041    
042      NewRule(RuleKey key) {
043        this.key = key;
044      }
045    
046      public NewRule setId(@Nullable Integer id) {
047        this.id = id;
048        return this;
049      }
050    
051      public NewRule setDescription(@Nullable String description) {
052        this.description = description;
053        return this;
054      }
055    
056      public NewRule setName(@Nullable String s) {
057        this.name = s;
058        return this;
059      }
060    
061      public NewRule setSeverity(@Nullable String severity) {
062        this.severity = StringUtils.defaultIfBlank(severity, DEFAULT_SEVERITY);
063        return this;
064      }
065    
066      public NewRule setStatus(@Nullable RuleStatus s) {
067        this.status = (RuleStatus)ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
068        return this;
069      }
070    
071      public NewRule setMetadata(@Nullable String metadata) {
072        this.metadata = metadata;
073        return this;
074      }
075    
076      public NewRuleParam addParam(String paramKey) {
077        if (params.containsKey(paramKey)) {
078          throw new IllegalStateException(String.format("Parameter '%s' already exists on rule '%s'", paramKey, key));
079        }
080        NewRuleParam param = new NewRuleParam(paramKey);
081        params.put(paramKey, param);
082        return param;
083      }
084    }