001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 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     */
036    public class DefaultCharacteristic implements Characteristic {
037    
038      private Integer id;
039      private String key;
040      private String name;
041      private Integer order;
042      private Integer parentId;
043      private Integer rootId;
044      private RuleKey ruleKey;
045      private String function;
046      private Integer factorValue;
047      private WorkDuration.UNIT factorUnit;
048      private Integer offsetValue;
049      private WorkDuration.UNIT offsetUnit;
050    
051      public Integer id() {
052        return id;
053      }
054    
055      public DefaultCharacteristic setId(Integer id) {
056        this.id = id;
057        return this;
058      }
059    
060      @CheckForNull
061      public String key() {
062        return key;
063      }
064    
065      public DefaultCharacteristic setKey(@Nullable String key) {
066        this.key = key;
067        return this;
068      }
069    
070      @CheckForNull
071      public String name() {
072        return name;
073      }
074    
075      public DefaultCharacteristic setName(@Nullable String name) {
076        this.name = name;
077        return this;
078      }
079    
080      @CheckForNull
081      public Integer order() {
082        return order;
083      }
084    
085      public DefaultCharacteristic setOrder(@Nullable Integer order) {
086        this.order = order;
087        return this;
088      }
089    
090      @CheckForNull
091      public Integer parentId() {
092        return parentId;
093      }
094    
095      public DefaultCharacteristic setParentId(@Nullable Integer parentId) {
096        this.parentId = parentId;
097        return this;
098      }
099    
100      @CheckForNull
101      public Integer rootId() {
102        return rootId;
103      }
104    
105      public DefaultCharacteristic setRootId(@Nullable Integer rootId) {
106        this.rootId = rootId;
107        return this;
108      }
109    
110      @CheckForNull
111      public RuleKey ruleKey() {
112        return ruleKey;
113      }
114    
115      public DefaultCharacteristic setRuleKey(@Nullable RuleKey ruleKey) {
116        this.ruleKey = ruleKey;
117        return this;
118      }
119    
120      @CheckForNull
121      public String function() {
122        return function;
123      }
124    
125      public DefaultCharacteristic setFunction(@Nullable String function) {
126        this.function = function;
127        return this;
128      }
129    
130      /**
131       * @deprecated since 4.2
132       */
133      @Deprecated
134      @CheckForNull
135      public WorkUnit factor() {
136        if (factorValue != null && factorUnit != null) {
137          return WorkUnit.create((double) factorValue, fromUnit(factorUnit));
138        }
139        return null;
140      }
141    
142      /**
143       * @deprecated since 4.2
144       */
145      @Deprecated
146      public DefaultCharacteristic setFactor(@Nullable WorkUnit factor) {
147        if (factor != null) {
148          this.factorValue = (int) factor.getValue();
149          this.factorUnit = toUnit(factor.getUnit());
150        }
151        return this;
152      }
153    
154      @CheckForNull
155      public Integer factorValue() {
156        return factorValue;
157      }
158    
159      public DefaultCharacteristic setFactorValue(@Nullable Integer factorValue) {
160        this.factorValue = factorValue;
161        return this;
162      }
163    
164      @CheckForNull
165      public WorkDuration.UNIT factorUnit() {
166        return factorUnit;
167      }
168    
169      public DefaultCharacteristic setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) {
170        this.factorUnit = factorUnit;
171        return this;
172      }
173    
174      /**
175       * @deprecated since 4.2
176       */
177      @Deprecated
178      public WorkUnit offset() {
179        if (offsetValue != null && offsetUnit != null) {
180          return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit));
181        }
182        return null;
183      }
184    
185      /**
186       * @deprecated since 4.2
187       */
188      @Deprecated
189      public DefaultCharacteristic setOffset(@Nullable WorkUnit offset) {
190        if (offset != null) {
191          this.offsetValue = (int) offset.getValue();
192          this.offsetUnit = toUnit(offset.getUnit());
193        }
194        return this;
195      }
196    
197      @CheckForNull
198      public Integer offsetValue() {
199        return offsetValue;
200      }
201    
202      public DefaultCharacteristic setOffsetValue(@Nullable Integer offsetValue) {
203        this.offsetValue = offsetValue;
204        return this;
205      }
206    
207      @CheckForNull
208      public WorkDuration.UNIT offsetUnit() {
209        return offsetUnit;
210      }
211    
212      public DefaultCharacteristic setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) {
213        this.offsetUnit = offsetUnit;
214        return this;
215      }
216    
217      public static WorkDuration.UNIT toUnit(@Nullable String requirementUnit) {
218        if (requirementUnit != null) {
219          if (WorkUnit.DAYS.equals(requirementUnit)) {
220            return WorkDuration.UNIT.DAYS;
221          } else if (WorkUnit.HOURS.equals(requirementUnit)) {
222            return WorkDuration.UNIT.HOURS;
223          } else if (WorkUnit.MINUTES.equals(requirementUnit)) {
224            return WorkDuration.UNIT.MINUTES;
225          }
226          throw new IllegalStateException("Invalid unit : " + requirementUnit);
227        }
228        return null;
229      }
230    
231      private static String fromUnit(WorkDuration.UNIT unit) {
232        if (WorkDuration.UNIT.DAYS.equals(unit)) {
233          return WorkUnit.DAYS;
234        } else if (WorkDuration.UNIT.HOURS.equals(unit)) {
235          return WorkUnit.HOURS;
236        } else if (WorkDuration.UNIT.MINUTES.equals(unit)) {
237          return WorkUnit.MINUTES;
238        }
239        throw new IllegalStateException("Invalid unit : " + unit);
240      }
241    
242      public boolean isRoot() {
243        return parentId == null;
244      }
245    
246      public boolean isRequirement() {
247        return ruleKey != null;
248      }
249    
250      @Override
251      public boolean equals(Object o) {
252        if (this == o) {
253          return true;
254        }
255        if (o == null || getClass() != o.getClass()) {
256          return false;
257        }
258    
259        DefaultCharacteristic that = (DefaultCharacteristic) o;
260    
261        if (key != null ? !key.equals(that.key) : that.key != null) {
262          return false;
263        }
264        if (ruleKey != null ? !ruleKey.equals(that.ruleKey) : that.ruleKey != null) {
265          return false;
266        }
267    
268        return true;
269      }
270    
271      @Override
272      public int hashCode() {
273        int result = key != null ? key.hashCode() : 0;
274        result = 31 * result + (ruleKey != null ? ruleKey.hashCode() : 0);
275        return result;
276      }
277    
278      @Override
279      public String toString() {
280        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
281      }
282    
283    }