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  @Override
060  public Integer id() {
061    return id;
062  }
063
064  public DefaultRequirement setId(Integer id) {
065    this.id = id;
066    return this;
067  }
068
069  @Override
070  public RuleKey ruleKey() {
071    return ruleKey;
072  }
073
074  public DefaultRequirement setRuleKey(RuleKey ruleKey) {
075    this.ruleKey = ruleKey;
076    return this;
077  }
078
079  @Override
080  public DefaultCharacteristic characteristic() {
081    return characteristic;
082  }
083
084  public DefaultRequirement setCharacteristic(DefaultCharacteristic characteristic) {
085    this.characteristic = characteristic;
086    this.characteristic.addRequirement(this);
087    return this;
088  }
089
090  @Override
091  public DefaultCharacteristic rootCharacteristic() {
092    return rootCharacteristic;
093  }
094
095  public DefaultRequirement setRootCharacteristic(DefaultCharacteristic rootCharacteristic) {
096    this.rootCharacteristic = rootCharacteristic;
097    return this;
098  }
099
100  @Override
101  public String function() {
102    return function;
103  }
104
105  public DefaultRequirement setFunction(String function) {
106    this.function = function;
107    return this;
108  }
109
110  /**
111   * @deprecated since 4.2
112   */
113  @Override
114  @Deprecated
115  public WorkUnit factor() {
116    return WorkUnit.create((double) factorValue, fromUnit(factorUnit));
117  }
118
119  /**
120   * @deprecated since 4.2
121   */
122  @Deprecated
123  public DefaultRequirement setFactor(WorkUnit factor) {
124    this.factorValue = (int) factor.getValue();
125    this.factorUnit = toUnit(factor.getUnit());
126    return this;
127  }
128
129  @Override
130  public int factorValue() {
131    return factorValue;
132  }
133
134  public DefaultRequirement setFactorValue(int factorValue) {
135    this.factorValue = factorValue;
136    return this;
137  }
138
139  @Override
140  @CheckForNull
141  public WorkDuration.UNIT factorUnit() {
142    return factorUnit;
143  }
144
145  public DefaultRequirement setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) {
146    this.factorUnit = factorUnit;
147    return this;
148  }
149
150  /**
151   * @deprecated since 4.2
152   */
153  @Override
154  @Deprecated
155  public WorkUnit offset() {
156    return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit));
157  }
158
159  /**
160   * @deprecated since 4.2
161   */
162  @Deprecated
163  public DefaultRequirement setOffset(WorkUnit offset) {
164    this.offsetValue = (int) offset.getValue();
165    this.offsetUnit = toUnit(offset.getUnit());
166    return this;
167  }
168
169  @Override
170  public int offsetValue() {
171    return offsetValue;
172  }
173
174  public DefaultRequirement setOffsetValue(int offsetValue) {
175    this.offsetValue = offsetValue;
176    return this;
177  }
178
179  @Override
180  @CheckForNull
181  public WorkDuration.UNIT offsetUnit() {
182    return offsetUnit;
183  }
184
185  public DefaultRequirement setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) {
186    this.offsetUnit = offsetUnit;
187    return this;
188  }
189
190  @Override
191  public Date createdAt() {
192    return createdAt;
193  }
194
195  public DefaultRequirement setCreatedAt(Date createdAt) {
196    this.createdAt = createdAt;
197    return this;
198  }
199
200  @Override
201  public Date updatedAt() {
202    return updatedAt;
203  }
204
205  public DefaultRequirement setUpdatedAt(Date updatedAt) {
206    this.updatedAt = updatedAt;
207    return this;
208  }
209
210  public static WorkDuration.UNIT toUnit(String requirementUnit){
211    if (WorkUnit.DAYS.equals(requirementUnit)) {
212      return WorkDuration.UNIT.DAYS;
213    } else if (WorkUnit.HOURS.equals(requirementUnit)) {
214      return WorkDuration.UNIT.HOURS;
215    } else if (WorkUnit.MINUTES.equals(requirementUnit)) {
216      return WorkDuration.UNIT.MINUTES;
217    }
218    throw new IllegalStateException("Invalid unit : " + requirementUnit);
219  }
220
221  private static String fromUnit(WorkDuration.UNIT unit){
222    if (WorkDuration.UNIT.DAYS.equals(unit)) {
223      return WorkUnit.DAYS;
224    } else if (WorkDuration.UNIT.HOURS.equals(unit)) {
225      return WorkUnit.HOURS;
226    } else if (WorkDuration.UNIT.MINUTES.equals(unit)) {
227      return WorkUnit.MINUTES;
228    }
229    throw new IllegalStateException("Invalid unit : " + unit);
230  }
231
232  @Override
233  public String toString() {
234    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
235  }
236
237  @Override
238  public boolean equals(Object o) {
239    if (this == o) {
240      return true;
241    }
242    if (o == null || getClass() != o.getClass()) {
243      return false;
244    }
245
246    DefaultRequirement that = (DefaultRequirement) o;
247
248    if (!characteristic.equals(that.characteristic)) {
249      return false;
250    }
251    if (!ruleKey.equals(that.ruleKey)) {
252      return false;
253    }
254
255    return true;
256  }
257
258  @Override
259  public int hashCode() {
260    int result = ruleKey.hashCode();
261    result = 31 * result + characteristic.hashCode();
262    return result;
263  }
264}