3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.api.server.debt.internal;
22 import com.google.common.base.MoreObjects;
23 import javax.annotation.CheckForNull;
24 import javax.annotation.Nullable;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.lang.builder.EqualsBuilder;
27 import org.sonar.api.server.debt.DebtRemediationFunction;
28 import org.sonar.api.utils.Duration;
30 import static com.google.common.base.Preconditions.checkArgument;
32 public class DefaultDebtRemediationFunction implements DebtRemediationFunction {
34 private static final int HOURS_IN_DAY = 24;
36 private final Type type;
37 private final String gapMultiplier;
38 private final String baseEffort;
40 public DefaultDebtRemediationFunction(@Nullable Type type, @Nullable String gapMultiplier, @Nullable String baseEffort) {
42 this.gapMultiplier = sanitizeValue("gap multiplier", gapMultiplier);
43 this.baseEffort = sanitizeValue("base effort", baseEffort);
48 private static String sanitizeValue(String label, @Nullable String s) {
49 if (StringUtils.isNotBlank(s)) {
51 Duration duration = Duration.decode(s, HOURS_IN_DAY);
52 return duration.encode(HOURS_IN_DAY);
53 } catch (Exception e) {
54 throw new IllegalArgumentException(String.format("Invalid %s: %s (%s)", label, s, e.getMessage()), e);
66 * @deprecated since 5.5, replaced by {@link #gapMultiplier}
71 public String coefficient() {
72 return gapMultiplier();
78 public String gapMultiplier() {
83 * @deprecated since 5.5, replaced by {@link #baseEffort}
88 public String offset() {
93 public String baseEffort() {
98 private void validate() {
99 checkArgument(type != null, "Remediation function type cannot be null");
102 checkArgument(this.gapMultiplier != null && this.baseEffort == null, "Linear functions must only have a non empty gap multiplier");
105 checkArgument(this.gapMultiplier != null && this.baseEffort != null, "Linear with offset functions must have both non null gap multiplier and base effort");
108 checkArgument(this.gapMultiplier == null && this.baseEffort != null, "Constant/issue functions must only have a non empty base effort");
111 throw new IllegalArgumentException(String.format("Unknown type on %s", this));
116 public boolean equals(Object o) {
117 if (!(o instanceof DefaultDebtRemediationFunction)) {
123 DefaultDebtRemediationFunction other = (DefaultDebtRemediationFunction) o;
124 return new EqualsBuilder()
125 .append(gapMultiplier, other.gapMultiplier())
126 .append(baseEffort, other.baseEffort())
127 .append(type, other.type())
132 public int hashCode() {
133 int result = type.hashCode();
134 result = 31 * result + (gapMultiplier != null ? gapMultiplier.hashCode() : 0);
135 result = 31 * result + (baseEffort != null ? baseEffort.hashCode() : 0);
140 public String toString() {
141 return MoreObjects.toStringHelper(DebtRemediationFunction.class)
143 .add("gap multiplier", gapMultiplier)
144 .add("base effort", baseEffort)