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 net.sourceforge.pmd.util.StringUtil;
7   
8   /**
9    * No, subclasses are not necessarily scalar per se, they're just easy to parse
10   * without error. If you can come up with a better name...
11   * 
12   * @author Brian Remedios
13   * @param <T>
14   */
15  public abstract class AbstractScalarProperty<T> extends AbstractProperty<T> {
16  
17      /**
18       * Constructor for AbstractScalarProperty.
19       * 
20       * @param theName String
21       * @param theDescription String
22       * @param theDefault Object
23       * @param theUIOrder float
24       */
25      protected AbstractScalarProperty(String theName, String theDescription, T theDefault, float theUIOrder) {
26          super(theName, theDescription, theDefault, theUIOrder);
27      }
28  
29      /**
30       * @param value String
31       * @return Object
32       */
33      protected abstract Object createFrom(String value);
34  
35      /**
36       * @param size int
37       * @return Object[]
38       */
39      protected Object[] arrayFor(int size) {
40          if (isMultiValue()) {
41              throw new IllegalStateException("Subclass '" + this.getClass().getSimpleName()
42                      + "' must implement the arrayFor(int) method.");
43          }
44          throw new UnsupportedOperationException("Arrays not supported on single valued property descriptors.");
45      }
46  
47      /**
48       * @param valueString String
49       * @return Object[]
50       * @throws IllegalArgumentException
51       * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
52       */
53      @SuppressWarnings("unchecked")
54      public T valueFrom(String valueString) throws IllegalArgumentException {
55  
56          if (!isMultiValue()) {
57              return (T) createFrom(valueString);
58          }
59  
60          String[] strValues = StringUtil.substringsOf(valueString, multiValueDelimiter());
61  
62          Object[] values = arrayFor(strValues.length);
63          for (int i = 0; i < strValues.length; i++) {
64              values[i] = createFrom(strValues[i]);
65          }
66          return (T) values;
67      }
68  }