001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact 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 internalKey; 041 RuleStatus status = RuleStatus.defaultStatus(); 042 Map<String, NewRuleParam> params = new HashMap<>(); 043 044 NewRule(RuleKey key) { 045 this.key = key; 046 } 047 048 public NewRule setId(@Nullable Integer id) { 049 this.id = id; 050 return this; 051 } 052 053 public NewRule setDescription(@Nullable String description) { 054 this.description = description; 055 return this; 056 } 057 058 public NewRule setName(@Nullable String s) { 059 this.name = s; 060 return this; 061 } 062 063 public NewRule setSeverity(@Nullable String severity) { 064 this.severity = StringUtils.defaultIfBlank(severity, DEFAULT_SEVERITY); 065 return this; 066 } 067 068 public NewRule setStatus(@Nullable RuleStatus s) { 069 this.status = (RuleStatus) ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus()); 070 return this; 071 } 072 073 public NewRule setInternalKey(@Nullable String s) { 074 this.internalKey = s; 075 return this; 076 } 077 078 public NewRuleParam addParam(String paramKey) { 079 if (params.containsKey(paramKey)) { 080 throw new IllegalStateException(String.format("Parameter '%s' already exists on rule '%s'", paramKey, key)); 081 } 082 NewRuleParam param = new NewRuleParam(paramKey); 083 params.put(paramKey, param); 084 return param; 085 } 086}