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