1
2
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
16
17
18
19
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
34
35
36
37
38
39
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
54
55
56
57 public Number lowerLimit() {
58 return lowerLimit;
59 }
60
61
62
63
64 protected String defaultAsString() {
65 return defaultValue().toString();
66 }
67
68
69
70
71
72
73 public Number upperLimit() {
74 return upperLimit;
75 }
76
77
78
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
90
91
92
93
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
108
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 }