Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RuleImpl.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.server.computation.issue;
  21. import com.google.common.base.Objects;
  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.server.debt.DebtRemediationFunction;
  29. import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
  30. import org.sonar.db.rule.RuleDto;
  31. import static com.google.common.collect.Sets.union;
  32. @Immutable
  33. public class RuleImpl implements Rule {
  34. private final int id;
  35. private final RuleKey key;
  36. private final String name;
  37. private final RuleStatus status;
  38. private final boolean isActivated;
  39. private final Integer subCharacteristicId;
  40. private final Set<String> tags;
  41. private final DebtRemediationFunction remediationFunction;
  42. public RuleImpl(RuleDto dto, boolean isActivated) {
  43. this.id = dto.getId();
  44. this.key = dto.getKey();
  45. this.name = dto.getName();
  46. this.status = dto.getStatus();
  47. this.subCharacteristicId = effectiveCharacteristicId(dto);
  48. this.tags = union(dto.getSystemTags(), dto.getTags());
  49. this.remediationFunction = effectiveRemediationFunction(dto);
  50. this.isActivated = isActivated;
  51. }
  52. @Override
  53. public int getId() {
  54. return id;
  55. }
  56. @Override
  57. public RuleKey getKey() {
  58. return key;
  59. }
  60. @Override
  61. public String getName() {
  62. return name;
  63. }
  64. @Override
  65. public RuleStatus getStatus() {
  66. return status;
  67. }
  68. @Override
  69. public boolean isActivated() {
  70. return isActivated;
  71. }
  72. @Override
  73. public Set<String> getTags() {
  74. return tags;
  75. }
  76. @Override
  77. public Integer getSubCharacteristicId() {
  78. return subCharacteristicId;
  79. }
  80. @Override
  81. public DebtRemediationFunction getRemediationFunction() {
  82. return remediationFunction;
  83. }
  84. @Override
  85. public boolean equals(@Nullable Object o) {
  86. if (this == o) {
  87. return true;
  88. }
  89. if (o == null || getClass() != o.getClass()) {
  90. return false;
  91. }
  92. RuleImpl rule = (RuleImpl) o;
  93. return key.equals(rule.key);
  94. }
  95. @Override
  96. public int hashCode() {
  97. return key.hashCode();
  98. }
  99. @Override
  100. public String toString() {
  101. return Objects.toStringHelper(this)
  102. .add("id", id)
  103. .add("key", key)
  104. .add("name", name)
  105. .add("status", status)
  106. .add("isActivated", isActivated)
  107. .add("subCharacteristicId", subCharacteristicId)
  108. .add("tags", tags)
  109. .toString();
  110. }
  111. @CheckForNull
  112. private static Integer effectiveCharacteristicId(RuleDto dto) {
  113. if (isEnabledCharacteristicId(dto.getSubCharacteristicId())) {
  114. return dto.getSubCharacteristicId();
  115. }
  116. if (isEnabledCharacteristicId(dto.getDefaultSubCharacteristicId())) {
  117. return dto.getDefaultSubCharacteristicId();
  118. }
  119. return null;
  120. }
  121. private static boolean isEnabledCharacteristicId(@Nullable Integer id) {
  122. return (id != null) && (id.intValue() != RuleDto.DISABLED_CHARACTERISTIC_ID);
  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.getRemediationCoefficient(), dto.getRemediationOffset());
  129. }
  130. String defaultFn = dto.getDefaultRemediationFunction();
  131. if (defaultFn != null) {
  132. return new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(defaultFn), dto.getDefaultRemediationCoefficient(), dto.getDefaultRemediationOffset());
  133. }
  134. return null;
  135. }
  136. }