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;
5   
6   import java.util.Enumeration;
7   import java.util.Map;
8   
9   import net.sourceforge.pmd.PropertyDescriptorFactory;
10  import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
11  
12  /**
13   * Defines a datatype with a set of preset values of any type as held within a
14   * pair of maps. While the values are not serialized out, the labels are and
15   * serve as keys to obtain the values. The choices() method provides the ordered
16   * selections to be used in an editor widget.
17   * 
18   * @author Brian Remedios
19   * @param <E>
20   */
21  public class EnumeratedProperty<E> extends AbstractEnumeratedProperty<E, Object> {
22  
23      public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<EnumeratedProperty>(
24              Enumeration.class) {
25  
26          public EnumeratedProperty createWith(Map<String, String> valuesById) {
27  
28              return new EnumeratedProperty(nameIn(valuesById), descriptionIn(valuesById), labelsIn(valuesById),
29                      choicesIn(valuesById), indexIn(valuesById), 0f);
30          }
31      };
32  
33      /**
34       * Constructor for EnumeratedProperty.
35       * 
36       * @param theName String
37       * @param theDescription String
38       * @param theLabels String[]
39       * @param theChoices E[]
40       * @param defaultIndex int
41       * @param theUIOrder float
42       * @throws IllegalArgumentException
43       */
44      public EnumeratedProperty(String theName, String theDescription, String[] theLabels, E[] theChoices,
45              int defaultIndex, float theUIOrder) {
46          super(theName, theDescription, theLabels, theChoices, new int[] { defaultIndex }, theUIOrder, false);
47      }
48  
49      /**
50       * @return Class
51       * @see net.sourceforge.pmd.PropertyDescriptor#type()
52       */
53      public Class<Object> type() {
54          return Object.class;
55      }
56  
57      /**
58       * @param value Object
59       * @return String
60       * @see net.sourceforge.pmd.PropertyDescriptor#errorFor(Object)
61       */
62      @Override
63      public String errorFor(Object value) {
64          return labelsByChoice.containsKey(value) ? null : nonLegalValueMsgFor(value);
65      }
66  
67      /**
68       * @param value String
69       * @return Object
70       * @throws IllegalArgumentException
71       * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
72       */
73      public Object valueFrom(String value) throws IllegalArgumentException {
74          return choiceFrom(value);
75      }
76  
77      /**
78       *
79       * @param value Object
80       * @return String
81       * @see net.sourceforge.pmd.PropertyDescriptor#asDelimitedString(Object)
82       */
83      @Override
84      public String asDelimitedString(Object value) {
85          return labelsByChoice.get(value);
86      }
87  }