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.

RuleImpl.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.ce.task.projectanalysis.issue;
  21. import com.google.common.base.MoreObjects;
  22. import java.util.Set;
  23. import javax.annotation.CheckForNull;
  24. import javax.annotation.Nullable;
  25. import javax.annotation.concurrent.Immutable;
  26. import org.sonar.api.rule.RuleKey;
  27. import org.sonar.api.rule.RuleStatus;
  28. import org.sonar.api.rules.RuleType;
  29. import org.sonar.api.server.debt.DebtRemediationFunction;
  30. import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
  31. import org.sonar.db.rule.RuleDto;
  32. import static com.google.common.collect.Sets.union;
  33. @Immutable
  34. public class RuleImpl implements Rule {
  35. private final String uuid;
  36. private final RuleKey key;
  37. private final String name;
  38. private final String language;
  39. private final RuleStatus status;
  40. private final Set<String> tags;
  41. private final DebtRemediationFunction remediationFunction;
  42. private final RuleType type;
  43. private final String pluginKey;
  44. private final boolean isExternal;
  45. private final boolean isAdHoc;
  46. public RuleImpl(RuleDto dto) {
  47. this.uuid = dto.getUuid();
  48. this.key = dto.getKey();
  49. this.name = dto.getName();
  50. this.language = dto.getLanguage();
  51. this.status = dto.getStatus();
  52. this.tags = union(dto.getSystemTags(), dto.getTags());
  53. this.remediationFunction = effectiveRemediationFunction(dto);
  54. this.type = RuleType.valueOfNullable(dto.getType());
  55. this.pluginKey = dto.getPluginKey();
  56. this.isExternal = dto.isExternal();
  57. this.isAdHoc = dto.isAdHoc();
  58. }
  59. @Override
  60. public String getUuid() {
  61. return this.uuid;
  62. }
  63. @Override
  64. public RuleKey getKey() {
  65. return key;
  66. }
  67. @Override
  68. public String getName() {
  69. return name;
  70. }
  71. @Override
  72. @CheckForNull
  73. public String getLanguage() {
  74. return language;
  75. }
  76. @Override
  77. public RuleStatus getStatus() {
  78. return status;
  79. }
  80. @Override
  81. public Set<String> getTags() {
  82. return tags;
  83. }
  84. @Override
  85. public DebtRemediationFunction getRemediationFunction() {
  86. return remediationFunction;
  87. }
  88. @Override
  89. public RuleType getType() {
  90. return type;
  91. }
  92. @CheckForNull
  93. @Override
  94. public String getPluginKey() {
  95. return pluginKey;
  96. }
  97. @Override
  98. public boolean equals(@Nullable Object o) {
  99. if (this == o) {
  100. return true;
  101. }
  102. if (o == null || getClass() != o.getClass()) {
  103. return false;
  104. }
  105. RuleImpl rule = (RuleImpl) o;
  106. return key.equals(rule.key);
  107. }
  108. @Override
  109. public int hashCode() {
  110. return key.hashCode();
  111. }
  112. @Override
  113. public String toString() {
  114. return MoreObjects.toStringHelper(this)
  115. .add("uuid", uuid)
  116. .add("key", key)
  117. .add("name", name)
  118. .add("language", language)
  119. .add("status", status)
  120. .add("tags", tags)
  121. .add("pluginKey", pluginKey)
  122. .toString();
  123. }
  124. @CheckForNull
  125. private static DebtRemediationFunction effectiveRemediationFunction(RuleDto dto) {
  126. String fn = dto.getRemediationFunction();
  127. if (fn != null) {
  128. return new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(fn), dto.getRemediationGapMultiplier(), dto.getRemediationBaseEffort());
  129. }
  130. String defaultFn = dto.getDefRemediationFunction();
  131. if (defaultFn != null) {
  132. return new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(defaultFn), dto.getDefRemediationGapMultiplier(), dto.getDefRemediationBaseEffort());
  133. }
  134. return null;
  135. }
  136. @Override
  137. public boolean isAdHoc() {
  138. return isAdHoc;
  139. }
  140. @Override
  141. public boolean isExternal() {
  142. return isExternal;
  143. }
  144. }