1 |
| package net.sourceforge.pmd.properties; |
2 |
| |
3 |
| import java.util.Map; |
4 |
| |
5 |
| import net.sourceforge.pmd.util.CollectionUtil; |
6 |
| import net.sourceforge.pmd.util.StringUtil; |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| public class EnumeratedProperty extends AbstractPMDProperty { |
17 |
| |
18 |
| private Object[][] choiceTuples; |
19 |
| private Map choicesByLabel; |
20 |
| private Map labelsByChoice; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
4
| public EnumeratedProperty(String theName, String theDescription, Object[][] theChoices, float theUIOrder) {
|
30 |
4
| this(theName, theDescription, theChoices, theUIOrder, 1);
|
31 |
| } |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
6
| public EnumeratedProperty(String theName, String theDescription, Object[][] theChoices, float theUIOrder, int maxValues) {
|
42 |
6
| super(theName, theDescription, theChoices[0][1], theUIOrder);
|
43 |
| |
44 |
6
| choiceTuples = theChoices;
|
45 |
6
| choicesByLabel = CollectionUtil.mapFrom(theChoices);
|
46 |
6
| labelsByChoice = CollectionUtil.invertedMapFrom(choicesByLabel);
|
47 |
| |
48 |
6
| maxValueCount(maxValues);
|
49 |
| } |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
1
| public Class type() {
|
57 |
1
| return Object.class;
|
58 |
| } |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
0
| public Object[][] choices() {
|
66 |
0
| return choiceTuples;
|
67 |
| } |
68 |
| |
69 |
0
| private String nonLegalValueMsgFor(Object value) {
|
70 |
0
| return "" + value + " is not a legal value";
|
71 |
| } |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
2
| public String errorFor(Object value) {
|
80 |
| |
81 |
2
| if (maxValueCount() == 1) {
|
82 |
1
| return labelsByChoice.containsKey(value) ?
|
83 |
| null : nonLegalValueMsgFor(value); |
84 |
| } |
85 |
| |
86 |
1
| Object[] values = (Object[])value;
|
87 |
1
| for (int i=0; i<values.length; i++) {
|
88 |
10
| if (labelsByChoice.containsKey(values[i])) continue;
|
89 |
0
| return nonLegalValueMsgFor(values[i]);
|
90 |
| } |
91 |
1
| return null;
|
92 |
| } |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
11
| private Object choiceFrom(String label) {
|
100 |
11
| Object result = choicesByLabel.get(label);
|
101 |
11
| if (result != null) return result;
|
102 |
0
| throw new IllegalArgumentException(label);
|
103 |
| } |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
2
| public Object valueFrom(String value) throws IllegalArgumentException {
|
113 |
| |
114 |
1
| if (maxValueCount() == 1) return choiceFrom(value);
|
115 |
| |
116 |
1
| String[] strValues = StringUtil.substringsOf(value, multiValueDelimiter);
|
117 |
| |
118 |
1
| Object[] values = new Object[strValues.length];
|
119 |
10
| for (int i=0;i<values.length; i++) values[i] = choiceFrom(strValues[i]);
|
120 |
1
| return values;
|
121 |
| } |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
2
| public String asDelimitedString(Object value) {
|
130 |
| |
131 |
1
| if (maxValueCount() == 1) return (String)labelsByChoice.get(value);
|
132 |
| |
133 |
1
| Object[] choices = (Object[])value;
|
134 |
| |
135 |
1
| StringBuffer sb = new StringBuffer();
|
136 |
| |
137 |
1
| sb.append(labelsByChoice.get(choices[0]));
|
138 |
| |
139 |
1
| for (int i=1; i<choices.length; i++) {
|
140 |
9
| sb.append(multiValueDelimiter);
|
141 |
9
| sb.append(labelsByChoice.get(choices[i]));
|
142 |
| } |
143 |
| |
144 |
1
| return sb.toString();
|
145 |
| } |
146 |
| } |