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