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  import net.sourceforge.pmd.util.StringUtil;
12  
13  /**
14   * Defines a datatype with a set of preset values of any type as held within a pair of
15   * maps. While the values are not serialized out, the labels are and serve as keys to
16   * obtain the values.  The choices() method provides the ordered selections to be used
17   * in an editor widget.
18   * 
19   * @author Brian Remedios
20   * @param <E>
21   */
22  public class EnumeratedMultiProperty<E> extends AbstractEnumeratedProperty<E, Object[]> {
23  	
24  	public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<EnumeratedMultiProperty>(Enumeration[].class) {
25  
26  		public EnumeratedMultiProperty createWith(Map<String, String> valuesById) {
27  
28  			return new EnumeratedMultiProperty(
29  					nameIn(valuesById),
30  					descriptionIn(valuesById),
31  					labelsIn(valuesById),
32  					choicesIn(valuesById),
33  					indiciesIn(valuesById),
34  					0f
35  					);
36  		}
37  	};
38  	
39  	/**
40  	 * Constructor for EnumeratedProperty.
41  	 * @param theName String
42  	 * @param theDescription String
43       * @param theLabels String[]
44       * @param theChoices E[]
45       * @param choiceIndices int[]
46  	 * @param theUIOrder float
47  	 * @throws IllegalArgumentException
48  	 */
49  	public EnumeratedMultiProperty(String theName, String theDescription, String[] theLabels, E[] theChoices, int[] choiceIndices, float theUIOrder) {
50  		super(theName, theDescription, theLabels, theChoices, choiceIndices, theUIOrder, true);
51  	}
52  	
53  	/**
54  	 * @return Class
55  	 * @see net.sourceforge.pmd.PropertyDescriptor#type()
56  	 */
57  	public Class<Object[]> type() {
58  		return Object[].class;
59  	}
60  	
61  	/**
62  	 * @return boolean
63  	 * @see net.sourceforge.pmd.PropertyDescriptor#isMultiValue()
64  	 */
65  	@Override
66  	public boolean isMultiValue() {
67  		return true;
68  	}
69  	
70  	/**
71  	 * @param value Object
72  	 * @return String
73  	 * @see net.sourceforge.pmd.PropertyDescriptor#errorFor(Object)
74  	 */
75  	@Override
76  	public String errorFor(Object value) {
77  		Object[] values = (Object[])value;
78  		for (int i=0; i<values.length; i++) {
79  			if (!labelsByChoice.containsKey(values[i])) {
80  				return nonLegalValueMsgFor(values[i]);
81  			}
82  		}
83  		return null;
84  	}
85  	
86  	/**
87  	 * 
88  	 * @param value String
89  	 * @return Object
90  	 * @throws IllegalArgumentException
91  	 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
92  	 */
93  	public Object[] valueFrom(String value) throws IllegalArgumentException {
94  		String[] strValues = StringUtil.substringsOf(value, multiValueDelimiter());
95  		
96  		Object[] values = new Object[strValues.length];
97  		for (int i=0;i<values.length; i++) {
98  		    values[i] = choiceFrom(strValues[i]);
99  		}
100 		return values;
101 	}
102 	
103 	/**
104 	 * 
105 	 * @param value Object
106 	 * @return String
107 	 * @see net.sourceforge.pmd.PropertyDescriptor#asDelimitedString(Object)
108 	 */
109 	@Override
110 	public String asDelimitedString(Object[] value) {
111 		Object[] choices = value;
112 		
113 		StringBuilder sb = new StringBuilder();
114 
115 		sb.append(labelsByChoice.get(choices[0]));
116 		
117 		for (int i=1; i<choices.length; i++) {
118 			sb.append(multiValueDelimiter());
119 			sb.append(labelsByChoice.get(choices[i]));
120 		}
121 
122 		return sb.toString();
123 	}
124 }