View Javadoc

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