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>(Enumeration[].class) {
25
26 public EnumeratedMultiProperty createWith(Map<String, String> valuesById) {
27
28 return new EnumeratedMultiProperty(
29 nameIn(valuesById),
30 descriptionIn(valuesById),
31 labelsIn(valuesById),
32 choicesIn(valuesById),
33 indiciesIn(valuesById),
34 0f
35 );
36 }
37 };
38
39
40
41
42
43
44
45
46
47
48
49 public EnumeratedMultiProperty(String theName, String theDescription, String[] theLabels, E[] theChoices, int[] choiceIndices, float theUIOrder) {
50 super(theName, theDescription, theLabels, theChoices, choiceIndices, theUIOrder, true);
51 }
52
53
54
55
56
57 public Class<Object[]> type() {
58 return Object[].class;
59 }
60
61
62
63
64
65 @Override
66 public boolean isMultiValue() {
67 return true;
68 }
69
70
71
72
73
74
75 @Override
76 public String errorFor(Object value) {
77 Object[] values = (Object[])value;
78 for (int i=0; i<values.length; i++) {
79 if (!labelsByChoice.containsKey(values[i])) {
80 return nonLegalValueMsgFor(values[i]);
81 }
82 }
83 return null;
84 }
85
86
87
88
89
90
91
92
93 public Object[] valueFrom(String value) throws IllegalArgumentException {
94 String[] strValues = StringUtil.substringsOf(value, multiValueDelimiter());
95
96 Object[] values = new Object[strValues.length];
97 for (int i=0;i<values.length; i++) {
98 values[i] = choiceFrom(strValues[i]);
99 }
100 return values;
101 }
102
103
104
105
106
107
108
109 @Override
110 public String asDelimitedString(Object[] value) {
111 Object[] choices = value;
112
113 StringBuilder sb = new StringBuilder();
114
115 sb.append(labelsByChoice.get(choices[0]));
116
117 for (int i=1; i<choices.length; i++) {
118 sb.append(multiValueDelimiter());
119 sb.append(labelsByChoice.get(choices[i]));
120 }
121
122 return sb.toString();
123 }
124 }