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