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.server.internal;
021
022import org.apache.commons.lang.builder.ToStringBuilder;
023import org.apache.commons.lang.builder.ToStringStyle;
024import org.sonar.api.rule.RuleKey;
025import org.sonar.api.technicaldebt.server.Characteristic;
026import org.sonar.api.utils.WorkUnit;
027import org.sonar.api.utils.internal.WorkDuration;
028
029import javax.annotation.CheckForNull;
030import javax.annotation.Nullable;
031
032/**
033 * @since 4.1
034 * @deprecated since 4.3.
035 */
036@Deprecated
037public class DefaultCharacteristic implements Characteristic {
038
039  private Integer id;
040  private String key;
041  private String name;
042  private Integer order;
043  private Integer parentId;
044  private Integer rootId;
045  private RuleKey ruleKey;
046  private String function;
047  private Integer factorValue;
048  private WorkDuration.UNIT factorUnit;
049  private Integer offsetValue;
050  private WorkDuration.UNIT offsetUnit;
051
052  @Override
053  public Integer id() {
054    return id;
055  }
056
057  public DefaultCharacteristic setId(Integer id) {
058    this.id = id;
059    return this;
060  }
061
062  @Override
063  @CheckForNull
064  public String key() {
065    return key;
066  }
067
068  public DefaultCharacteristic setKey(@Nullable String key) {
069    this.key = key;
070    return this;
071  }
072
073  @Override
074  @CheckForNull
075  public String name() {
076    return name;
077  }
078
079  public DefaultCharacteristic setName(@Nullable String name) {
080    this.name = name;
081    return this;
082  }
083
084  @Override
085  @CheckForNull
086  public Integer order() {
087    return order;
088  }
089
090  public DefaultCharacteristic setOrder(@Nullable Integer order) {
091    this.order = order;
092    return this;
093  }
094
095  @Override
096  @CheckForNull
097  public Integer parentId() {
098    return parentId;
099  }
100
101  public DefaultCharacteristic setParentId(@Nullable Integer parentId) {
102    this.parentId = parentId;
103    return this;
104  }
105
106  @Override
107  @CheckForNull
108  public Integer rootId() {
109    return rootId;
110  }
111
112  public DefaultCharacteristic setRootId(@Nullable Integer rootId) {
113    this.rootId = rootId;
114    return this;
115  }
116
117  /**
118   * @deprecated since 4.2
119   */
120  @Override
121  @Deprecated
122  @CheckForNull
123  public RuleKey ruleKey() {
124    return ruleKey;
125  }
126
127  /**
128   * @deprecated since 4.2
129   */
130  @Deprecated
131  public DefaultCharacteristic setRuleKey(@Nullable RuleKey ruleKey) {
132    this.ruleKey = ruleKey;
133    return this;
134  }
135
136  /**
137   * @deprecated since 4.2
138   */
139  @Override
140  @Deprecated
141  @CheckForNull
142  public String function() {
143    return function;
144  }
145
146  /**
147   * @deprecated since 4.2
148   */
149  @Deprecated
150  public DefaultCharacteristic setFunction(@Nullable String function) {
151    this.function = function;
152    return this;
153  }
154
155  /**
156   * @deprecated since 4.2
157   */
158  @Override
159  @Deprecated
160  @CheckForNull
161  public WorkUnit factor() {
162    if (factorValue != null && factorUnit != null) {
163      return WorkUnit.create((double) factorValue, fromUnit(factorUnit));
164    }
165    return null;
166  }
167
168  /**
169   * @deprecated since 4.2
170   */
171  @Deprecated
172  public DefaultCharacteristic setFactor(@Nullable WorkUnit factor) {
173    if (factor != null) {
174      this.factorValue = (int) factor.getValue();
175      this.factorUnit = toUnit(factor.getUnit());
176    }
177    return this;
178  }
179
180  /**
181   * @deprecated since 4.3
182   */
183  @Override
184  @Deprecated
185  @CheckForNull
186  public Integer factorValue() {
187    return factorValue;
188  }
189
190  /**
191   * @deprecated since 4.3
192   */
193  @Deprecated
194  public DefaultCharacteristic setFactorValue(@Nullable Integer factorValue) {
195    this.factorValue = factorValue;
196    return this;
197  }
198
199  @Override
200  @CheckForNull
201  public WorkDuration.UNIT factorUnit() {
202    return factorUnit;
203  }
204
205  /**
206   * @deprecated since 4.3
207   */
208  @Deprecated
209  public DefaultCharacteristic setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) {
210    this.factorUnit = factorUnit;
211    return this;
212  }
213
214  /**
215   * @deprecated since 4.2
216   */
217  @Override
218  @Deprecated
219  public WorkUnit offset() {
220    if (offsetValue != null && offsetUnit != null) {
221      return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit));
222    }
223    return null;
224  }
225
226  /**
227   * @deprecated since 4.2
228   */
229  @Deprecated
230  public DefaultCharacteristic setOffset(@Nullable WorkUnit offset) {
231    if (offset != null) {
232      this.offsetValue = (int) offset.getValue();
233      this.offsetUnit = toUnit(offset.getUnit());
234    }
235    return this;
236  }
237
238  /**
239   * @deprecated since 4.3
240   */
241  @Override
242  @Deprecated
243  @CheckForNull
244  public Integer offsetValue() {
245    return offsetValue;
246  }
247
248  /**
249   * @deprecated since 4.3
250   */
251  @Deprecated
252  public DefaultCharacteristic setOffsetValue(@Nullable Integer offsetValue) {
253    this.offsetValue = offsetValue;
254    return this;
255  }
256
257  /**
258   * @deprecated since 4.3
259   */
260  @Override
261  @Deprecated
262  @CheckForNull
263  public WorkDuration.UNIT offsetUnit() {
264    return offsetUnit;
265  }
266
267  /**
268   * @deprecated since 4.3
269   */
270  @Deprecated
271  public DefaultCharacteristic setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) {
272    this.offsetUnit = offsetUnit;
273    return this;
274  }
275
276  /**
277   * @deprecated since 4.3
278   */
279  @Deprecated
280  public static WorkDuration.UNIT toUnit(@Nullable String requirementUnit) {
281    if (requirementUnit != null) {
282      if (WorkUnit.DAYS.equals(requirementUnit)) {
283        return WorkDuration.UNIT.DAYS;
284      } else if (WorkUnit.HOURS.equals(requirementUnit)) {
285        return WorkDuration.UNIT.HOURS;
286      } else if (WorkUnit.MINUTES.equals(requirementUnit)) {
287        return WorkDuration.UNIT.MINUTES;
288      }
289      throw new IllegalStateException("Invalid unit : " + requirementUnit);
290    }
291    return null;
292  }
293
294  private static String fromUnit(WorkDuration.UNIT unit) {
295    if (WorkDuration.UNIT.DAYS.equals(unit)) {
296      return WorkUnit.DAYS;
297    } else if (WorkDuration.UNIT.HOURS.equals(unit)) {
298      return WorkUnit.HOURS;
299    } else if (WorkDuration.UNIT.MINUTES.equals(unit)) {
300      return WorkUnit.MINUTES;
301    }
302    throw new IllegalStateException("Invalid unit : " + unit);
303  }
304
305  @Override
306  public boolean isRoot() {
307    return parentId == null;
308  }
309
310  /**
311   * @deprecated since 4.3
312   */
313  @Override
314  @Deprecated
315  public boolean isRequirement() {
316    return ruleKey != null;
317  }
318
319  @Override
320  public boolean equals(Object o) {
321    if (this == o) {
322      return true;
323    }
324    if (o == null || getClass() != o.getClass()) {
325      return false;
326    }
327
328    DefaultCharacteristic that = (DefaultCharacteristic) o;
329
330    if ((key != null) ? !key.equals(that.key) : (that.key != null)) {
331      return false;
332    }
333    return !((ruleKey != null) ? !ruleKey.equals(that.ruleKey) : (that.ruleKey != null));
334
335  }
336
337  @Override
338  public int hashCode() {
339    int result = (key != null) ? key.hashCode() : 0;
340    result = 31 * result + ((ruleKey != null) ? ruleKey.hashCode() : 0);
341    return result;
342  }
343
344  @Override
345  public String toString() {
346    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
347  }
348
349}