1
2
3
4 package net.sourceforge.pmd.lang.rule.properties;
5
6 import net.sourceforge.pmd.util.StringUtil;
7
8
9
10
11
12
13
14
15 public abstract class AbstractScalarProperty<T> extends AbstractProperty<T> {
16
17
18
19
20
21
22
23
24 protected AbstractScalarProperty(String theName, String theDescription, T theDefault, float theUIOrder) {
25 super(theName, theDescription, theDefault, theUIOrder);
26 }
27
28
29
30
31
32 protected abstract Object createFrom(String value);
33
34
35
36
37
38 protected Object[] arrayFor(int size) {
39 if (isMultiValue()) {
40 throw new IllegalStateException("Subclass '" + this.getClass().getSimpleName() + "' must implement the arrayFor(int) method.");
41 }
42 throw new UnsupportedOperationException("Arrays not supported on single valued property descriptors.");
43 }
44
45
46
47
48
49
50
51 @SuppressWarnings("unchecked")
52 public T valueFrom(String valueString) throws IllegalArgumentException {
53
54 if (!isMultiValue()) {
55 return (T)createFrom(valueString);
56 }
57
58 String[] strValues = StringUtil.substringsOf(valueString, multiValueDelimiter());
59
60 Object[] values = arrayFor(strValues.length);
61 for (int i=0; i<strValues.length; i++) {
62 values[i] = createFrom(strValues[i]);
63 }
64 return (T)values;
65 }
66 }