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 021package org.sonar.api.technicaldebt.batch.internal; 022 023import org.apache.commons.lang.builder.ToStringBuilder; 024import org.apache.commons.lang.builder.ToStringStyle; 025import org.picocontainer.annotations.Nullable; 026import org.sonar.api.rule.RuleKey; 027import org.sonar.api.technicaldebt.batch.Requirement; 028import org.sonar.api.utils.WorkUnit; 029import org.sonar.api.utils.internal.WorkDuration; 030 031import javax.annotation.CheckForNull; 032 033import java.util.Date; 034 035public class DefaultRequirement implements Requirement { 036 037 public static final String FUNCTION_LINEAR = "linear"; 038 public static final String FUNCTION_LINEAR_WITH_OFFSET = "linear_offset"; 039 public static final String CONSTANT_ISSUE = "constant_issue"; 040 041 private Integer id; 042 private RuleKey ruleKey; 043 private DefaultCharacteristic characteristic; 044 private DefaultCharacteristic rootCharacteristic; 045 046 private String function; 047 private int factorValue; 048 private WorkDuration.UNIT factorUnit; 049 private int offsetValue; 050 private WorkDuration.UNIT offsetUnit; 051 052 private Date createdAt; 053 private Date updatedAt; 054 055 public Integer id() { 056 return id; 057 } 058 059 public DefaultRequirement setId(Integer id) { 060 this.id = id; 061 return this; 062 } 063 064 public RuleKey ruleKey() { 065 return ruleKey; 066 } 067 068 public DefaultRequirement setRuleKey(RuleKey ruleKey) { 069 this.ruleKey = ruleKey; 070 return this; 071 } 072 073 public DefaultCharacteristic characteristic() { 074 return characteristic; 075 } 076 077 public DefaultRequirement setCharacteristic(DefaultCharacteristic characteristic) { 078 this.characteristic = characteristic; 079 this.characteristic.addRequirement(this); 080 return this; 081 } 082 083 public DefaultCharacteristic rootCharacteristic() { 084 return rootCharacteristic; 085 } 086 087 public DefaultRequirement setRootCharacteristic(DefaultCharacteristic rootCharacteristic) { 088 this.rootCharacteristic = rootCharacteristic; 089 return this; 090 } 091 092 public String function() { 093 return function; 094 } 095 096 public DefaultRequirement setFunction(String function) { 097 this.function = function; 098 return this; 099 } 100 101 /** 102 * @deprecated since 4.2 103 */ 104 @Deprecated 105 public WorkUnit factor() { 106 return WorkUnit.create((double) factorValue, fromUnit(factorUnit)); 107 } 108 109 /** 110 * @deprecated since 4.2 111 */ 112 @Deprecated 113 public DefaultRequirement setFactor(WorkUnit factor) { 114 this.factorValue = (int) factor.getValue(); 115 this.factorUnit = toUnit(factor.getUnit()); 116 return this; 117 } 118 119 public int factorValue() { 120 return factorValue; 121 } 122 123 public DefaultRequirement setFactorValue(int factorValue) { 124 this.factorValue = factorValue; 125 return this; 126 } 127 128 @CheckForNull 129 public WorkDuration.UNIT factorUnit() { 130 return factorUnit; 131 } 132 133 public DefaultRequirement setFactorUnit(@Nullable WorkDuration.UNIT factorUnit) { 134 this.factorUnit = factorUnit; 135 return this; 136 } 137 138 /** 139 * @deprecated since 4.2 140 */ 141 @Deprecated 142 public WorkUnit offset() { 143 return WorkUnit.create((double) offsetValue, fromUnit(offsetUnit)); 144 } 145 146 /** 147 * @deprecated since 4.2 148 */ 149 @Deprecated 150 public DefaultRequirement setOffset(WorkUnit offset) { 151 this.offsetValue = (int) offset.getValue(); 152 this.offsetUnit = toUnit(offset.getUnit()); 153 return this; 154 } 155 156 public int offsetValue() { 157 return offsetValue; 158 } 159 160 public DefaultRequirement setOffsetValue(int offsetValue) { 161 this.offsetValue = offsetValue; 162 return this; 163 } 164 165 @CheckForNull 166 public WorkDuration.UNIT offsetUnit() { 167 return offsetUnit; 168 } 169 170 public DefaultRequirement setOffsetUnit(@Nullable WorkDuration.UNIT offsetUnit) { 171 this.offsetUnit = offsetUnit; 172 return this; 173 } 174 175 public Date createdAt() { 176 return createdAt; 177 } 178 179 public DefaultRequirement setCreatedAt(Date createdAt) { 180 this.createdAt = createdAt; 181 return this; 182 } 183 184 public Date updatedAt() { 185 return updatedAt; 186 } 187 188 public DefaultRequirement setUpdatedAt(Date updatedAt) { 189 this.updatedAt = updatedAt; 190 return this; 191 } 192 193 public static WorkDuration.UNIT toUnit(String requirementUnit){ 194 if (WorkUnit.DAYS.equals(requirementUnit)) { 195 return WorkDuration.UNIT.DAYS; 196 } else if (WorkUnit.HOURS.equals(requirementUnit)) { 197 return WorkDuration.UNIT.HOURS; 198 } else if (WorkUnit.MINUTES.equals(requirementUnit)) { 199 return WorkDuration.UNIT.MINUTES; 200 } 201 throw new IllegalStateException("Invalid unit : " + requirementUnit); 202 } 203 204 private static String fromUnit(WorkDuration.UNIT unit){ 205 if (WorkDuration.UNIT.DAYS.equals(unit)) { 206 return WorkUnit.DAYS; 207 } else if (WorkDuration.UNIT.HOURS.equals(unit)) { 208 return WorkUnit.HOURS; 209 } else if (WorkDuration.UNIT.MINUTES.equals(unit)) { 210 return WorkUnit.MINUTES; 211 } 212 throw new IllegalStateException("Invalid unit : " + unit); 213 } 214 215 @Override 216 public String toString() { 217 return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 218 } 219 220 @Override 221 public boolean equals(Object o) { 222 if (this == o) { 223 return true; 224 } 225 if (o == null || getClass() != o.getClass()) { 226 return false; 227 } 228 229 DefaultRequirement that = (DefaultRequirement) o; 230 231 if (!characteristic.equals(that.characteristic)) { 232 return false; 233 } 234 if (!ruleKey.equals(that.ruleKey)) { 235 return false; 236 } 237 238 return true; 239 } 240 241 @Override 242 public int hashCode() { 243 int result = ruleKey.hashCode(); 244 result = 31 * result + characteristic.hashCode(); 245 return result; 246 } 247}