1
2
3
4 package net.sourceforge.pmd.lang.rule.properties;
5
6 import java.util.Map;
7
8 import net.sourceforge.pmd.PropertyDescriptorFactory;
9 import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
10 import net.sourceforge.pmd.util.StringUtil;
11
12
13
14
15
16
17 public class CharacterMultiProperty extends AbstractDelimitedProperty<Character[]> {
18
19 public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<CharacterMultiProperty>(Character[].class) {
20
21 public CharacterMultiProperty createWith(Map<String, String> valuesById) {
22 return new CharacterMultiProperty(
23 nameIn(valuesById),
24 descriptionIn(valuesById),
25 defaultValueIn(valuesById),
26 null
27 );
28 }
29 };
30
31
32
33
34
35
36
37
38
39
40 public CharacterMultiProperty(String theName, String theDescription, Character[] theDefaults, float theUIOrder, char delimiter) {
41 super(theName, theDescription, theDefaults, delimiter, theUIOrder);
42
43 if (theDefaults != null) {
44 for (int i=0; i<theDefaults.length; i++) {
45 if (theDefaults[i].charValue() == delimiter) {
46 throw new IllegalArgumentException("Cannot include the delimiter in the set of defaults");
47 }
48 }
49 }
50 }
51
52
53
54
55
56
57
58
59
60 public CharacterMultiProperty(String theName, String theDescription, String theDefaults, Map<String, String> otherParams) {
61 this(theName, theDescription, charsIn(theDefaults, delimiterIn(otherParams)), 0.0f, delimiterIn(otherParams));
62 }
63
64 private static Character[] charsIn(String charString, char delimiter) {
65
66 String[] values = StringUtil.substringsOf(charString, delimiter);
67 Character[] chars = new Character[values.length];
68
69 for (int i=0; i<values.length;i++) {
70 if (values.length != 1) {
71 throw new IllegalArgumentException("missing/ambiguous character value");
72 }
73 chars[i] = values[i].charAt(0);
74 }
75 return chars;
76 }
77
78
79
80
81
82 public Class<Character[]> type() {
83 return Character[].class;
84 }
85
86
87
88
89
90
91
92 public Character[] valueFrom(String valueString) throws IllegalArgumentException {
93 String[] values = StringUtil.substringsOf(valueString, multiValueDelimiter());
94
95 Character[] chars = new Character[values.length];
96 for (int i=0; i<values.length; i++) {
97 chars[i] = Character.valueOf(values[i].charAt(0));
98 }
99 return chars;
100 }
101 }