View Javadoc

1   package net.sourceforge.pmd.lang.rule.properties.factories;
2   
3   import java.util.Collections;
4   import java.util.Comparator;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import net.sourceforge.pmd.PropertyDescriptor;
9   import net.sourceforge.pmd.PropertyDescriptorFactory;
10  import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
11  import net.sourceforge.pmd.lang.rule.properties.CharacterMultiProperty;
12  import net.sourceforge.pmd.lang.rule.properties.CharacterProperty;
13  import net.sourceforge.pmd.lang.rule.properties.DoubleMultiProperty;
14  import net.sourceforge.pmd.lang.rule.properties.DoubleProperty;
15  import net.sourceforge.pmd.lang.rule.properties.EnumeratedMultiProperty;
16  import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
17  import net.sourceforge.pmd.lang.rule.properties.FileProperty;
18  import net.sourceforge.pmd.lang.rule.properties.FloatMultiProperty;
19  import net.sourceforge.pmd.lang.rule.properties.FloatProperty;
20  import net.sourceforge.pmd.lang.rule.properties.IntegerMultiProperty;
21  import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
22  import net.sourceforge.pmd.lang.rule.properties.LongMultiProperty;
23  import net.sourceforge.pmd.lang.rule.properties.LongProperty;
24  import net.sourceforge.pmd.lang.rule.properties.MethodMultiProperty;
25  import net.sourceforge.pmd.lang.rule.properties.MethodProperty;
26  import net.sourceforge.pmd.lang.rule.properties.StringMultiProperty;
27  import net.sourceforge.pmd.lang.rule.properties.StringProperty;
28  import net.sourceforge.pmd.lang.rule.properties.TypeMultiProperty;
29  import net.sourceforge.pmd.lang.rule.properties.TypeProperty;
30  /**
31   * 
32   * @author Brian Remedios
33   */
34  public class PropertyDescriptorUtil {
35  						
36  	public static final Comparator<PropertyDescriptor<?>> ComparatorByOrder = new Comparator<PropertyDescriptor<?>>() {
37  		public int compare(PropertyDescriptor<?> pd1, PropertyDescriptor<?> pd2) {
38  			return  pd2.uiOrder() > pd1.uiOrder() ? -1 : 1;
39  		}
40  	};
41  	
42      private static final Map<String, PropertyDescriptorFactory> descriptorFactoriesByType;
43      static {
44      	Map<String, PropertyDescriptorFactory> temp = new HashMap<String, PropertyDescriptorFactory>(18);
45      	
46      	temp.put("Boolean", 	BooleanProperty.FACTORY);
47      	
48      	temp.put("String", 		StringProperty.FACTORY);
49      	temp.put("String[]", 	StringMultiProperty.FACTORY);
50      	temp.put("Character", 	CharacterProperty.FACTORY);
51      	temp.put("Character[]", CharacterMultiProperty.FACTORY);
52      	
53      	temp.put("Integer", 	IntegerProperty.FACTORY);
54      	temp.put("Integer[]", 	IntegerMultiProperty.FACTORY);
55      	temp.put("Long", 		LongProperty.FACTORY);
56      	temp.put("Long[]", 		LongMultiProperty.FACTORY);
57      	temp.put("Float", 		FloatProperty.FACTORY);
58      	temp.put("Float[]", 	FloatMultiProperty.FACTORY);
59      	temp.put("Double", 		DoubleProperty.FACTORY);
60      	temp.put("Double[]", 	DoubleMultiProperty.FACTORY);
61      	
62      	temp.put("Enum", 		EnumeratedProperty.FACTORY);
63      	temp.put("Enum[]", 		EnumeratedMultiProperty.FACTORY);
64      	
65      	temp.put("Class", 		TypeProperty.FACTORY);
66      	temp.put("Class[]", 	TypeMultiProperty.FACTORY);
67      	temp.put("Method", 		MethodProperty.FACTORY);
68      	temp.put("Method[]", 	MethodMultiProperty.FACTORY);
69      	
70      	temp.put("File", 		FileProperty.FACTORY);
71  
72      	descriptorFactoriesByType = Collections.unmodifiableMap(temp);
73      	}
74      
75      public static PropertyDescriptorFactory factoryFor(String typeId) {
76      	return descriptorFactoriesByType.get(typeId);
77      }
78      
79      public static String typeIdFor(Class<?> valueType) {
80      	
81      	// a reverse lookup, not very efficient but fine for now
82      	for (Map.Entry<String, PropertyDescriptorFactory> entry : descriptorFactoriesByType.entrySet()) {
83      		if (entry.getValue().valueType() == valueType) {
84      			return entry.getKey();
85      		}
86      	}
87      	return null;
88      }    
89      
90  }