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.technicaldebt.batch.internal;
022
023import org.apache.commons.lang.builder.ToStringBuilder;
024import org.apache.commons.lang.builder.ToStringStyle;
025import org.picocontainer.annotations.Nullable;
026import org.sonar.api.rule.RuleKey;
027import org.sonar.api.technicaldebt.batch.Requirement;
028import org.sonar.api.utils.WorkUnit;
029import org.sonar.api.utils.internal.WorkDuration;
030
031import javax.annotation.CheckForNull;
032
033import java.util.Date;
034
035/**
036 * @deprecated since 4.3
037 */
038@Deprecated
039public class DefaultRequirement implements Requirement {
040
041  public static final String FUNCTION_LINEAR = "linear";
042  public static final String FUNCTION_LINEAR_WITH_OFFSET = "linear_offset";
043  public static final String CONSTANT_ISSUE = "constant_issue";
044
045  private Integer id;
046  private RuleKey ruleKey;
047  private DefaultCharacteristic characteristic;
048  private DefaultCharacteristic rootCharacteristic;
049
050  private String function;
051  private int factorValue;
052  private WorkDuration.UNIT factorUnit;
053  private int offsetValue;
054  private WorkDuration.UNIT offsetUnit;
055
056  private Date createdAt;
057  private Date updatedAt;
058
059  public Integer id() {
060    return id;
061  }
062
063  public DefaultRequirement setId(Integer id) {
064    this.id = id;
065    return this;
066  }
067
068  public RuleKey ruleKey() {
069    return ruleKey;
070  }
071
072  public DefaultRequirement setRuleKey(RuleKey ruleKey) {
073    this.ruleKey = ruleKey;
074    return this;
075  }
076
077  public DefaultCharacteristic characteristic() {
078    return characteristic;
079  }
080
081  public DefaultRequirement setCharacteristic(DefaultCharacteristic characteristic) {
082    this.characteristic = characteristic;
083    this.characteristic.addRequirement(this);
084    return this;
085  }
086
087  public DefaultCharacteristic rootCharacteristic() {
088    return rootCharacteristic;
089  }
090
091  public DefaultRequirement setRootCharacteristic(DefaultCharacteristic rootCharacteristic) {
092    this.rootCharacteristic = rootCharacteristic;
093    return this;
094  }
095
096  public String function() {
097    return function;
098  }
099
100  public DefaultRequirement setFunction(String function) {
101    this.function = function;
102    return this;
103  }
104
105  /**
106   * @deprecated since 4.2
107   */
108  @Deprecated
109  public WorkUnit factor() {
110    return WorkUnit.create((double) factorValue, fromUnit(factorUnit));
111  }
112
113  /**
114   * @deprecated since 4.2
115   */
116  @Deprecated
117  public DefaultRequirement setFactor(WorkUnit factor) {
118    this.factorValue = (int) factor.getValue();
119    this.factorUnit = toUnit(factor.getUnit());
120    return this;
121  }
122
123  public int factorValue() {
124    return factorValue;
125  }
126
127  public DefaultRequirement setFactorValue(int factorValue) {
128    this.factorValue = factorValue;
129    return this;
130  }
131
132  @CheckForNull
133  public WorkDuration.UNIT factorUnit() {
134    return factorUnit;
135  }
136
137  public DefaultRequirement setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) {
138    this.factorUnit = factorUnit;
139    return this;
140  }
141
142  /**
143   * @deprecated since 4.2
144   */
145  @Deprecated
146  public WorkUnit offset() {
147    return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit));
148  }
149
150  /**
151   * @deprecated since 4.2
152   */
153  @Deprecated
154  public DefaultRequirement setOffset(WorkUnit offset) {
155    this.offsetValue = (int) offset.getValue();
156    this.offsetUnit = toUnit(offset.getUnit());
157    return this;
158  }
159
160  public int offsetValue() {
161    return offsetValue;
162  }
163
164  public DefaultRequirement setOffsetValue(int offsetValue) {
165    this.offsetValue = offsetValue;
166    return this;
167  }
168
169  @CheckForNull
170  public WorkDuration.UNIT offsetUnit() {
171    return offsetUnit;
172  }
173
174  public DefaultRequirement setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) {
175    this.offsetUnit = offsetUnit;
176    return this;
177  }
178
179  public Date createdAt() {
180    return createdAt;
181  }
182
183  public DefaultRequirement setCreatedAt(Date createdAt) {
184    this.createdAt = createdAt;
185    return this;
186  }
187
188  public Date updatedAt() {
189    return updatedAt;
190  }
191
192  public DefaultRequirement setUpdatedAt(Date updatedAt) {
193    this.updatedAt = updatedAt;
194    return this;
195  }
196
197  public static WorkDuration.UNIT toUnit(String requirementUnit){
198    if (WorkUnit.DAYS.equals(requirementUnit)) {
199      return WorkDuration.UNIT.DAYS;
200    } else if (WorkUnit.HOURS.equals(requirementUnit)) {
201      return WorkDuration.UNIT.HOURS;
202    } else if (WorkUnit.MINUTES.equals(requirementUnit)) {
203      return WorkDuration.UNIT.MINUTES;
204    }
205    throw new IllegalStateException("Invalid unit : " + requirementUnit);
206  }
207
208  private static String fromUnit(WorkDuration.UNIT unit){
209    if (WorkDuration.UNIT.DAYS.equals(unit)) {
210      return WorkUnit.DAYS;
211    } else if (WorkDuration.UNIT.HOURS.equals(unit)) {
212      return WorkUnit.HOURS;
213    } else if (WorkDuration.UNIT.MINUTES.equals(unit)) {
214      return WorkUnit.MINUTES;
215    }
216    throw new IllegalStateException("Invalid unit : " + unit);
217  }
218
219  @Override
220  public String toString() {
221    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
222  }
223
224  @Override
225  public boolean equals(Object o) {
226    if (this == o) {
227      return true;
228    }
229    if (o == null || getClass() != o.getClass()) {
230      return false;
231    }
232
233    DefaultRequirement that = (DefaultRequirement) o;
234
235    if (!characteristic.equals(that.characteristic)) {
236      return false;
237    }
238    if (!ruleKey.equals(that.ruleKey)) {
239      return false;
240    }
241
242    return true;
243  }
244
245  @Override
246  public int hashCode() {
247    int result = ruleKey.hashCode();
248    result = 31 * result + characteristic.hashCode();
249    return result;
250  }
251}