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