View Javadoc

1   package net.sourceforge.pmd.properties;
2   
3   import net.sourceforge.pmd.PropertyDescriptor;
4   import net.sourceforge.pmd.Rule;
5   
6   
7   /**
8    * 
9    * @author Brian Remedios
10   * @version $Revision$
11   */
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  	/**
24  	 * Constructor for AbstractPMDProperty.
25  	 * @param theName String
26  	 * @param theDescription String
27  	 * @param theDefault Object
28  	 * @param theUIOrder float
29  	 */
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  	/**
38  	 * Method multiValueDelimiter.
39  	 * @param aDelimiter char
40  	 */
41  	protected void multiValueDelimiter(char aDelimiter) {
42  		multiValueDelimiter = aDelimiter;
43  	}
44  	
45  	/**
46  	 * Method multiValueDelimiter.
47  	 * @return char
48  	 * @see net.sourceforge.pmd.PropertyDescriptor#multiValueDelimiter()
49  	 */
50  	public char multiValueDelimiter() {
51  		return multiValueDelimiter;
52  	}
53  	
54  	/**
55  	 * Method name.
56  	 * @return String
57  	 * @see net.sourceforge.pmd.PropertyDescriptor#name()
58  	 */
59  	public String name() {
60  		return name;
61  	}
62  
63  	/**
64  	 * Method description.
65  	 * @return String
66  	 * @see net.sourceforge.pmd.PropertyDescriptor#description()
67  	 */
68  	public String description() {
69  		return description;
70  	}
71  
72  	/**
73  	 * 
74  	 * @return Object
75  	 * @see net.sourceforge.pmd.PropertyDescriptor#defaultValue()
76  	 */
77  	public Object defaultValue() {
78  		return defaultValue;
79  	}
80  	
81  	/**
82  	 * Method maxValueCount.
83  	 * @return int
84  	 * @see net.sourceforge.pmd.PropertyDescriptor#maxValueCount()
85  	 */
86  	public int maxValueCount() {
87  		return maxValueCount;
88  	}
89  	
90  	/**
91  	 * Method maxValueCount.
92  	 * @param theCount int
93  	 * @see net.sourceforge.pmd.PropertyDescriptor#maxValueCount()
94  	 */
95  	protected void maxValueCount(int theCount) {
96  		maxValueCount = theCount;
97  	}
98  	
99  	/**
100 	 * Method isRequired.
101 	 * @return boolean
102 	 * @see net.sourceforge.pmd.PropertyDescriptor#isRequired()
103 	 */
104 	public boolean isRequired() {
105 		return isRequired;
106 	}
107 	
108 	/**
109 	 * Method uiOrder.
110 	 * @return float
111 	 * @see net.sourceforge.pmd.PropertyDescriptor#uiOrder()
112 	 */
113 	public float uiOrder() {
114 		return uiOrder;
115 	}
116 	
117 	/**
118 	 * Return the value as a string that can be easily recognized and parsed
119 	 * when we see it again.
120 	 * 
121 	 * @param value Object
122 	 * @return String
123 	 */
124 	protected String asString(Object value) {
125 		return value == null ? "" : value.toString();
126 	}
127 	
128 	
129 	/**
130 	 * Method asDelimitedString.
131 	 * @param values Object
132 	 * @return String
133 	 * @see net.sourceforge.pmd.PropertyDescriptor#asDelimitedString(Object)
134 	 */
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 	/**
157 	 * Method compareTo.
158 	 * @param otherProperty Object
159 	 * @return int
160 	 * @see java.lang.Comparable#compareTo(Object)
161 	 */
162 	public int compareTo(PropertyDescriptor otherProperty) {
163 		float otherOrder = otherProperty.uiOrder();
164 		return (int) (otherOrder - uiOrder);
165 	}
166 	
167 	/**
168 	 * Method errorFor.
169 	 * @param value Object
170 	 * @return String
171 	 * @see net.sourceforge.pmd.PropertyDescriptor#errorFor(Object)
172 	 */
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 	/**
181 	 * Method valueErrorFor.
182 	 * @param value Object
183 	 * @return String
184 	 */
185 	protected String valueErrorFor(Object value) {
186 		// override as required
187 		return null;
188 	}
189 	
190 	/**
191 	 * Method isArray.
192 	 * @param value Object
193 	 * @return boolean
194 	 */
195 	protected boolean isArray(Object value) {
196 		return value != null && value.getClass().getComponentType() != null;
197 	}
198 	
199 	/**
200 	 * Method typeErrorFor.
201 	 * @param value Object
202 	 * @return String
203 	 */
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 	/**
228 	 * Method propertyErrorFor.
229 	 * @param rule Rule
230 	 * @return String
231 	 * @see net.sourceforge.pmd.PropertyDescriptor#propertyErrorFor(Rule)
232 	 */
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 	/**
241 	 * Method choices.
242 	 * @return Object[][]
243 	 * @see net.sourceforge.pmd.PropertyDescriptor#choices()
244 	 */
245 	public Object[][] choices() {
246 		return null;
247 	}
248 	
249 	/**
250 	 * Method preferredRowCount.
251 	 * @return int
252 	 * @see net.sourceforge.pmd.PropertyDescriptor#preferredRowCount()
253 	 */
254 	public int preferredRowCount() {
255 		return 1;
256 	}
257 	
258 	/**
259 	 * Method areEqual.
260 	 * @param value Object
261 	 * @param otherValue Object
262 	 * @return boolean
263 	 */
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 }