EnumeratedProperty.java
01 /**
02  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03  */
04 package net.sourceforge.pmd.lang.rule.properties;
05 
06 /**
07  * Defines a datatype with a set of preset values of any type as held within a pair of
08  * maps. While the values are not serialized out, the labels are and serve as keys to
09  * obtain the values.  The choices() method provides the ordered selections to be used
10  * in an editor widget.
11  
12  @author Brian Remedios
13  */
14 public class EnumeratedProperty<E> extends AbstractEnumeratedProperty<E, Object> {
15   
16   /**
17    * Constructor for EnumeratedProperty.
18    @param theName String
19    @param theDescription String
20      @param theLabels String[]
21       @param theChoices E[]
22       @param defaultIndex int
23    @param theUIOrder float
24    @throws IllegalArgumentException
25    */
26   public EnumeratedProperty(String theName, String theDescription, String[] theLabels, E[] theChoices, int defaultIndex, float theUIOrder) {
27     super(theName, theDescription, theLabels, theChoices, new int[] {defaultIndex}, theUIOrder, false);
28   }
29   
30   /**
31    @return Class
32    @see net.sourceforge.pmd.PropertyDescriptor#type()
33    */
34   public Class<Object> type() {
35     return Object.class;
36   }
37   
38   /**
39    @param value Object
40    @return String
41    @see net.sourceforge.pmd.PropertyDescriptor#errorFor(Object)
42    */
43   @Override
44   public String errorFor(Object value) {
45     return labelsByChoice.containsKey(value?
46       null : nonLegalValueMsgFor(value);
47   }
48   
49   /**
50    @param value String
51    @return Object
52    @throws IllegalArgumentException
53    @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
54    */
55   public Object valueFrom(String valuethrows IllegalArgumentException {
56       return choiceFrom(value);
57   }
58   
59   /**
60    *
61    @param value Object
62    @return String
63    @see net.sourceforge.pmd.PropertyDescriptor#asDelimitedString(Object)
64    */
65   @Override
66   public String asDelimitedString(Object value) {
67       return labelsByChoice.get(value);
68   }
69 }