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