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