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.WorkDuration;
28 import org.sonar.api.utils.WorkUnit;
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 public Integer factorValue() {
159 public DefaultCharacteristic setFactorValue(@Nullable Integer factorValue) {
160 this.factorValue = factorValue;
165 public WorkDuration.UNIT factorUnit() {
169 public DefaultCharacteristic setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) {
170 this.factorUnit = factorUnit;
175 * @deprecated since 4.2
178 public WorkUnit offset() {
179 if (offsetValue!= null && offsetUnit!= null) {
180 return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit));
186 * @deprecated since 4.2
189 public DefaultCharacteristic setOffset(@Nullable WorkUnit offset) {
190 if (offset != null) {
191 this.offsetValue = (int) offset.getValue();
192 this.offsetUnit = toUnit(offset.getUnit());
198 public Integer offsetValue() {
202 public DefaultCharacteristic setOffsetValue(@Nullable Integer offsetValue) {
203 this.offsetValue = offsetValue;
208 public WorkDuration.UNIT offsetUnit() {
212 public DefaultCharacteristic setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) {
213 this.offsetUnit = offsetUnit;
217 public static WorkDuration.UNIT toUnit(@Nullable String requirementUnit) {
218 if (requirementUnit != null) {
219 if (requirementUnit.equals(WorkUnit.DAYS)) {
220 return WorkDuration.UNIT.DAYS;
221 } else if (requirementUnit.equals(WorkUnit.HOURS)) {
222 return WorkDuration.UNIT.HOURS;
223 } else if (requirementUnit.equals(WorkUnit.MINUTES)) {
224 return WorkDuration.UNIT.MINUTES;
226 throw new IllegalStateException("Invalid unit : " + requirementUnit);
231 private static String fromUnit(WorkDuration.UNIT unit){
232 if (unit.equals(WorkDuration.UNIT.DAYS)) {
233 return WorkUnit.DAYS;
234 } else if (unit.equals(WorkDuration.UNIT.HOURS)) {
235 return WorkUnit.HOURS;
236 } else if (unit.equals(WorkDuration.UNIT.MINUTES)) {
237 return WorkUnit.MINUTES;
239 throw new IllegalStateException("Invalid unit : " + unit);
242 public boolean isRoot() {
243 return parentId == null;
246 public boolean isRequirement() {
247 return ruleKey != null;
251 public boolean equals(Object o) {
255 if (o == null || getClass() != o.getClass()) {
259 DefaultCharacteristic that = (DefaultCharacteristic) o;
261 if (key != null ? !key.equals(that.key) : that.key != null) {
264 if (ruleKey != null ? !ruleKey.equals(that.ruleKey) : that.ruleKey != null) {
272 public int hashCode() {
273 int result = key != null ? key.hashCode() : 0;
274 result = 31 * result + (ruleKey != null ? ruleKey.hashCode() : 0);
279 public String toString() {
280 return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);