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
25 protected AbstractScalarProperty(String theName, String theDescription, T theDefault, float theUIOrder) {
26 super(theName, theDescription, theDefault, theUIOrder);
27 }
28
29
30
31
32
33 protected abstract Object createFrom(String value);
34
35
36
37
38
39 protected Object[] arrayFor(int size) {
40 if (isMultiValue()) {
41 throw new IllegalStateException("Subclass '" + this.getClass().getSimpleName()
42 + "' must implement the arrayFor(int) method.");
43 }
44 throw new UnsupportedOperationException("Arrays not supported on single valued property descriptors.");
45 }
46
47
48
49
50
51
52
53 @SuppressWarnings("unchecked")
54 public T valueFrom(String valueString) throws IllegalArgumentException {
55
56 if (!isMultiValue()) {
57 return (T) createFrom(valueString);
58 }
59
60 String[] strValues = StringUtil.substringsOf(valueString, multiValueDelimiter());
61
62 Object[] values = arrayFor(strValues.length);
63 for (int i = 0; i < strValues.length; i++) {
64 values[i] = createFrom(strValues[i]);
65 }
66 return (T) values;
67 }
68 }