1 package net.sourceforge.pmd.properties; 2 3 import net.sourceforge.pmd.PropertyDescriptor; 4 import net.sourceforge.pmd.Rule; 5 6 7 12 public abstract class AbstractPMDProperty implements PropertyDescriptor { 13 14 private String name; 15 private String description; 16 private Object defaultValue; 17 private boolean isRequired = false; 18 private int maxValueCount = 1; 19 private float uiOrder; 20 21 protected char multiValueDelimiter = '|'; 22 23 30 protected AbstractPMDProperty(String theName, String theDescription, Object theDefault, float theUIOrder) { 31 name = theName; 32 description = theDescription; 33 defaultValue = theDefault; 34 uiOrder = theUIOrder; 35 } 36 37 41 protected void multiValueDelimiter(char aDelimiter) { 42 multiValueDelimiter = aDelimiter; 43 } 44 45 50 public char multiValueDelimiter() { 51 return multiValueDelimiter; 52 } 53 54 59 public String name() { 60 return name; 61 } 62 63 68 public String description() { 69 return description; 70 } 71 72 77 public Object defaultValue() { 78 return defaultValue; 79 } 80 81 86 public int maxValueCount() { 87 return maxValueCount; 88 } 89 90 95 protected void maxValueCount(int theCount) { 96 maxValueCount = theCount; 97 } 98 99 104 public boolean isRequired() { 105 return isRequired; 106 } 107 108 113 public float uiOrder() { 114 return uiOrder; 115 } 116 117 124 protected String asString(Object value) { 125 return value == null ? "" : value.toString(); 126 } 127 128 129 135 public String asDelimitedString(Object values) { 136 137 if (values == null) return ""; 138 139 if (values instanceof Object []) { 140 Object [] valueSet = (Object [])values; 141 if (valueSet.length == 0) return ""; 142 if (valueSet.length == 1) return asString(valueSet[0]); 143 144 StringBuffer sb = new StringBuffer (); 145 sb.append(asString(valueSet[0])); 146 for (int i=1; i<valueSet.length; i++) { 147 sb.append(multiValueDelimiter); 148 sb.append(asString(valueSet[i])); 149 } 150 return sb.toString(); 151 } 152 153 return asString(values); 154 } 155 156 162 public int compareTo(Object otherProperty) { 163 float otherOrder = ((PropertyDescriptor)otherProperty).uiOrder(); 164 return (int) (otherOrder - uiOrder); 165 } 166 167 173 public String errorFor(Object value) { 174 175 String typeError = typeErrorFor(value); 176 if (typeError != null) return typeError; 177 return valueErrorFor(value); 178 } 179 180 185 protected String valueErrorFor(Object value) { 186 return null; 188 } 189 190 195 protected boolean isArray(Object value) { 196 return value != null && value.getClass().getComponentType() != null; 197 } 198 199 204 protected String typeErrorFor(Object value) { 205 206 if (value == null && !isRequired) return null; 207 208 if (maxValueCount > 1) { 209 if (!isArray(value)) { 210 return "Value is not an array of type: " + type(); 211 } 212 213 Class arrayType = value.getClass().getComponentType(); 214 if (arrayType == null || !arrayType.isAssignableFrom(type())) { 215 return "Value is not an array of type: " + type(); 216 } 217 return null; 218 } 219 220 if (!type().isAssignableFrom(value.getClass())) { 221 return "" + value + " is not an instance of " + type(); 222 } 223 224 return null; 225 } 226 227 233 public String propertyErrorFor(Rule rule) { 234 String strValue = rule.getStringProperty(name()); 235 if (strValue == null && !isRequired()) return null; 236 Object realValue = valueFrom(strValue); 237 return errorFor(realValue); 238 } 239 240 245 public Object [][] choices() { 246 return null; 247 } 248 249 254 public int preferredRowCount() { 255 return 1; 256 } 257 258 264 public static final boolean areEqual(Object value, Object otherValue) { 265 if (value == otherValue) return true; 266 if (value == null) return false; 267 if (otherValue == null) return false; 268 269 return value.equals(otherValue); 270 } 271 } 272 | Popular Tags |