1 package net.sourceforge.pmd.lang.rule.properties.factories;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import net.sourceforge.pmd.PropertyDescriptor;
9 import net.sourceforge.pmd.PropertyDescriptorFactory;
10 import net.sourceforge.pmd.util.CollectionUtil;
11 import net.sourceforge.pmd.util.StringUtil;
12 import static net.sourceforge.pmd.PropertyDescriptorFields.*;
13
14
15
16
17
18
19
20 public class BasicPropertyDescriptorFactory<T> implements PropertyDescriptorFactory {
21
22 private final Class<?> valueType;
23 private final Map<String, Boolean> fieldTypesByKey;
24
25 protected static final Map<String, Boolean> coreFieldTypesByKey = CollectionUtil.mapFrom(
26 new String[] { NAME, DESC, DEFAULT_VALUE},
27 new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE}
28 );
29
30 public BasicPropertyDescriptorFactory(Class<?> theValueType) {
31 valueType = theValueType;
32 fieldTypesByKey = Collections.unmodifiableMap(coreFieldTypesByKey);
33 }
34
35
36
37
38
39
40
41
42
43
44
45 public BasicPropertyDescriptorFactory(Class<?> theValueType, Map<String, Boolean> additionalFieldTypesByKey) {
46
47 valueType = theValueType;
48 Map<String, Boolean> temp = new HashMap<String, Boolean>(coreFieldTypesByKey.size() + additionalFieldTypesByKey.size());
49 temp.putAll(coreFieldTypesByKey);
50 temp.putAll(additionalFieldTypesByKey);
51
52 fieldTypesByKey = Collections.unmodifiableMap(temp);
53 }
54
55 public Class<?> valueType() {
56 return valueType;
57 }
58
59 public PropertyDescriptor<?> createWith(Map<String, String> valuesById) {
60 throw new RuntimeException("Unimplemented createWith() method in subclass");
61 }
62
63 public Map<String, Boolean> expectedFields() {
64 return fieldTypesByKey;
65 }
66
67 protected String nameIn(Map<String, String> valuesById) {
68 return valuesById.get(NAME);
69 }
70
71 protected String descriptionIn(Map<String, String> valuesById) {
72 return valuesById.get(DESC);
73 }
74
75 protected String defaultValueIn(Map<String, String> valuesById) {
76 return valuesById.get(DEFAULT_VALUE);
77 }
78
79 protected String numericDefaultValueIn(Map<String, String> valuesById) {
80 String number = defaultValueIn(valuesById);
81 return StringUtil.isEmpty(number) ? "0" : number;
82 }
83
84 protected static String minValueIn(Map<String, String> valuesById) {
85 return valuesById.get(MIN);
86 }
87
88 protected static String maxValueIn(Map<String, String> valuesById) {
89 return valuesById.get(MAX);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 protected static Integer[] integersIn(String numberString) {
108 String[] values = numberString.split(",");
109 List<Integer> ints = new ArrayList<Integer>(values.length);
110 for (String value : values) {
111 try {
112 Integer newInt = Integer.parseInt(value);
113 ints.add(newInt);
114 } catch (Exception ex) {
115
116 }
117 }
118 return ints.toArray(new Integer[ints.size()]);
119 }
120
121 protected static Long[] longsIn(String numberString) {
122 String[] values = numberString.split(",");
123 List<Long> longs = new ArrayList<Long>(values.length);
124 for (String value : values) {
125 try {
126 Long newLong = Long.parseLong(value);
127 longs.add(newLong);
128 } catch (Exception ex) {
129
130 }
131 }
132 return longs.toArray(new Long[longs.size()]);
133 }
134
135 protected static Float[] floatsIn(String numberString) {
136 String[] values = numberString.split(",");
137 List<Float> floats = new ArrayList<Float>(values.length);
138 for (String value : values) {
139 try {
140 Float newFloat = Float.parseFloat(value);
141 floats.add(newFloat);
142 } catch (Exception ex) {
143
144 }
145 }
146 return floats.toArray(new Float[floats.size()]);
147 }
148
149 protected static Double[] doublesIn(String numberString) {
150 String[] values = numberString.split(",");
151 List<Double> doubles = new ArrayList<Double>(values.length);
152 for (String value : values) {
153 try {
154 Double newDouble = Double.parseDouble(value);
155 doubles.add(newDouble);
156 } catch (Exception ex) {
157
158 }
159 }
160 return doubles.toArray(new Double[doubles.size()]);
161 }
162
163 protected static String[] labelsIn(Map<String, String> valuesById) {
164 return null;
165 }
166
167 protected static Object[] choicesIn(Map<String, String> valuesById) {
168 return null;
169 }
170
171 protected static int indexIn(Map<String, String> valuesById) {
172 return 0;
173 }
174
175 protected static int[] indiciesIn(Map<String, String> valuesById) {
176 return null;
177 }
178
179 protected static char delimiterIn(Map<String, String> valuesById) {
180 String characterStr = valuesById.get(DELIMITER).trim();
181 return characterStr.charAt(0);
182 }
183
184 protected static String[] minMaxFrom(Map<String, String> valuesById) {
185 String min = minValueIn(valuesById);
186 String max = maxValueIn(valuesById);
187 if (StringUtil.isEmpty(min) || StringUtil.isEmpty(max)) {
188 throw new RuntimeException("min and max values must be specified");
189 }
190 return new String[] { min, max };
191 }
192
193 protected static String[] legalPackageNamesIn(Map<String, String> valuesById) {
194 String names = valuesById.get(LEGAL_PACKAGES);
195 if (StringUtil.isEmpty(names)) {
196 return null;
197 }
198 return StringUtil.substringsOf(names, '|');
199 }
200
201 public static Map<String, Boolean> expectedFieldTypesWith(String[] otherKeys, Boolean[] otherValues) {
202 Map<String, Boolean> largerMap = new HashMap<String, Boolean>(otherKeys.length + coreFieldTypesByKey.size());
203 largerMap.putAll(coreFieldTypesByKey);
204 for (int i=0; i<otherKeys.length; i++) {
205 largerMap.put(otherKeys[i], otherValues[i]);
206 }
207 return largerMap;
208 }
209
210
211
212
213
214
215
216 }