1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.allanbank.mongodb.bson.element;
21
22 import com.allanbank.mongodb.bson.Element;
23 import com.allanbank.mongodb.bson.ElementType;
24 import com.allanbank.mongodb.bson.NumericElement;
25 import com.allanbank.mongodb.bson.Visitor;
26 import com.allanbank.mongodb.bson.io.StringEncoder;
27
28
29
30
31
32
33
34
35
36
37 public class IntegerElement extends AbstractElement implements NumericElement {
38
39
40 public static final ElementType TYPE = ElementType.INTEGER;
41
42
43 private static final long serialVersionUID = 3738845320555958508L;
44
45
46
47
48
49
50
51
52
53 private static long computeSize(final String name) {
54 long result = 6;
55 result += StringEncoder.utf8Size(name);
56
57 return result;
58 }
59
60
61 private final int myValue;
62
63
64
65
66
67
68
69
70
71
72
73 public IntegerElement(final String name, final int value) {
74 this(name, value, computeSize(name));
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public IntegerElement(final String name, final int value, final long size) {
93 super(name, size);
94
95 myValue = value;
96 }
97
98
99
100
101
102
103 @Override
104 public void accept(final Visitor visitor) {
105 visitor.visitInteger(getName(), getValue());
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120 @Override
121 public int compareTo(final Element otherElement) {
122 int result = super.compareTo(otherElement);
123
124 if (result == 0) {
125
126 final NumericElement other = (NumericElement) otherElement;
127 final ElementType otherType = other.getType();
128
129
130 result = compare(getIntValue(), other.getIntValue());
131 if (otherType == ElementType.INTEGER) {
132 result = compare(getIntValue(), other.getIntValue());
133 }
134 else if (otherType == ElementType.LONG) {
135 result = compare(getLongValue(), other.getLongValue());
136 }
137 else {
138 result = Double.compare(getDoubleValue(),
139 other.getDoubleValue());
140
141 }
142 }
143
144 return result;
145 }
146
147
148
149
150
151
152
153
154
155
156 @Override
157 public boolean equals(final Object object) {
158 boolean result = false;
159 if (this == object) {
160 result = true;
161 }
162 else if ((object != null) && (getClass() == object.getClass())) {
163 final IntegerElement other = (IntegerElement) object;
164
165 result = super.equals(object) && (myValue == other.myValue);
166 }
167 return result;
168 }
169
170
171
172
173
174
175
176 @Override
177 public double getDoubleValue() {
178 return myValue;
179 }
180
181
182
183
184
185
186
187 @Override
188 public int getIntValue() {
189 return myValue;
190 }
191
192
193
194
195
196
197
198 @Override
199 public long getLongValue() {
200 return myValue;
201 }
202
203
204
205
206 @Override
207 public ElementType getType() {
208 return TYPE;
209 }
210
211
212
213
214
215
216 public int getValue() {
217 return myValue;
218 }
219
220
221
222
223
224
225
226 @Override
227 public Integer getValueAsObject() {
228 return Integer.valueOf(myValue);
229 }
230
231
232
233
234
235
236
237 @Override
238 public String getValueAsString() {
239 return Integer.toString(myValue);
240 }
241
242
243
244
245
246
247 @Override
248 public int hashCode() {
249 int result = 1;
250 result = (31 * result) + super.hashCode();
251 result = (31 * result) + myValue;
252 return result;
253 }
254
255
256
257
258
259
260
261 @Override
262 public IntegerElement withName(final String name) {
263 if (getName().equals(name)) {
264 return this;
265 }
266 return new IntegerElement(name, myValue);
267 }
268 }