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.StringUtils; 024import org.apache.commons.lang.builder.ToStringBuilder; 025import org.apache.commons.lang.builder.ToStringStyle; 026import org.sonar.api.technicaldebt.batch.Characteristic; 027 028import javax.annotation.CheckForNull; 029import javax.annotation.Nullable; 030 031import java.util.Date; 032import java.util.List; 033 034import static com.google.common.collect.Lists.newArrayList; 035 036public class DefaultCharacteristic implements Characteristic { 037 038 private Integer id; 039 private String key; 040 private String name; 041 private Integer order; 042 private DefaultCharacteristic parent; 043 private DefaultCharacteristic root; 044 private List<DefaultCharacteristic> children; 045 private List<DefaultRequirement> requirements; 046 047 private Date createdAt; 048 private Date updatedAt; 049 050 public DefaultCharacteristic() { 051 this.children = newArrayList(); 052 this.requirements = newArrayList(); 053 } 054 055 public Integer id() { 056 return id; 057 } 058 059 public DefaultCharacteristic setId(Integer id) { 060 this.id = id; 061 return this; 062 } 063 064 public String key() { 065 return key; 066 } 067 068 public DefaultCharacteristic setKey(String key) { 069 this.key = StringUtils.trimToNull(key); 070 return this; 071 } 072 073 public String name() { 074 return name; 075 } 076 077 public DefaultCharacteristic setName(String name) { 078 this.name = name; 079 return this; 080 } 081 082 // TODO Replace this is a TechnicalDebtFactory class 083 public DefaultCharacteristic setName(String s, boolean asKey) { 084 this.name = StringUtils.trimToNull(s); 085 if (asKey) { 086 this.key = StringUtils.upperCase(this.name); 087 this.key = StringUtils.replaceChars(this.key, ' ', '_'); 088 } 089 return this; 090 } 091 092 @CheckForNull 093 public Integer order() { 094 return order; 095 } 096 097 public DefaultCharacteristic setOrder(@Nullable Integer order) { 098 this.order = order; 099 return this; 100 } 101 102 @CheckForNull 103 public DefaultCharacteristic parent() { 104 return parent; 105 } 106 107 public DefaultCharacteristic setParent(@Nullable DefaultCharacteristic parent) { 108 if (parent != null) { 109 this.parent = parent; 110 parent.addChild(this); 111 } 112 return this; 113 } 114 115 @CheckForNull 116 public DefaultCharacteristic root() { 117 return root; 118 } 119 120 public DefaultCharacteristic setRoot(@Nullable DefaultCharacteristic root) { 121 this.root = root; 122 return this; 123 } 124 125 public List<DefaultCharacteristic> children() { 126 return children; 127 } 128 129 private DefaultCharacteristic addChild(DefaultCharacteristic child) { 130 this.children.add(child); 131 return this; 132 } 133 134 public List<DefaultRequirement> requirements() { 135 return requirements; 136 } 137 138 public DefaultCharacteristic addRequirement(DefaultRequirement requirement) { 139 this.requirements.add(requirement); 140 return this; 141 } 142 143 public boolean isRoot() { 144 return parent == null; 145 } 146 147 public Date createdAt() { 148 return createdAt; 149 } 150 151 public DefaultCharacteristic setCreatedAt(Date createdAt) { 152 this.createdAt = createdAt; 153 return this; 154 } 155 156 public Date updatedAt() { 157 return updatedAt; 158 } 159 160 public DefaultCharacteristic setUpdatedAt(Date updatedAt) { 161 this.updatedAt = updatedAt; 162 return this; 163 } 164 165 @Override 166 public String toString() { 167 return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); 168 } 169 170 @Override 171 public boolean equals(Object o) { 172 if (this == o) { 173 return true; 174 } 175 if (o == null || getClass() != o.getClass()) { 176 return false; 177 } 178 DefaultCharacteristic that = (DefaultCharacteristic) o; 179 return key.equals(that.key); 180 } 181 182 @Override 183 public int hashCode() { 184 return key.hashCode(); 185 } 186}