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