001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 */
020package org.sonar.api.server.debt.internal;
021
022import com.google.common.base.Objects;
023import javax.annotation.CheckForNull;
024import javax.annotation.Nullable;
025import org.apache.commons.lang.StringUtils;
026import org.apache.commons.lang.builder.EqualsBuilder;
027import org.sonar.api.server.debt.DebtRemediationFunction;
028import org.sonar.api.utils.Duration;
029
030import static com.google.common.base.Preconditions.checkArgument;
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 static 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    checkArgument(type != null, "Remediation function type cannot be null");
079    switch (type) {
080      case LINEAR:
081        checkArgument(this.coefficient != null && this.offset == null, "Linear functions must only have a non empty coefficient");
082        break;
083      case LINEAR_OFFSET:
084        checkArgument(this.coefficient != null && this.offset != null, "Linear with offset functions must have both non null coefficient and offset");
085        break;
086      case CONSTANT_ISSUE:
087        checkArgument(this.coefficient == null && this.offset != null, "Constant/issue functions must only have a non empty offset");
088        break;
089      default:
090        throw new IllegalArgumentException(String.format("Unknown type on %s", this));
091    }
092  }
093
094  @Override
095  public boolean equals(Object o) {
096    if (!(o instanceof DefaultDebtRemediationFunction)) {
097      return false;
098    }
099    if (this == o) {
100      return true;
101    }
102    DefaultDebtRemediationFunction other = (DefaultDebtRemediationFunction) o;
103    return new EqualsBuilder()
104      .append(coefficient, other.coefficient())
105      .append(offset, other.offset())
106      .append(type, other.type())
107      .isEquals();
108  }
109
110  @Override
111  public int hashCode() {
112    int result = type.hashCode();
113    result = 31 * result + (coefficient != null ? coefficient.hashCode() : 0);
114    result = 31 * result + (offset != null ? offset.hashCode() : 0);
115    return result;
116  }
117
118  @Override
119  public String toString() {
120    return Objects.toStringHelper(DebtRemediationFunction.class)
121      .add("type", type)
122      .add("coefficient", coefficient)
123      .add("offset", offset)
124      .toString();
125  }
126}