View Javadoc

    1   package net.sourceforge.pmd.lang.rule.properties;
    2
    3   import java.util.Map;
    4
    5   import net.sourceforge.pmd.NumericPropertyDescriptor;
    6   import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
    7   import static net.sourceforge.pmd.PropertyDescriptorFields.*;
    8
    9   /**
    10   * Maintains a pair of boundary limit values between which all values managed
    11   * by the subclasses must fit.
    12   *
    13   * @author Brian Remedios
    14   * @param <T>
    15   */
    16  public abstract class AbstractNumericProperty<T> extends AbstractScalarProperty<T> implements NumericPropertyDescriptor<T> {
    17
    18  	private Number lowerLimit;
    19  	private Number upperLimit;
    20
    21  	public static final Map<String, Boolean> numberFieldTypesByKey = BasicPropertyDescriptorFactory.expectedFieldTypesWith(
    22  			new String[]  { MIN,  	  MAX},
    23  			new Boolean[] { Boolean.TRUE, Boolean.TRUE}
    24  			);
    25
    26  	/**
    27  	 *
    28  	 * @param theName
    29  	 * @param theDescription
    30  	 * @param lower
    31  	 * @param upper
    32  	 * @param theDefault
    33  	 * @param theUIOrder
    34  	 * @throws IllegalArgumentException
    35  	 */
    36  	protected AbstractNumericProperty(String theName, String theDescription, Number lower, Number upper, T theDefault, float theUIOrder) {
    37  		super(theName, theDescription, theDefault, theUIOrder);
    38
    39  		if (lower.doubleValue() > upper.doubleValue()) {
    40  			throw new IllegalArgumentException("Lower limit cannot be greater than the upper limit");
    41  		}
    42
    43  		lowerLimit = lower;
    44  		upperLimit = upper;
    45  	}
    46
    47  	/**
    48  	 * Returns the minimum value that instances of the property can have
    49  	 * @return The minimum value.
    50  	 * @see net.sourceforge.pmd.NumericPropertyDescriptor#lowerLimit()
    51  	 */
    52  	public Number lowerLimit() {
    53  		return lowerLimit;
    54  	}
    55
    56      /**
    57       * @return String
    58       */
    59      protected String defaultAsString() {
    60          return defaultValue().toString();
    61      }
    62
    63  	/**
    64  	 * Returns the maximum value that instances of the property can have
    65  	 * @return The maximum value.
    66  	 * @see net.sourceforge.pmd.NumericPropertyDescriptor#upperLimit()
    67  	 */
    68  	public Number upperLimit() {
    69  		return upperLimit;
    70  	}
    71
    72  	/**
    73  	 * @return String
    74  	 */
    75  	public String rangeString() {
    76  		StringBuilder sb = new StringBuilder();
    77  		sb.append('(').append(lowerLimit);
    78  		sb.append(" -> ").append(upperLimit);
    79  		sb.append(')');
    80  		return sb.toString();
    81  	}
    82
    83  	/**
    84  	 * Returns a string describing any error the value may have when
    85  	 * characterized by the receiver.
    86  	 *
    87  	 * @param value Object
    88  	 * @return String
    89  	 */
    90  	protected String valueErrorFor(Object value) {
    91
    92  		double number = ((Number)value).doubleValue();
    93
    94  		if (number > upperLimit.doubleValue() || number < lowerLimit.doubleValue() ) {
    95  			return value + " is out of range " + rangeString();
    96  		}
    97
    98  		return null;
    99  	}
    100
    101 	/**
    102 	 * Method addAttributesTo.
    103 	 * @param attributes Map<String,String>
    104 	 */
    105 	protected void addAttributesTo(Map<String, String> attributes) {
    106 		super.addAttributesTo(attributes);
    107
    108 		attributes.put(MIN, lowerLimit.toString());
    109 		attributes.put(MAX, upperLimit.toString());
    110 	}
    111 }