2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
21 package org.sonar.api.server.debt.internal;
23 import com.google.common.base.Objects;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.builder.EqualsBuilder;
26 import org.sonar.api.server.debt.DebtRemediationFunction;
27 import org.sonar.api.utils.Duration;
29 import javax.annotation.CheckForNull;
30 import javax.annotation.Nullable;
32 public class DefaultDebtRemediationFunction implements DebtRemediationFunction {
34 private static final int HOURS_IN_DAY = 24;
36 private final Type type;
37 private final String coefficient;
38 private final String offset;
40 public DefaultDebtRemediationFunction(@Nullable Type type, @Nullable String coefficient, @Nullable String offset) {
42 throw new IllegalArgumentException("Remediation function type cannot be null");
45 this.coefficient = sanitizeValue("coefficient", coefficient);
46 this.offset = sanitizeValue("offset", offset);
51 private String sanitizeValue(String label, @Nullable String s) {
52 if (StringUtils.isNotBlank(s)) {
54 Duration duration = Duration.decode(s, HOURS_IN_DAY);
55 return duration.encode(HOURS_IN_DAY);
56 } catch (Exception e) {
57 throw new IllegalArgumentException(String.format("Invalid %s: %s", label, s), e);
70 public String coefficient() {
76 public String offset() {
80 private void validate() {
83 if (this.coefficient == null || this.offset != null) {
84 throw new IllegalArgumentException(String.format("Only coefficient must be set on %s", this));
88 if (this.coefficient == null || this.offset == null) {
89 throw new IllegalArgumentException(String.format("Both coefficient and offset are required on %s", this));
93 if (this.coefficient != null || this.offset == null) {
94 throw new IllegalArgumentException(String.format("Only offset must be set on %s", this));
98 throw new IllegalArgumentException(String.format("Unknown type on %s", this));
103 public boolean equals(Object o) {
104 if (!(o instanceof DefaultDebtRemediationFunction)) {
110 DefaultDebtRemediationFunction other = (DefaultDebtRemediationFunction) o;
111 return new EqualsBuilder()
112 .append(coefficient, other.coefficient())
113 .append(offset, other.offset())
114 .append(type, other.type())
119 public int hashCode() {
120 int result = type.hashCode();
121 result = 31 * result + (coefficient != null ? coefficient.hashCode() : 0);
122 result = 31 * result + (offset != null ? offset.hashCode() : 0);
127 public String toString() {
128 return Objects.toStringHelper(DebtRemediationFunction.class)
130 .add("coefficient", coefficient)
131 .add("offset", offset)