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  
11  /**
12   * Defines a property type that supports single Character values.
13   * 
14   * @author Brian Remedios
15   */
16  public class CharacterProperty extends AbstractProperty<Character> {
17  
18      public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<CharacterProperty>(
19              Character.class) {
20  
21          public CharacterProperty createWith(Map<String, String> valuesById) {
22              return new CharacterProperty(nameIn(valuesById), descriptionIn(valuesById),
23                      defaultValueIn(valuesById) != null ? new Character(defaultValueIn(valuesById).charAt(0)) : null, 0f);
24          }
25      };
26  
27      /**
28       * Constructor for CharacterProperty.
29       * 
30       * @param theName String
31       * @param theDescription String
32       * @param theDefault Character
33       * @param theUIOrder float
34       */
35      public CharacterProperty(String theName, String theDescription, Character theDefault, float theUIOrder) {
36          super(theName, theDescription, theDefault, theUIOrder);
37      }
38  
39      /**
40       * Constructor for CharacterProperty.
41       * 
42       * @param theName String
43       * @param theDescription String
44       * @param defaultStr String
45       * @param theUIOrder float
46       * @throws IllegalArgumentException
47       */
48      public CharacterProperty(String theName, String theDescription, String defaultStr, float theUIOrder) {
49          this(theName, theDescription, charFrom(defaultStr), theUIOrder);
50      }
51  
52      /**
53       * @param charStr String
54       * @return Character
55       */
56      public static Character charFrom(String charStr) {
57  
58          if (charStr == null || charStr.length() != 1) {
59              throw new IllegalArgumentException("missing/invalid character value");
60          }
61          return charStr.charAt(0);
62      }
63  
64      /**
65       * @return Class
66       * @see net.sourceforge.pmd.PropertyDescriptor#type()
67       */
68      public Class<Character> type() {
69          return Character.class;
70      }
71  
72      /**
73       * @param valueString String
74       * @return Object
75       * @throws IllegalArgumentException
76       * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
77       */
78      public Character valueFrom(String valueString) throws IllegalArgumentException {
79          return charFrom(valueString);
80      }
81  
82      /**
83       * Method defaultAsString.
84       * 
85       * @return String
86       */
87      protected String defaultAsString() {
88          return Character.toString(defaultValue());
89      }
90  }