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 without error.
    10   * 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  	 * @param theName String
    20  	 * @param theDescription String
    21  	 * @param theDefault Object
    22  	 * @param theUIOrder float
    23  	 */
    24  	protected AbstractScalarProperty(String theName, String theDescription, T theDefault, float theUIOrder) {
    25  		super(theName, theDescription, theDefault, theUIOrder);
    26  	}
    27
    28  	/**
    29  	 * @param value String
    30  	 * @return Object
    31  	 */
    32  	protected abstract Object createFrom(String value);
    33
    34  	/**
    35  	 * @param size int
    36  	 * @return Object[]
    37  	 */
    38  	protected Object[] arrayFor(int size) {
    39  	    if (isMultiValue()) {
    40  		throw new IllegalStateException("Subclass '" + this.getClass().getSimpleName() + "' must implement the arrayFor(int) method.");
    41  	    }
    42  		throw new UnsupportedOperationException("Arrays not supported on single valued property descriptors.");
    43  	}
    44
    45  	/**
    46  	 * @param valueString String
    47  	 * @return Object[]
    48  	 * @throws IllegalArgumentException
    49  	 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
    50  	 */
    51  	@SuppressWarnings("unchecked")
    52  	public T valueFrom(String valueString) throws IllegalArgumentException {
    53
    54  		if (!isMultiValue()) {
    55  		    return (T)createFrom(valueString);
    56  		}
    57
    58  		String[] strValues = StringUtil.substringsOf(valueString, multiValueDelimiter());
    59
    60  		Object[] values = arrayFor(strValues.length);
    61  		for (int i=0; i<strValues.length; i++) {
    62  		    values[i] = createFrom(strValues[i]);
    63  		}
    64  		return (T)values;
    65  	}
    66  }