2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 package org.sonar.api.technicaldebt.server.internal;
23 import org.apache.commons.lang.builder.ToStringBuilder;
24 import org.apache.commons.lang.builder.ToStringStyle;
25 import org.sonar.api.rule.RuleKey;
26 import org.sonar.api.technicaldebt.server.Characteristic;
27 import org.sonar.api.utils.WorkUnit;
28 import org.sonar.api.utils.internal.WorkDuration;
30 import javax.annotation.CheckForNull;
31 import javax.annotation.Nullable;
36 public class DefaultCharacteristic implements Characteristic {
41 private Integer order;
42 private Integer parentId;
43 private Integer rootId;
44 private RuleKey ruleKey;
45 private String function;
46 private Integer factorValue;
47 private WorkDuration.UNIT factorUnit;
48 private Integer offsetValue;
49 private WorkDuration.UNIT offsetUnit;
55 public DefaultCharacteristic setId(Integer id) {
65 public DefaultCharacteristic setKey(@Nullable String key) {
71 public String name() {
75 public DefaultCharacteristic setName(@Nullable String name) {
81 public Integer order() {
85 public DefaultCharacteristic setOrder(@Nullable Integer order) {
91 public Integer parentId() {
95 public DefaultCharacteristic setParentId(@Nullable Integer parentId) {
96 this.parentId = parentId;
101 public Integer rootId() {
105 public DefaultCharacteristic setRootId(@Nullable Integer rootId) {
106 this.rootId = rootId;
111 public RuleKey ruleKey() {
115 public DefaultCharacteristic setRuleKey(@Nullable RuleKey ruleKey) {
116 this.ruleKey = ruleKey;
121 public String function() {
125 public DefaultCharacteristic setFunction(@Nullable String function) {
126 this.function = function;
131 * @deprecated since 4.2
135 public WorkUnit factor() {
136 if (factorValue != null && factorUnit != null) {
137 return WorkUnit.create((double) factorValue, fromUnit(factorUnit));
143 * @deprecated since 4.2
146 public DefaultCharacteristic setFactor(@Nullable WorkUnit factor) {
147 if (factor != null) {
148 this.factorValue = (int) factor.getValue();
149 this.factorUnit = toUnit(factor.getUnit());
155 * @deprecated since 4.3
159 public Integer factorValue() {
164 * @deprecated since 4.3
167 public DefaultCharacteristic setFactorValue(@Nullable Integer factorValue) {
168 this.factorValue = factorValue;
173 public WorkDuration.UNIT factorUnit() {
178 * @deprecated since 4.3
181 public DefaultCharacteristic setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) {
182 this.factorUnit = factorUnit;
187 * @deprecated since 4.2
190 public WorkUnit offset() {
191 if (offsetValue != null && offsetUnit != null) {
192 return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit));
198 * @deprecated since 4.2
201 public DefaultCharacteristic setOffset(@Nullable WorkUnit offset) {
202 if (offset != null) {
203 this.offsetValue = (int) offset.getValue();
204 this.offsetUnit = toUnit(offset.getUnit());
210 * @deprecated since 4.3
214 public Integer offsetValue() {
219 * @deprecated since 4.3
222 public DefaultCharacteristic setOffsetValue(@Nullable Integer offsetValue) {
223 this.offsetValue = offsetValue;
228 * @deprecated since 4.3
232 public WorkDuration.UNIT offsetUnit() {
237 * @deprecated since 4.3
240 public DefaultCharacteristic setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) {
241 this.offsetUnit = offsetUnit;
246 * @deprecated since 4.3
249 public static WorkDuration.UNIT toUnit(@Nullable String requirementUnit) {
250 if (requirementUnit != null) {
251 if (WorkUnit.DAYS.equals(requirementUnit)) {
252 return WorkDuration.UNIT.DAYS;
253 } else if (WorkUnit.HOURS.equals(requirementUnit)) {
254 return WorkDuration.UNIT.HOURS;
255 } else if (WorkUnit.MINUTES.equals(requirementUnit)) {
256 return WorkDuration.UNIT.MINUTES;
258 throw new IllegalStateException("Invalid unit : " + requirementUnit);
263 private static String fromUnit(WorkDuration.UNIT unit) {
264 if (WorkDuration.UNIT.DAYS.equals(unit)) {
265 return WorkUnit.DAYS;
266 } else if (WorkDuration.UNIT.HOURS.equals(unit)) {
267 return WorkUnit.HOURS;
268 } else if (WorkDuration.UNIT.MINUTES.equals(unit)) {
269 return WorkUnit.MINUTES;
271 throw new IllegalStateException("Invalid unit : " + unit);
274 public boolean isRoot() {
275 return parentId == null;
279 * @deprecated since 4.3
282 public boolean isRequirement() {
283 return ruleKey != null;
287 public boolean equals(Object o) {
291 if (o == null || getClass() != o.getClass()) {
295 DefaultCharacteristic that = (DefaultCharacteristic) o;
297 if (key != null ? !key.equals(that.key) : that.key != null) {
300 if (ruleKey != null ? !ruleKey.equals(that.ruleKey) : that.ruleKey != null) {
308 public int hashCode() {
309 int result = key != null ? key.hashCode() : 0;
310 result = 31 * result + (ruleKey != null ? ruleKey.hashCode() : 0);
315 public String toString() {
316 return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);