001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020
021package org.sonar.api.server.debt.internal;
022
023import com.google.common.base.Objects;
024import org.apache.commons.lang.StringUtils;
025import org.apache.commons.lang.builder.EqualsBuilder;
026import org.sonar.api.server.debt.DebtRemediationFunction;
027import org.sonar.api.utils.Duration;
028
029import javax.annotation.CheckForNull;
030import javax.annotation.Nullable;
031
032public class DefaultDebtRemediationFunction implements DebtRemediationFunction {
033
034  private static final int HOURS_IN_DAY = 24;
035
036  private final Type type;
037  private final String coefficient;
038  private final String offset;
039
040  public DefaultDebtRemediationFunction(@Nullable Type type, @Nullable String coefficient, @Nullable String offset) {
041    this.type = type;
042    this.coefficient = sanitizeValue("coefficient", coefficient);
043    this.offset = sanitizeValue("offset", offset);
044    validate();
045  }
046
047  @CheckForNull
048  private String sanitizeValue(String label, @Nullable String s) {
049    if (StringUtils.isNotBlank(s)) {
050      try {
051        Duration duration = Duration.decode(s, HOURS_IN_DAY);
052        return duration.encode(HOURS_IN_DAY);
053      } catch (Exception e) {
054        throw new IllegalArgumentException(String.format("Invalid %s: %s (%s)", label, s, e.getMessage()), e);
055      }
056    }
057    return null;
058  }
059
060  @Override
061  public Type type() {
062    return type;
063  }
064
065  @Override
066  @CheckForNull
067  public String coefficient() {
068    return coefficient;
069  }
070
071  @Override
072  @CheckForNull
073  public String offset() {
074    return offset;
075  }
076
077  private void validate() {
078    if (type == null) {
079      throw new IllegalArgumentException("Remediation function type cannot be null");
080    }
081    switch (type) {
082      case LINEAR:
083        if (this.coefficient == null || this.offset != null) {
084          throw new IllegalArgumentException("Linear functions must only have a non empty coefficient");
085        }
086        break;
087      case LINEAR_OFFSET:
088        if (this.coefficient == null || this.offset == null) {
089          throw new IllegalArgumentException("Linear with offset functions must have both non null coefficient and offset");
090        }
091        break;
092      case CONSTANT_ISSUE:
093        if (this.coefficient != null || this.offset == null) {
094          throw new IllegalArgumentException("Constant/issue functions must only have a non empty offset");
095        }
096        break;
097      default:
098        throw new IllegalArgumentException(String.format("Unknown type on %s", this));
099    }
100  }
101
102  @Override
103  public boolean equals(Object o) {
104    if (!(o instanceof DefaultDebtRemediationFunction)) {
105      return false;
106    }
107    if (this == o) {
108      return true;
109    }
110    DefaultDebtRemediationFunction other = (DefaultDebtRemediationFunction) o;
111    return new EqualsBuilder()
112      .append(coefficient, other.coefficient())
113      .append(offset, other.offset())
114      .append(type, other.type())
115      .isEquals();
116  }
117
118  @Override
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);
123    return result;
124  }
125
126  @Override
127  public String toString() {
128    return Objects.toStringHelper(DebtRemediationFunction.class)
129      .add("type", type)
130      .add("coefficient", coefficient)
131      .add("offset", offset)
132      .toString();
133  }
134}