1 | package edu.stanford.rt.datatype; |
---|
2 | |
---|
3 | /** |
---|
4 | * @author Ninghui Li, Sandra Qiu<br> |
---|
5 | * |
---|
6 | * Implementation of <code>IntegerType</code> element |
---|
7 | */ |
---|
8 | public class IntegerType extends OrderedType |
---|
9 | { |
---|
10 | private long min; |
---|
11 | private long max; |
---|
12 | private boolean includeMin = true; |
---|
13 | private boolean includeMax = true; |
---|
14 | private long base; |
---|
15 | private long step; |
---|
16 | |
---|
17 | /** |
---|
18 | * Constructor for IntegerType. |
---|
19 | */ |
---|
20 | public IntegerType( |
---|
21 | String name, |
---|
22 | long min, |
---|
23 | long max, |
---|
24 | boolean includeMin, |
---|
25 | boolean includeMax, |
---|
26 | long base, |
---|
27 | long step) |
---|
28 | throws IllegalArgumentException |
---|
29 | { |
---|
30 | super(name); |
---|
31 | |
---|
32 | // Check to make sure that at least one value is legal |
---|
33 | if (min > max) |
---|
34 | { |
---|
35 | throw new IllegalArgumentException("IntegerType: min > max"); |
---|
36 | } |
---|
37 | if (base < min || base > max) |
---|
38 | { |
---|
39 | throw new IllegalArgumentException("IntegerType: base in not within min and max"); |
---|
40 | } |
---|
41 | if (step <= 0) |
---|
42 | { |
---|
43 | throw new IllegalArgumentException("IntegerType: step <= 0"); |
---|
44 | } |
---|
45 | |
---|
46 | long x = (min - base) % step; |
---|
47 | if (x > 0 && (min + step - x) > max) |
---|
48 | { |
---|
49 | throw new IllegalArgumentException("IntegerType: First legal value is out of bound."); |
---|
50 | } |
---|
51 | |
---|
52 | this.min = min; |
---|
53 | this.max = max; |
---|
54 | this.includeMin = includeMin; |
---|
55 | this.includeMax = includeMax; |
---|
56 | this.base = base; |
---|
57 | this.step = step; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Method getMin. |
---|
62 | * @return long |
---|
63 | */ |
---|
64 | public long getMin() |
---|
65 | { |
---|
66 | return min; |
---|
67 | } |
---|
68 | /** |
---|
69 | * Method getMax. |
---|
70 | * @return long |
---|
71 | */ |
---|
72 | public long getMax() |
---|
73 | { |
---|
74 | return max; |
---|
75 | } |
---|
76 | /** |
---|
77 | * Method isMinIncluded. |
---|
78 | * @return boolean |
---|
79 | */ |
---|
80 | public boolean isMinIncluded() |
---|
81 | { |
---|
82 | return includeMin; |
---|
83 | } |
---|
84 | /** |
---|
85 | * Method isMaxIncluded. |
---|
86 | * @return boolean |
---|
87 | */ |
---|
88 | public boolean isMaxIncluded() |
---|
89 | { |
---|
90 | return includeMax; |
---|
91 | } |
---|
92 | /** |
---|
93 | * Method getBase. |
---|
94 | * @return long |
---|
95 | */ |
---|
96 | public long getBase() |
---|
97 | { |
---|
98 | return base; |
---|
99 | } |
---|
100 | /** |
---|
101 | * Method getStep. |
---|
102 | * @return long |
---|
103 | */ |
---|
104 | public long getStep() |
---|
105 | { |
---|
106 | return step; |
---|
107 | } |
---|
108 | |
---|
109 | /* (non-Javadoc) |
---|
110 | * @see edu.stanford.rt.datatype.SimpleType#isValidValue(DataValue) |
---|
111 | */ |
---|
112 | public boolean isValidValue(DataValue v) |
---|
113 | { |
---|
114 | if (!(v instanceof IntegerValue)) |
---|
115 | { |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | long l = ((IntegerValue) v).getValue(); |
---|
120 | if (l >= min && l <= max && ((l - base) % step == 0)) |
---|
121 | { |
---|
122 | return true; |
---|
123 | } |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | /* (non-Javadoc) |
---|
128 | * @see edu.stanford.rt.datatype.OrderedType#compares(DataValue, DataValue) |
---|
129 | */ |
---|
130 | public int compares(DataValue value1, DataValue value2) |
---|
131 | { |
---|
132 | if (!(value1 instanceof IntegerValue) |
---|
133 | || !(value2 instanceof IntegerValue)) |
---|
134 | throw new IllegalArgumentException("Wrong argument type"); |
---|
135 | |
---|
136 | long val1 = ((IntegerValue) value1).getValue(); |
---|
137 | long val2 = ((IntegerValue) value2).getValue(); |
---|
138 | |
---|
139 | if (val1 < val2) |
---|
140 | return -1; |
---|
141 | else if (val1 > val2) |
---|
142 | return 1; |
---|
143 | else |
---|
144 | return 0; |
---|
145 | } |
---|
146 | |
---|
147 | /* (non-Javadoc) |
---|
148 | * @see edu.stanford.rt.datatype.DataType#toString(String) |
---|
149 | */ |
---|
150 | public String toString(String indent) |
---|
151 | { |
---|
152 | String thisIndent = indent + " "; |
---|
153 | StringBuffer sb = new StringBuffer(); |
---|
154 | sb.append(thisIndent).append("IntegerType: ").append( |
---|
155 | getName()); |
---|
156 | |
---|
157 | if (includeMin) |
---|
158 | sb.append(" ["); |
---|
159 | else |
---|
160 | sb.append(" ("); |
---|
161 | sb.append(min); |
---|
162 | sb.append(", "); |
---|
163 | sb.append(max); |
---|
164 | |
---|
165 | if (includeMax) |
---|
166 | sb.append("]"); |
---|
167 | else |
---|
168 | sb.append(") "); |
---|
169 | if (base != 0) |
---|
170 | sb.append("base=" + base); |
---|
171 | if (step != 1) |
---|
172 | sb.append(", step=" + step); |
---|
173 | sb.append("\n"); |
---|
174 | |
---|
175 | return sb.toString(); |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | } |
---|