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