001: package net.sourceforge.pmd.properties;
002:
003: import net.sourceforge.pmd.util.StringUtil;
004:
005: /**
006: * Defines a property type that supports Character values.
007: *
008: * @author Brian Remedios
009: * @version $Revision$
010: */
011: public class CharacterProperty extends AbstractPMDProperty {
012:
013: /**
014: * Constructor for CharacterProperty.
015: * @param theName String
016: * @param theDescription String
017: * @param theDefault char
018: * @param theUIOrder float
019: */
020: public CharacterProperty(String theName, String theDescription,
021: char theDefault, float theUIOrder) {
022: super (theName, theDescription, Character.valueOf(theDefault),
023: theUIOrder);
024: }
025:
026: /**
027: * Constructor for CharacterProperty.
028: * @param theName String
029: * @param theDescription String
030: * @param theDefaults char[]
031: * @param theUIOrder float
032: * @param delimiter char
033: */
034: public CharacterProperty(String theName, String theDescription,
035: char[] theDefaults, float theUIOrder, char delimiter) {
036: this (theName, theDescription, asCharacters(theDefaults),
037: theUIOrder, delimiter);
038: }
039:
040: /**
041: * Constructor for CharacterProperty.
042: * @param theName String
043: * @param theDescription String
044: * @param theDefaults String
045: * @param theUIOrder float
046: * @param delimiter char
047: */
048: public CharacterProperty(String theName, String theDescription,
049: String theDefaults, float theUIOrder, char delimiter) {
050: this (theName, theDescription, theDefaults.toCharArray(),
051: theUIOrder, delimiter);
052: }
053:
054: /**
055: * Constructor for CharacterProperty.
056: * @param theName String
057: * @param theDescription String
058: * @param theDefaults char[]
059: * @param theUIOrder float
060: * @param delimiter char
061: */
062: public CharacterProperty(String theName, String theDescription,
063: Character[] theDefaults, float theUIOrder, char delimiter) {
064: super (theName, theDescription, theDefaults, theUIOrder);
065:
066: multiValueDelimiter(delimiter);
067: maxValueCount(Integer.MAX_VALUE);
068: }
069:
070: /**
071: * Method asCharacters.
072: * @param chars char[]
073: * @return Character[]
074: */
075: private static final Character[] asCharacters(char[] chars) {
076: Character[] characters = new Character[chars.length];
077: for (int i = 0; i < chars.length; i++)
078: characters[i] = Character.valueOf(chars[i]);
079: return characters;
080: }
081:
082: /**
083: * Method type.
084: * @return Class
085: * @see net.sourceforge.pmd.PropertyDescriptor#type()
086: */
087: public Class<Character> type() {
088: return Character.class;
089: }
090:
091: /**
092: * Method valueFrom.
093: * @param valueString String
094: * @return Object
095: * @throws IllegalArgumentException
096: * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
097: */
098: public Object valueFrom(String valueString)
099: throws IllegalArgumentException {
100:
101: if (maxValueCount() == 1) {
102: if (valueString.length() > 1)
103: throw new IllegalArgumentException(valueString);
104: return Character.valueOf(valueString.charAt(0));
105: }
106:
107: String[] values = StringUtil.substringsOf(valueString,
108: multiValueDelimiter);
109:
110: Character[] chars = new Character[values.length];
111: for (int i = 0; i < values.length; i++)
112: chars[i] = Character.valueOf(values[i].charAt(0));
113: return chars;
114: }
115: }
|