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
11
12
13
14
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
29
30
31
32
33
34
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
49
50
51
52 public Number lowerLimit() {
53 return lowerLimit;
54 }
55
56
57
58
59 protected String defaultAsString() {
60 return defaultValue().toString();
61 }
62
63
64
65
66
67
68 public Number upperLimit() {
69 return upperLimit;
70 }
71
72
73
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
85
86
87
88
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
103
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 }