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 pair of
14   * maps. While the values are not serialized out, the labels are and serve as keys to
15   * obtain the values.  The choices() method provides the ordered selections to be used
16   * 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>(Enumeration.class) {
24  
25  		public EnumeratedProperty createWith(Map<String, String> valuesById) {
26  
27  			return new EnumeratedProperty(
28  					nameIn(valuesById),
29  					descriptionIn(valuesById),
30  					labelsIn(valuesById),
31  					choicesIn(valuesById),
32  					indexIn(valuesById),
33  					0f
34  					);
35  		}
36  	};
37  	
38  	/**
39  	 * Constructor for EnumeratedProperty.
40  	 * @param theName String
41  	 * @param theDescription String
42       * @param theLabels String[]
43     	 * @param theChoices E[]
44     	 * @param defaultIndex int
45  	 * @param theUIOrder float
46  	 * @throws IllegalArgumentException
47  	 */
48  	public EnumeratedProperty(String theName, String theDescription, String[] theLabels, E[] theChoices, int defaultIndex, float theUIOrder) {
49  		super(theName, theDescription, theLabels, theChoices, new int[] {defaultIndex}, theUIOrder, false);
50  	}
51  	
52  	/**
53  	 * @return Class
54  	 * @see net.sourceforge.pmd.PropertyDescriptor#type()
55  	 */
56  	public Class<Object> type() {
57  		return Object.class;
58  	}
59  	
60  	/**
61  	 * @param value Object
62  	 * @return String
63  	 * @see net.sourceforge.pmd.PropertyDescriptor#errorFor(Object)
64  	 */
65  	@Override
66  	public String errorFor(Object value) {
67  		return labelsByChoice.containsKey(value) ?
68  			null : nonLegalValueMsgFor(value);
69  	}
70  	
71  	/**
72  	 * @param value String
73  	 * @return Object
74  	 * @throws IllegalArgumentException
75  	 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
76  	 */
77  	public Object valueFrom(String value) throws IllegalArgumentException {
78  	    return choiceFrom(value);
79  	}
80  	
81  	/**
82  	 *
83  	 * @param value Object
84  	 * @return String
85  	 * @see net.sourceforge.pmd.PropertyDescriptor#asDelimitedString(Object)
86  	 */
87  	@Override
88  	public String asDelimitedString(Object value) {
89  	    return labelsByChoice.get(value);
90  	}
91  }