01 /**
02 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03 */
04 package net.sourceforge.pmd.lang.rule.properties;
05
06 import net.sourceforge.pmd.util.StringUtil;
07
08 /**
09 * No, subclasses are not necessarily scalar per se, they're just easy to parse without error.
10 * If you can come up with a better name...
11 *
12 * @author Brian Remedios
13 */
14 public abstract class AbstractScalarProperty<T> extends AbstractProperty<T> {
15
16 /**
17 * Constructor for AbstractScalarProperty.
18 * @param theName String
19 * @param theDescription String
20 * @param theDefault Object
21 * @param theUIOrder float
22 */
23 protected AbstractScalarProperty(String theName, String theDescription, T theDefault, float theUIOrder) {
24 super(theName, theDescription, theDefault, theUIOrder);
25 }
26
27 /**
28 * @param value String
29 * @return Object
30 */
31 protected abstract Object createFrom(String value);
32
33 /**
34 * @param size int
35 * @return Object[]
36 */
37 protected Object[] arrayFor(int size) {
38 if (isMultiValue()) {
39 throw new IllegalStateException("Subclass '" + this.getClass().getSimpleName() + "' must implement the arrayFor(int) method.");
40 }
41 throw new UnsupportedOperationException("Arrays not supported on single valued property descriptors.");
42 }
43
44 /**
45 * @param valueString String
46 * @return Object[]
47 * @throws IllegalArgumentException
48 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
49 */
50 @SuppressWarnings("unchecked")
51 public T valueFrom(String valueString) throws IllegalArgumentException {
52
53 if (!isMultiValue()) {
54 return (T)createFrom(valueString);
55 }
56
57 String[] strValues = StringUtil.substringsOf(valueString, multiValueDelimiter());
58
59 Object[] values = arrayFor(strValues.length);
60 for (int i=0; i<strValues.length; i++) {
61 values[i] = createFrom(strValues[i]);
62 }
63 return (T)values;
64 }
65 }
|