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