1
2
3
4 package net.sourceforge.pmd.lang.rule.properties;
5
6 import java.util.Enumeration;
7 import java.util.Map;
8
9 import net.sourceforge.pmd.PropertyDescriptorFactory;
10 import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
11 import net.sourceforge.pmd.util.StringUtil;
12
13
14
15
16
17
18
19
20
21
22 public class EnumeratedMultiProperty<E> extends AbstractEnumeratedProperty<E, Object[]> {
23
24 public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<EnumeratedMultiProperty>(
25 Enumeration[].class) {
26
27 public EnumeratedMultiProperty createWith(Map<String, String> valuesById) {
28
29 return new EnumeratedMultiProperty(nameIn(valuesById), descriptionIn(valuesById), labelsIn(valuesById),
30 choicesIn(valuesById), indiciesIn(valuesById), 0f);
31 }
32 };
33
34
35
36
37
38
39
40
41
42
43
44
45 public EnumeratedMultiProperty(String theName, String theDescription, String[] theLabels, E[] theChoices,
46 int[] choiceIndices, float theUIOrder) {
47 super(theName, theDescription, theLabels, theChoices, choiceIndices, theUIOrder, true);
48 }
49
50
51
52
53
54 public Class<Object[]> type() {
55 return Object[].class;
56 }
57
58
59
60
61
62 @Override
63 public boolean isMultiValue() {
64 return true;
65 }
66
67
68
69
70
71
72 @Override
73 public String errorFor(Object value) {
74 Object[] values = (Object[]) value;
75 for (int i = 0; i < values.length; i++) {
76 if (!labelsByChoice.containsKey(values[i])) {
77 return nonLegalValueMsgFor(values[i]);
78 }
79 }
80 return null;
81 }
82
83
84
85
86
87
88
89
90 public Object[] valueFrom(String value) throws IllegalArgumentException {
91 String[] strValues = StringUtil.substringsOf(value, multiValueDelimiter());
92
93 Object[] values = new Object[strValues.length];
94 for (int i = 0; i < values.length; i++) {
95 values[i] = choiceFrom(strValues[i]);
96 }
97 return values;
98 }
99
100
101
102
103
104
105
106 @Override
107 public String asDelimitedString(Object[] value) {
108 Object[] choices = value;
109
110 StringBuilder sb = new StringBuilder();
111
112 sb.append(labelsByChoice.get(choices[0]));
113
114 for (int i = 1; i < choices.length; i++) {
115 sb.append(multiValueDelimiter());
116 sb.append(labelsByChoice.get(choices[i]));
117 }
118
119 return sb.toString();
120 }
121 }