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.

DefaultDebtRemediationFunction.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.server.debt.internal;
  21. import javax.annotation.CheckForNull;
  22. import javax.annotation.Nullable;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.apache.commons.lang.builder.EqualsBuilder;
  25. import org.sonar.api.server.debt.DebtRemediationFunction;
  26. import org.sonar.api.utils.Duration;
  27. import static org.sonar.api.utils.Preconditions.checkArgument;
  28. public class DefaultDebtRemediationFunction implements DebtRemediationFunction {
  29. private static final int HOURS_IN_DAY = 24;
  30. private final Type type;
  31. private final String gapMultiplier;
  32. private final String baseEffort;
  33. public DefaultDebtRemediationFunction(@Nullable Type type, @Nullable String gapMultiplier, @Nullable String baseEffort) {
  34. this.type = type;
  35. this.gapMultiplier = sanitizeValue("gap multiplier", gapMultiplier);
  36. this.baseEffort = sanitizeValue("base effort", baseEffort);
  37. validate();
  38. }
  39. @CheckForNull
  40. private static String sanitizeValue(String label, @Nullable String s) {
  41. if (StringUtils.isNotBlank(s)) {
  42. try {
  43. Duration duration = Duration.decode(s, HOURS_IN_DAY);
  44. return duration.encode(HOURS_IN_DAY);
  45. } catch (Exception e) {
  46. throw new IllegalArgumentException(String.format("Invalid %s: %s (%s)", label, s, e.getMessage()), e);
  47. }
  48. }
  49. return null;
  50. }
  51. @Override
  52. public Type type() {
  53. return type;
  54. }
  55. @Override
  56. @CheckForNull
  57. public String gapMultiplier() {
  58. return gapMultiplier;
  59. }
  60. @Override
  61. public String baseEffort() {
  62. return baseEffort;
  63. }
  64. private void validate() {
  65. checkArgument(type != null, "Remediation function type cannot be null");
  66. switch (type) {
  67. case LINEAR:
  68. checkArgument(this.gapMultiplier != null && this.baseEffort == null, "Linear functions must only have a non empty gap multiplier");
  69. break;
  70. case LINEAR_OFFSET:
  71. checkArgument(this.gapMultiplier != null && this.baseEffort != null, "Linear with offset functions must have both non null gap multiplier and base effort");
  72. break;
  73. case CONSTANT_ISSUE:
  74. checkArgument(this.gapMultiplier == null && this.baseEffort != null, "Constant/issue functions must only have a non empty base effort");
  75. break;
  76. default:
  77. throw new IllegalArgumentException(String.format("Unknown type on %s", this));
  78. }
  79. }
  80. @Override
  81. public boolean equals(Object o) {
  82. if (!(o instanceof DefaultDebtRemediationFunction)) {
  83. return false;
  84. }
  85. if (this == o) {
  86. return true;
  87. }
  88. DefaultDebtRemediationFunction other = (DefaultDebtRemediationFunction) o;
  89. return new EqualsBuilder()
  90. .append(gapMultiplier, other.gapMultiplier())
  91. .append(baseEffort, other.baseEffort())
  92. .append(type, other.type())
  93. .isEquals();
  94. }
  95. @Override
  96. public int hashCode() {
  97. int result = type.hashCode();
  98. result = 31 * result + (gapMultiplier != null ? gapMultiplier.hashCode() : 0);
  99. result = 31 * result + (baseEffort != null ? baseEffort.hashCode() : 0);
  100. return result;
  101. }
  102. @Override
  103. public String toString() {
  104. return "DebtRemediationFunction{" +
  105. "type=" + type + ", " +
  106. "gap multiplier=" + gapMultiplier + ", " +
  107. "base effort=" + baseEffort
  108. + "}";
  109. }
  110. }