001: package net.sourceforge.pmd.properties;
002:
003: import java.util.Map;
004:
005: import net.sourceforge.pmd.util.CollectionUtil;
006: import net.sourceforge.pmd.util.StringUtil;
007:
008: /**
009: * Defines a datatype with a set of preset values of any type as held within a pair of
010: * maps. While the values are not serialized out, the labels are and serve as keys to
011: * obtain the values.
012: *
013: * @author Brian Remedios
014: * @version $Revision$
015: */
016: public class EnumeratedProperty<E> extends AbstractPMDProperty {
017:
018: private Map<String, E> choicesByLabel;
019: private Map<E, String> labelsByChoice;
020:
021: /**
022: * Constructor for EnumeratedProperty.
023: * @param theName String
024: * @param theDescription String
025: * @param theLabels String[]
026: * @param theChoices E[]
027: * @param theUIOrder float
028: */
029: public EnumeratedProperty(String theName, String theDescription,
030: String[] theLabels, E[] theChoices, float theUIOrder) {
031: this (theName, theDescription, theLabels, theChoices,
032: theUIOrder, 1);
033: }
034:
035: /**
036: * Constructor for EnumeratedProperty.
037: * @param theName String
038: * @param theDescription String
039: * @param theLabels String[]
040: * @param theChoices E[]
041: * @param theUIOrder float
042: * @param maxValues int
043: */
044: public EnumeratedProperty(String theName, String theDescription,
045: String[] theLabels, E[] theChoices, float theUIOrder,
046: int maxValues) {
047: super (theName, theDescription, theChoices[0], theUIOrder);
048:
049: choicesByLabel = CollectionUtil.mapFrom(theLabels, theChoices);
050: labelsByChoice = CollectionUtil.invertedMapFrom(choicesByLabel);
051:
052: maxValueCount(maxValues);
053: }
054:
055: /**
056: * Method type.
057: * @return Class
058: * @see net.sourceforge.pmd.PropertyDescriptor#type()
059: */
060: public Class<Object> type() {
061: return Object.class;
062: }
063:
064: private String nonLegalValueMsgFor(Object value) {
065: return "" + value + " is not a legal value";
066: }
067:
068: /**
069: * Method errorFor.
070: * @param value Object
071: * @return String
072: * @see net.sourceforge.pmd.PropertyDescriptor#errorFor(Object)
073: */
074: public String errorFor(Object value) {
075:
076: if (maxValueCount() == 1) {
077: return labelsByChoice.containsKey(value) ? null
078: : nonLegalValueMsgFor(value);
079: }
080:
081: Object[] values = (Object[]) value;
082: for (int i = 0; i < values.length; i++) {
083: if (labelsByChoice.containsKey(values[i]))
084: continue;
085: return nonLegalValueMsgFor(values[i]);
086: }
087: return null;
088: }
089:
090: /**
091: * Method choiceFrom.
092: * @param label String
093: * @return E
094: */
095: private E choiceFrom(String label) {
096: E result = choicesByLabel.get(label);
097: if (result != null)
098: return result;
099: throw new IllegalArgumentException(label);
100: }
101:
102: /**
103: * Method valueFrom.
104: * @param value String
105: * @return Object
106: * @throws IllegalArgumentException
107: * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
108: */
109: public Object valueFrom(String value)
110: throws IllegalArgumentException {
111:
112: if (maxValueCount() == 1)
113: return choiceFrom(value);
114:
115: String[] strValues = StringUtil.substringsOf(value,
116: multiValueDelimiter);
117:
118: Object[] values = new Object[strValues.length];
119: for (int i = 0; i < values.length; i++)
120: values[i] = choiceFrom(strValues[i]);
121: return values;
122: }
123:
124: /**
125: * Method asDelimitedString.
126: * @param value Object
127: * @return String
128: * @see net.sourceforge.pmd.PropertyDescriptor#asDelimitedString(Object)
129: */
130: public String asDelimitedString(Object value) {
131:
132: if (maxValueCount() == 1)
133: return labelsByChoice.get(value);
134:
135: Object[] choices = (Object[]) value;
136:
137: StringBuffer sb = new StringBuffer();
138:
139: sb.append(labelsByChoice.get(choices[0]));
140:
141: for (int i = 1; i < choices.length; i++) {
142: sb.append(multiValueDelimiter);
143: sb.append(labelsByChoice.get(choices[i]));
144: }
145:
146: return sb.toString();
147: }
148: }
|