You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewRule.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.batch.rule.internal;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import javax.annotation.Nullable;
  24. import org.apache.commons.lang3.ObjectUtils;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.sonar.api.rule.RuleKey;
  27. import org.sonar.api.rule.RuleStatus;
  28. import org.sonar.api.rule.Severity;
  29. public class NewRule {
  30. private static final String DEFAULT_SEVERITY = Severity.defaultSeverity();
  31. final RuleKey key;
  32. String name;
  33. String description;
  34. String severity = DEFAULT_SEVERITY;
  35. String type;
  36. String internalKey;
  37. RuleStatus status = RuleStatus.defaultStatus();
  38. Map<String, NewRuleParam> params = new HashMap<>();
  39. public NewRule(RuleKey key) {
  40. this.key = key;
  41. }
  42. public NewRule setDescription(@Nullable String description) {
  43. this.description = description;
  44. return this;
  45. }
  46. public NewRule setName(@Nullable String s) {
  47. this.name = s;
  48. return this;
  49. }
  50. public NewRule setSeverity(@Nullable String severity) {
  51. this.severity = StringUtils.defaultIfBlank(severity, DEFAULT_SEVERITY);
  52. return this;
  53. }
  54. public NewRule setType(@Nullable String type) {
  55. this.type = type;
  56. return this;
  57. }
  58. public NewRule setStatus(@Nullable RuleStatus s) {
  59. this.status = ObjectUtils.defaultIfNull(s, RuleStatus.defaultStatus());
  60. return this;
  61. }
  62. public NewRule setInternalKey(@Nullable String s) {
  63. this.internalKey = s;
  64. return this;
  65. }
  66. public NewRuleParam addParam(String paramKey) {
  67. if (params.containsKey(paramKey)) {
  68. throw new IllegalStateException(String.format("Parameter '%s' already exists on rule '%s'", paramKey, key));
  69. }
  70. NewRuleParam param = new NewRuleParam(paramKey);
  71. params.put(paramKey, param);
  72. return param;
  73. }
  74. }