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(Type type, @Nullable String coefficient, @Nullable String offset) {
42 this.coefficient = sanitizeValue("coefficient", coefficient);
43 this.offset = sanitizeValue("offset", offset);
48 private 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", label, s), e);
67 public String coefficient() {
73 public String offset() {
77 private void validate() {
80 if (this.coefficient == null || this.offset != null) {
81 throw new IllegalArgumentException(String.format("Only coefficient must be set on %s", this));
85 if (this.coefficient == null || this.offset == null) {
86 throw new IllegalArgumentException(String.format("Both coefficient and offset are required on %s", this));
90 if (this.coefficient != null || this.offset == null) {
91 throw new IllegalArgumentException(String.format("Only offset must be set on %s", this));
95 throw new IllegalArgumentException(String.format("Unknown type on %s", this));
100 public boolean equals(Object o) {
101 if (!(o instanceof DefaultDebtRemediationFunction)) {
107 DefaultDebtRemediationFunction other = (DefaultDebtRemediationFunction) o;
108 return new EqualsBuilder()
109 .append(coefficient, other.coefficient())
110 .append(offset, other.offset())
111 .append(type, other.type())
116 public int hashCode() {
117 int result = type.hashCode();
118 result = 31 * result + (coefficient != null ? coefficient.hashCode() : 0);
119 result = 31 * result + (offset != null ? offset.hashCode() : 0);
124 public String toString() {
125 return Objects.toStringHelper(DebtRemediationFunction.class)
127 .add("coefficient", coefficient)
128 .add("offset", offset)