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.batch.debt;
022
023import org.apache.commons.lang.builder.EqualsBuilder;
024import org.apache.commons.lang.builder.HashCodeBuilder;
025import org.apache.commons.lang.builder.ReflectionToStringBuilder;
026import org.apache.commons.lang.builder.ToStringStyle;
027import org.sonar.api.utils.Duration;
028
029import javax.annotation.CheckForNull;
030import javax.annotation.Nullable;
031
032/**
033 * @since 4.3
034 */
035public class DebtRemediationFunction {
036
037  public static enum Type {
038    LINEAR, LINEAR_OFFSET, CONSTANT_ISSUE
039  }
040
041  private Type type;
042  private Duration coefficient;
043  private Duration offset;
044
045  private DebtRemediationFunction(Type type, @Nullable Duration coefficient, @Nullable Duration offset) {
046    this.type = type;
047    this.coefficient = coefficient;
048    this.offset = offset;
049  }
050
051  public static DebtRemediationFunction create(Type type, @Nullable Duration coefficient, @Nullable Duration offset) {
052    return new DebtRemediationFunction(type, coefficient, offset);
053  }
054
055  public static DebtRemediationFunction createLinear(Duration coefficient) {
056    return new DebtRemediationFunction(Type.LINEAR, coefficient, null);
057  }
058
059  public static DebtRemediationFunction createLinearWithOffset(Duration coefficient, Duration offset) {
060    return new DebtRemediationFunction(Type.LINEAR_OFFSET, coefficient, offset);
061  }
062
063  public static DebtRemediationFunction createConstantPerIssue(Duration offset) {
064    return new DebtRemediationFunction(Type.CONSTANT_ISSUE, null, offset);
065  }
066
067  public Type type() {
068    return type;
069  }
070
071  @CheckForNull
072  public Duration coefficient() {
073    return coefficient;
074  }
075
076  @CheckForNull
077  public Duration offset() {
078    return offset;
079  }
080
081  @Override
082  public boolean equals(Object o) {
083    if (this == o) {
084      return true;
085    }
086    if (o == null || getClass() != o.getClass()) {
087      return false;
088    }
089    DebtRemediationFunction that = (DebtRemediationFunction) o;
090    return new EqualsBuilder()
091      .append(type, that.type())
092      .append(coefficient, that.coefficient())
093      .append(offset, that.offset())
094      .isEquals();
095  }
096
097  @Override
098  public int hashCode() {
099    return new HashCodeBuilder(15, 31)
100      .append(type)
101      .append(coefficient)
102      .append(offset)
103      .toHashCode();
104  }
105
106  @Override
107  public String toString() {
108    return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
109  }
110}