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; |
21 | |
|
22 | |
import java.util.Collections; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 15 | public enum ElementType { |
36 | |
|
37 | |
|
38 | 1 | ARRAY((byte) 0x04), |
39 | |
|
40 | |
|
41 | 1 | BINARY((byte) 0x05), |
42 | |
|
43 | |
|
44 | 1 | BOOLEAN((byte) 0x08), |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 1 | @Deprecated |
52 | |
DB_POINTER((byte) 0x0C), |
53 | |
|
54 | |
|
55 | 1 | DOCUMENT((byte) 0x03), |
56 | |
|
57 | |
|
58 | 1 | DOUBLE((byte) 0x01), |
59 | |
|
60 | |
|
61 | 1 | INTEGER((byte) 0x10), |
62 | |
|
63 | |
|
64 | 1 | JAVA_SCRIPT((byte) 0x0D), |
65 | |
|
66 | |
|
67 | 1 | JAVA_SCRIPT_WITH_SCOPE((byte) 0x0F), |
68 | |
|
69 | |
|
70 | 1 | LONG((byte) 0x12), |
71 | |
|
72 | |
|
73 | 1 | MAX_KEY((byte) 0x7F), |
74 | |
|
75 | |
|
76 | 1 | MIN_KEY((byte) 0xFF), |
77 | |
|
78 | |
|
79 | 1 | MONGO_TIMESTAMP((byte) 0x11), |
80 | |
|
81 | |
|
82 | 1 | NULL((byte) 0x0A), |
83 | |
|
84 | |
|
85 | 1 | OBJECT_ID((byte) 0x07), |
86 | |
|
87 | |
|
88 | 1 | REGEX((byte) 0x0B), |
89 | |
|
90 | |
|
91 | 1 | STRING((byte) 0x02), |
92 | |
|
93 | |
|
94 | 1 | SYMBOL((byte) 0x0E), |
95 | |
|
96 | |
|
97 | 1 | UTC_TIMESTAMP((byte) 0x09); |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
private static final Map<ElementType, Integer> ourMongoDbOrdering; |
111 | |
|
112 | |
static { |
113 | 1 | final Map<ElementType, Integer> mongoDbOrdering = new HashMap<ElementType, Integer>( |
114 | |
(int) Math.ceil(values().length / 0.75)); |
115 | |
|
116 | 1 | int ordinal = 0; |
117 | |
|
118 | 1 | mongoDbOrdering.put(ElementType.MIN_KEY, Integer.valueOf(ordinal)); |
119 | 1 | ordinal += 1; |
120 | |
|
121 | 1 | mongoDbOrdering.put(ElementType.NULL, Integer.valueOf(ordinal)); |
122 | 1 | ordinal += 1; |
123 | |
|
124 | |
|
125 | 1 | mongoDbOrdering.put(ElementType.DOUBLE, Integer.valueOf(ordinal)); |
126 | 1 | mongoDbOrdering.put(ElementType.INTEGER, Integer.valueOf(ordinal)); |
127 | 1 | mongoDbOrdering.put(ElementType.LONG, Integer.valueOf(ordinal)); |
128 | 1 | ordinal += 1; |
129 | |
|
130 | |
|
131 | 1 | mongoDbOrdering.put(ElementType.SYMBOL, Integer.valueOf(ordinal)); |
132 | 1 | mongoDbOrdering.put(ElementType.STRING, Integer.valueOf(ordinal)); |
133 | 1 | ordinal += 1; |
134 | |
|
135 | 1 | mongoDbOrdering.put(ElementType.DOCUMENT, Integer.valueOf(ordinal)); |
136 | 1 | ordinal += 1; |
137 | 1 | mongoDbOrdering.put(ElementType.ARRAY, Integer.valueOf(ordinal)); |
138 | 1 | ordinal += 1; |
139 | 1 | mongoDbOrdering.put(ElementType.BINARY, Integer.valueOf(ordinal)); |
140 | 1 | ordinal += 1; |
141 | 1 | mongoDbOrdering.put(ElementType.OBJECT_ID, Integer.valueOf(ordinal)); |
142 | 1 | ordinal += 1; |
143 | 1 | mongoDbOrdering.put(ElementType.BOOLEAN, Integer.valueOf(ordinal)); |
144 | 1 | ordinal += 1; |
145 | |
|
146 | |
|
147 | 1 | mongoDbOrdering |
148 | |
.put(ElementType.UTC_TIMESTAMP, Integer.valueOf(ordinal)); |
149 | 1 | mongoDbOrdering.put(ElementType.MONGO_TIMESTAMP, |
150 | |
Integer.valueOf(ordinal)); |
151 | 1 | ordinal += 1; |
152 | |
|
153 | 1 | mongoDbOrdering.put(ElementType.REGEX, Integer.valueOf(ordinal)); |
154 | 1 | ordinal += 1; |
155 | 1 | mongoDbOrdering.put(ElementType.DB_POINTER, Integer.valueOf(ordinal)); |
156 | 1 | ordinal += 1; |
157 | 1 | mongoDbOrdering.put(ElementType.JAVA_SCRIPT, Integer.valueOf(ordinal)); |
158 | 1 | ordinal += 1; |
159 | 1 | mongoDbOrdering.put(ElementType.JAVA_SCRIPT_WITH_SCOPE, |
160 | |
Integer.valueOf(ordinal)); |
161 | 1 | ordinal += 1; |
162 | |
|
163 | 1 | mongoDbOrdering.put(ElementType.MAX_KEY, Integer.valueOf(ordinal)); |
164 | 1 | ordinal += 1; |
165 | |
|
166 | 1 | ourMongoDbOrdering = Collections.unmodifiableMap(mongoDbOrdering); |
167 | 1 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public static ElementType valueOf(final byte token) { |
179 | 68645 | switch (token) { |
180 | |
case 0x01: { |
181 | 10 | return DOUBLE; |
182 | |
} |
183 | |
case 0x02: { |
184 | 440 | return STRING; |
185 | |
} |
186 | |
case 0x03: { |
187 | 32129 | return DOCUMENT; |
188 | |
} |
189 | |
case 0x04: { |
190 | 120 | return ARRAY; |
191 | |
} |
192 | |
case 0x05: { |
193 | 32020 | return BINARY; |
194 | |
} |
195 | |
|
196 | |
case 0x07: { |
197 | 9 | return OBJECT_ID; |
198 | |
} |
199 | |
case 0x08: { |
200 | 155 | return BOOLEAN; |
201 | |
} |
202 | |
case 0x09: { |
203 | 13 | return UTC_TIMESTAMP; |
204 | |
} |
205 | |
case 0x0A: { |
206 | 9 | return NULL; |
207 | |
} |
208 | |
case 0x0B: { |
209 | 15 | return REGEX; |
210 | |
} |
211 | |
case 0x0C: { |
212 | 9 | return DB_POINTER; |
213 | |
} |
214 | |
case 0x0D: { |
215 | 9 | return JAVA_SCRIPT; |
216 | |
} |
217 | |
case 0x0E: { |
218 | 9 | return SYMBOL; |
219 | |
} |
220 | |
case 0x0F: { |
221 | 9 | return JAVA_SCRIPT_WITH_SCOPE; |
222 | |
} |
223 | |
case 0x10: { |
224 | 3650 | return INTEGER; |
225 | |
} |
226 | |
case 0x11: { |
227 | 9 | return MONGO_TIMESTAMP; |
228 | |
} |
229 | |
case 0x12: { |
230 | 11 | return LONG; |
231 | |
} |
232 | |
case 0x7F: { |
233 | 9 | return MAX_KEY; |
234 | |
} |
235 | |
case (byte) 0xFF: { |
236 | 9 | return MIN_KEY; |
237 | |
} |
238 | |
default: { |
239 | 20 | for (final ElementType type : values()) { |
240 | 19 | if (token == type.getToken()) { |
241 | 0 | return type; |
242 | |
} |
243 | |
} |
244 | |
|
245 | 1 | return null; |
246 | |
} |
247 | |
} |
248 | |
} |
249 | |
|
250 | |
|
251 | |
private final byte myToken; |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | 19 | private ElementType(final byte token) { |
260 | 19 | myToken = token; |
261 | 19 | } |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public int compare(final ElementType rhs) { |
274 | |
|
275 | 517 | final int lhsValue = ourMongoDbOrdering.get(this).intValue(); |
276 | 517 | final int rhsValue = ourMongoDbOrdering.get(rhs).intValue(); |
277 | |
|
278 | 517 | return lhsValue - rhsValue; |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public byte getToken() { |
287 | 68944 | return myToken; |
288 | |
} |
289 | |
|
290 | |
} |