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 AbstractDelimitedProperty<Character[]> {
18  	
19  	public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<CharacterMultiProperty>(Character[].class) {
20  
21  		public CharacterMultiProperty createWith(Map<String, String> valuesById) {
22  			return new CharacterMultiProperty(
23  					nameIn(valuesById),
24  					descriptionIn(valuesById),
25  					defaultValueIn(valuesById),
26  					null
27  					);
28  		}
29  	};
30  	
31  	/**
32  	 * Constructor for CharacterProperty.
33  	 * @param theName String
34  	 * @param theDescription String
35  	 * @param theDefaults char[]
36  	 * @param theUIOrder float
37  	 * @param delimiter char
38  	 * @throws IllegalArgumentException
39  	 */
40  	public CharacterMultiProperty(String theName, String theDescription, Character[] theDefaults, float theUIOrder, char delimiter) {
41  		super(theName, theDescription, theDefaults, delimiter, theUIOrder);
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  	 * Constructor for CharacterProperty that accepts additional params from a map.
54  	 * 
55  	 * @param theName
56  	 * @param theDescription
57  	 * @param theDefaults
58  	 * @param otherParams
59  	 */
60  	public CharacterMultiProperty(String theName, String theDescription, String theDefaults, Map<String, String> otherParams) {
61  	    this(theName, theDescription, charsIn(theDefaults, delimiterIn(otherParams)), 0.0f, delimiterIn(otherParams));
62  	}
63  	
64  	private static Character[] charsIn(String charString, char delimiter) {
65  	    
66  	    String[] values = StringUtil.substringsOf(charString, delimiter);
67  	    Character[] chars = new Character[values.length];
68  	    
69  	    for (int i=0; i<values.length;i++) {
70  	        if (values.length != 1) {
71  	            throw new IllegalArgumentException("missing/ambiguous character value");
72  	        }
73  	        chars[i] = values[i].charAt(0);
74  	    }
75  	    return chars;
76  	}
77  	
78  	/**
79  	 * @return Class
80  	 * @see net.sourceforge.pmd.PropertyDescriptor#type()
81  	 */
82  	public Class<Character[]> type() {
83  		return Character[].class;
84  	}
85  		
86  	/**
87  	 * @param valueString String
88  	 * @return Object
89  	 * @throws IllegalArgumentException
90  	 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
91  	 */
92  	public Character[] valueFrom(String valueString) throws IllegalArgumentException {
93  		String[] values = StringUtil.substringsOf(valueString, multiValueDelimiter());
94  		
95  		Character[] chars = new Character[values.length];
96  		for (int i=0; i<values.length; i++) {
97  		    chars[i] = Character.valueOf(values[i].charAt(0));
98  		}
99  		return chars;
100 	}
101 }