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