001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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 java.util.HashMap;
023import java.util.Map;
024import javax.annotation.Nullable;
025import org.apache.commons.lang.ObjectUtils;
026import org.apache.commons.lang.StringUtils;
027import org.sonar.api.rule.RuleKey;
028import org.sonar.api.rule.RuleStatus;
029import org.sonar.api.rule.Severity;
030
031public class NewRule {
032
033  private static final String DEFAULT_SEVERITY = Severity.defaultSeverity();
034
035  final RuleKey key;
036  Integer id;
037  String name;
038  String description;
039  String severity = DEFAULT_SEVERITY;
040  String type;
041  String internalKey;
042  RuleStatus status = RuleStatus.defaultStatus();
043  Map<String, NewRuleParam> params = new HashMap<>();
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 setType(@Nullable String type) {
070    this.type = type;
071    return this;
072  }
073
074  public NewRule setStatus(@Nullable RuleStatus s) {
075    this.status = (RuleStatus) ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
076    return this;
077  }
078
079  public NewRule setInternalKey(@Nullable String s) {
080    this.internalKey = s;
081    return this;
082  }
083
084  public NewRuleParam addParam(String paramKey) {
085    if (params.containsKey(paramKey)) {
086      throw new IllegalStateException(String.format("Parameter '%s' already exists on rule '%s'", paramKey, key));
087    }
088    NewRuleParam param = new NewRuleParam(paramKey);
089    params.put(paramKey, param);
090    return param;
091  }
092}