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.Map;
7   
8   import net.sourceforge.pmd.PropertyDescriptorFactory;
9   import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
10  import net.sourceforge.pmd.util.StringUtil;
11  
12  /**
13   * Defines a property type that supports multiple Character values.
14   * 
15   * @author Brian Remedios
16   */
17  public class CharacterMultiProperty extends AbstractProperty<Character[]> {
18  
19      public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<CharacterMultiProperty>(
20              Character[].class) {
21  
22          public CharacterMultiProperty createWith(Map<String, String> valuesById) {
23              char delimiter = delimiterIn(valuesById);
24              return new CharacterMultiProperty(nameIn(valuesById), descriptionIn(valuesById), charsIn(
25                      defaultValueIn(valuesById), delimiter), 0.0f, delimiter);
26          }
27      };
28  
29      /**
30       * Constructor for CharacterProperty.
31       * 
32       * @param theName String
33       * @param theDescription String
34       * @param theDefaults char[]
35       * @param theUIOrder float
36       * @param delimiter char
37       * @throws IllegalArgumentException
38       */
39      public CharacterMultiProperty(String theName, String theDescription, Character[] theDefaults, float theUIOrder,
40              char delimiter) {
41          super(theName, theDescription, theDefaults, theUIOrder, delimiter);
42  
43          if (theDefaults != null) {
44              for (int i = 0; i < theDefaults.length; i++) {
45                  if (theDefaults[i].charValue() == delimiter) {
46                      throw new IllegalArgumentException("Cannot include the delimiter in the set of defaults");
47                  }
48              }
49          }
50      }
51  
52      /**
53       * @return Class
54       * @see net.sourceforge.pmd.PropertyDescriptor#type()
55       */
56      public Class<Character[]> type() {
57          return Character[].class;
58      }
59  
60      /**
61       * @param valueString String
62       * @return Object
63       * @throws IllegalArgumentException
64       * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
65       */
66      public Character[] valueFrom(String valueString) throws IllegalArgumentException {
67          String[] values = StringUtil.substringsOf(valueString, multiValueDelimiter());
68  
69          Character[] chars = new Character[values.length];
70          for (int i = 0; i < values.length; i++) {
71              chars[i] = Character.valueOf(values[i].charAt(0));
72          }
73          return chars;
74      }
75  
76      /**
77       * @return boolean
78       * @see net.sourceforge.pmd.PropertyDescriptor#isMultiValue()
79       */
80      @Override
81      public boolean isMultiValue() {
82          return true;
83      }
84  }