Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 271 |  | Methods: | 21
| NCLOC: | 114 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
AbstractPMDProperty.java | 46.9% | 55.6% | 57.1% | 53.4% |

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 | 604 | protected AbstractPMDProperty(String theName, String theDescription, Object theDefault, float theUIOrder) {
31 | 604 | name = theName;
32 | 604 | description = theDescription;
33 | 604 | defaultValue = theDefault;
34 | 604 | uiOrder = theUIOrder;
35 |  | }
36 |  |
37 |  | /**
38 |  | * Method multiValueDelimiter.
39 |  | * @param aDelimiter char
40 |  | */
41 | 180 | protected void multiValueDelimiter(char aDelimiter) {
42 | 180 | multiValueDelimiter = aDelimiter;
43 |  | }
44 |  |
45 |  | /**
46 |  | * Method multiValueDelimiter.
47 |  | * @return char
48 |  | * @see net.sourceforge.pmd.PropertyDescriptor#multiValueDelimiter()
49 |  | */
50 | 0 | public char multiValueDelimiter() {
51 | 0 | return multiValueDelimiter;
52 |  | }
53 |  |
54 |  | /**
55 |  | * Method name.
56 |  | * @return String
57 |  | * @see net.sourceforge.pmd.PropertyDescriptor#name()
58 |  | */
59 | 1096 | public String name() {
60 | 1096 | return name;
61 |  | }
62 |  |
63 |  | /**
64 |  | * Method description.
65 |  | * @return String
66 |  | * @see net.sourceforge.pmd.PropertyDescriptor#description()
67 |  | */
68 | 0 | public String description() {
69 | 0 | return description;
70 |  | }
71 |  |
72 |  | /**
73 |  | *
74 |  | * @return Object
75 |  | * @see net.sourceforge.pmd.PropertyDescriptor#defaultValue()
76 |  | */
77 | 67 | public Object defaultValue() {
78 | 67 | return defaultValue;
79 |  | }
80 |  |
81 |  | /**
82 |  | * Method maxValueCount.
83 |  | * @return int
84 |  | * @see net.sourceforge.pmd.PropertyDescriptor#maxValueCount()
85 |  | */
86 | 1059 | public int maxValueCount() {
87 | 1059 | return maxValueCount;
88 |  | }
89 |  |
90 |  | /**
91 |  | * Method maxValueCount.
92 |  | * @param theCount int
93 |  | * @see net.sourceforge.pmd.PropertyDescriptor#maxValueCount()
94 |  | */
95 | 299 | protected void maxValueCount(int theCount) {
96 | 299 | maxValueCount = theCount;
97 |  | }
98 |  |
99 |  | /**
100 |  | * Method isRequired.
101 |  | * @return boolean
102 |  | * @see net.sourceforge.pmd.PropertyDescriptor#isRequired()
103 |  | */
104 | 0 | public boolean isRequired() {
105 | 0 | return isRequired;
106 |  | }
107 |  |
108 |  | /**
109 |  | * Method uiOrder.
110 |  | * @return float
111 |  | * @see net.sourceforge.pmd.PropertyDescriptor#uiOrder()
112 |  | */
113 | 0 | public float uiOrder() {
114 | 0 | 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 | 98 | protected String asString(Object value) {
125 | 98 | 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 | 43 | public String asDelimitedString(Object values) {
136 |  |
137 | 0 | if (values == null) return "";
138 |  |
139 | 43 | if (values instanceof Object[]) {
140 | 10 | Object[] valueSet = (Object[])values;
141 | 0 | if (valueSet.length == 0) return "";
142 | 0 | if (valueSet.length == 1) return asString(valueSet[0]);
143 |  |
144 | 10 | StringBuffer sb = new StringBuffer();
145 | 10 | sb.append(asString(valueSet[0]));
146 | 10 | for (int i=1; i<valueSet.length; i++) {
147 | 66 | sb.append(multiValueDelimiter);
148 | 66 | sb.append(asString(valueSet[i]));
149 |  | }
150 | 10 | return sb.toString();
151 |  | }
152 |  |
153 | 33 | 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 | 0 | public int compareTo(Object otherProperty) {
163 | 0 | float otherOrder = ((PropertyDescriptor)otherProperty).uiOrder();
164 | 0 | 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 | 14 | public String errorFor(Object value) {
174 |  |
175 | 14 | String typeError = typeErrorFor(value);
176 | 0 | if (typeError != null) return typeError;
177 | 14 | return valueErrorFor(value);
178 |  | }
179 |  |
180 |  | /**
181 |  | * Method valueErrorFor.
182 |  | * @param value Object
183 |  | * @return String
184 |  | */
185 | 10 | protected String valueErrorFor(Object value) {
186 |  | // override as required
187 | 10 | return null;
188 |  | }
189 |  |
190 |  | /**
191 |  | * Method isArray.
192 |  | * @param value Object
193 |  | * @return boolean
194 |  | */
195 | 7 | protected boolean isArray(Object value) {
196 | 7 | return value != null && value.getClass().getComponentType() != null;
197 |  | }
198 |  |
199 |  | /**
200 |  | * Method typeErrorFor.
201 |  | * @param value Object
202 |  | * @return String
203 |  | */
204 | 14 | protected String typeErrorFor(Object value) {
205 |  |
206 | 0 | if (value == null && !isRequired) return null;
207 |  |
208 | 14 | if (maxValueCount > 1) {
209 | 7 | if (!isArray(value)) {
210 | 0 | return "Value is not an array of type: " + type();
211 |  | }
212 |  |
213 | 7 | Class arrayType = value.getClass().getComponentType();
214 | 7 | if (arrayType == null || !arrayType.isAssignableFrom(type())) {
215 | 0 | return "Value is not an array of type: " + type();
216 |  | }
217 | 7 | return null;
218 |  | }
219 |  |
220 | 7 | if (!type().isAssignableFrom(value.getClass())) {
221 | 0 | return "" + value + " is not an instance of " + type();
222 |  | }
223 |  |
224 | 7 | 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 | 0 | public String propertyErrorFor(Rule rule) {
234 | 0 | String strValue = rule.getStringProperty(name());
235 | 0 | if (strValue == null && !isRequired()) return null;
236 | 0 | Object realValue = valueFrom(strValue);
237 | 0 | return errorFor(realValue);
238 |  | }
239 |  |
240 |  | /**
241 |  | * Method choices.
242 |  | * @return Object[][]
243 |  | * @see net.sourceforge.pmd.PropertyDescriptor#choices()
244 |  | */
245 | 0 | public Object[][] choices() {
246 | 0 | return null;
247 |  | }
248 |  |
249 |  | /**
250 |  | * Method preferredRowCount.
251 |  | * @return int
252 |  | * @see net.sourceforge.pmd.PropertyDescriptor#preferredRowCount()
253 |  | */
254 | 0 | public int preferredRowCount() {
255 | 0 | return 1;
256 |  | }
257 |  |
258 |  | /**
259 |  | * Method areEqual.
260 |  | * @param value Object
261 |  | * @param otherValue Object
262 |  | * @return boolean
263 |  | */
264 | 0 | public static final boolean areEqual(Object value, Object otherValue) {
265 | 0 | if (value == otherValue) return true;
266 | 0 | if (value == null) return false;
267 | 0 | if (otherValue == null) return false;
268 |  |
269 | 0 | return value.equals(otherValue);
270 |  | }
271 |  | }