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.builder.impl; |
21 | |
|
22 | |
import java.io.StringWriter; |
23 | |
import java.util.Date; |
24 | |
import java.util.List; |
25 | |
import java.util.UUID; |
26 | |
import java.util.regex.Pattern; |
27 | |
|
28 | |
import com.allanbank.mongodb.bson.DocumentAssignable; |
29 | |
import com.allanbank.mongodb.bson.Element; |
30 | |
import com.allanbank.mongodb.bson.ElementAssignable; |
31 | |
import com.allanbank.mongodb.bson.builder.ArrayBuilder; |
32 | |
import com.allanbank.mongodb.bson.builder.BuilderFactory; |
33 | |
import com.allanbank.mongodb.bson.builder.DocumentBuilder; |
34 | |
import com.allanbank.mongodb.bson.element.ArrayElement; |
35 | |
import com.allanbank.mongodb.bson.element.BinaryElement; |
36 | |
import com.allanbank.mongodb.bson.element.BooleanElement; |
37 | |
import com.allanbank.mongodb.bson.element.DocumentElement; |
38 | |
import com.allanbank.mongodb.bson.element.DoubleElement; |
39 | |
import com.allanbank.mongodb.bson.element.IntegerElement; |
40 | |
import com.allanbank.mongodb.bson.element.JavaScriptElement; |
41 | |
import com.allanbank.mongodb.bson.element.JavaScriptWithScopeElement; |
42 | |
import com.allanbank.mongodb.bson.element.JsonSerializationVisitor; |
43 | |
import com.allanbank.mongodb.bson.element.LongElement; |
44 | |
import com.allanbank.mongodb.bson.element.MaxKeyElement; |
45 | |
import com.allanbank.mongodb.bson.element.MinKeyElement; |
46 | |
import com.allanbank.mongodb.bson.element.MongoTimestampElement; |
47 | |
import com.allanbank.mongodb.bson.element.NullElement; |
48 | |
import com.allanbank.mongodb.bson.element.ObjectId; |
49 | |
import com.allanbank.mongodb.bson.element.ObjectIdElement; |
50 | |
import com.allanbank.mongodb.bson.element.RegularExpressionElement; |
51 | |
import com.allanbank.mongodb.bson.element.StringElement; |
52 | |
import com.allanbank.mongodb.bson.element.SymbolElement; |
53 | |
import com.allanbank.mongodb.bson.element.TimestampElement; |
54 | |
import com.allanbank.mongodb.bson.element.UuidElement; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 4217 | public class ArrayBuilderImpl extends AbstractBuilder implements ArrayBuilder { |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public ArrayBuilderImpl() { |
69 | 247 | this(null); |
70 | 247 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public ArrayBuilderImpl(final AbstractBuilder outerBuilder) { |
79 | 3252 | super(outerBuilder); |
80 | 3252 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
@Override |
86 | |
public ArrayBuilder add(final boolean value) { |
87 | 29 | return addBoolean(value); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@Override |
94 | |
public ArrayBuilder add(final byte subType, final byte[] data) |
95 | |
throws IllegalArgumentException { |
96 | 3 | return addBinary(subType, data); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
@Override |
103 | |
public ArrayBuilder add(final byte[] data) { |
104 | 19 | if (data == null) { |
105 | 1 | return addNull(); |
106 | |
} |
107 | 18 | return addBinary(data); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
@Override |
114 | |
public ArrayBuilder add(final Date timestamp) { |
115 | 33 | if (timestamp == null) { |
116 | 1 | return addNull(); |
117 | |
} |
118 | 32 | return addTimestamp(timestamp.getTime()); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
@Override |
125 | |
public ArrayBuilder add(final DocumentAssignable document) { |
126 | 200059 | if (document == null) { |
127 | 1 | return addNull(); |
128 | |
} |
129 | 200058 | return addDocument(document); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
@Override |
136 | |
public ArrayBuilder add(final DocumentElement document) { |
137 | 2 | if (document == null) { |
138 | 1 | return addNull(); |
139 | |
} |
140 | 1 | return addDocument(document); |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
@Override |
147 | |
public ArrayBuilder add(final double value) { |
148 | 721 | return addDouble(value); |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
@Override |
156 | |
public ArrayBuilder add(final ElementAssignable element) |
157 | |
throws IllegalArgumentException { |
158 | 22 | if (element == null) { |
159 | 1 | throw new IllegalArgumentException("Cannot add a null element."); |
160 | |
} |
161 | 21 | myElements.add(element.asElement().withName(nextIndex())); |
162 | 21 | return this; |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
@Override |
169 | |
public ArrayBuilder add(final int value) { |
170 | 82 | return addInteger(value); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
@Override |
177 | |
public ArrayBuilder add(final long value) { |
178 | 29 | return addLong(value); |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
@Override |
185 | |
public ArrayBuilder add(final Object value) throws IllegalArgumentException { |
186 | 168 | myElements.add(BuilderFactory.e(nextIndex(), value)); |
187 | 167 | return this; |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
@Override |
194 | |
public ArrayBuilder add(final ObjectId id) { |
195 | 20 | if (id == null) { |
196 | 1 | return addNull(); |
197 | |
} |
198 | 19 | return addObjectId(id); |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
@Override |
205 | |
public ArrayBuilder add(final Pattern pattern) { |
206 | 17 | if (pattern == null) { |
207 | 1 | return addNull(); |
208 | |
} |
209 | 16 | return addRegularExpression(pattern); |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
@Override |
216 | |
public ArrayBuilder add(final String value) { |
217 | 70 | if (value == null) { |
218 | 1 | return addNull(); |
219 | |
} |
220 | 69 | return addString(value); |
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
@Override |
227 | |
@Deprecated |
228 | |
public ArrayBuilder add(final String databaseName, |
229 | |
final String collectionName, final ObjectId id) |
230 | |
throws IllegalArgumentException { |
231 | 3 | return addDBPointer(databaseName, collectionName, id); |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
@Override |
238 | |
public ArrayBuilder add(final UUID uuid) throws IllegalArgumentException { |
239 | 14 | if (uuid == null) { |
240 | 1 | return addNull(); |
241 | |
} |
242 | 13 | return addUuid(uuid); |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
@Override |
249 | |
public ArrayBuilder addBinary(final byte subType, final byte[] value) |
250 | |
throws IllegalArgumentException { |
251 | 17 | myElements.add(new BinaryElement(nextIndex(), subType, value)); |
252 | 17 | return this; |
253 | |
} |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
@Override |
259 | |
public ArrayBuilder addBinary(final byte[] value) |
260 | |
throws IllegalArgumentException { |
261 | 26 | myElements.add(new BinaryElement(nextIndex(), value)); |
262 | 26 | return this; |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
@Override |
269 | |
public ArrayBuilder addBoolean(final boolean value) { |
270 | 97 | myElements.add(new BooleanElement(nextIndex(), value)); |
271 | 97 | return this; |
272 | |
} |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
@Override |
278 | |
@Deprecated |
279 | |
public ArrayBuilder addDBPointer(final String databaseName, |
280 | |
final String collectionName, final ObjectId id) |
281 | |
throws IllegalArgumentException { |
282 | 15 | myElements.add(new com.allanbank.mongodb.bson.element.DBPointerElement( |
283 | |
nextIndex(), databaseName, collectionName, id)); |
284 | 15 | return this; |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
@Override |
291 | |
public ArrayBuilder addDocument(final DocumentAssignable document) |
292 | |
throws IllegalArgumentException { |
293 | 200342 | myElements.add(new DocumentElement(nextIndex(), document.asDocument())); |
294 | 200342 | return this; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
@Override |
301 | |
public ArrayBuilder addDouble(final double value) { |
302 | 2762 | myElements.add(new DoubleElement(nextIndex(), value)); |
303 | 2762 | return this; |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
@Override |
310 | |
public ArrayBuilder addInteger(final int value) { |
311 | 222 | myElements.add(new IntegerElement(nextIndex(), value)); |
312 | 222 | return this; |
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
@Override |
319 | |
public ArrayBuilder addJavaScript(final String code) |
320 | |
throws IllegalArgumentException { |
321 | 22 | myElements.add(new JavaScriptElement(nextIndex(), code)); |
322 | 22 | return this; |
323 | |
} |
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
@Override |
329 | |
public ArrayBuilder addJavaScript(final String code, |
330 | |
final DocumentAssignable scope) throws IllegalArgumentException { |
331 | 11 | myElements.add(new JavaScriptWithScopeElement(nextIndex(), code, scope |
332 | |
.asDocument())); |
333 | 11 | return this; |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
@Override |
340 | |
public ArrayBuilder addLegacyUuid(final UUID uuid) |
341 | |
throws IllegalArgumentException { |
342 | 2 | myElements.add(new UuidElement(nextIndex(), |
343 | |
UuidElement.LEGACY_UUID_SUBTTYPE, uuid)); |
344 | 1 | return this; |
345 | |
} |
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
@Override |
351 | |
public ArrayBuilder addLong(final long value) { |
352 | 96 | myElements.add(new LongElement(nextIndex(), value)); |
353 | 96 | return this; |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
@Override |
360 | |
public ArrayBuilder addMaxKey() { |
361 | 23 | myElements.add(new MaxKeyElement(nextIndex())); |
362 | 23 | return this; |
363 | |
} |
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
@Override |
369 | |
public ArrayBuilder addMinKey() { |
370 | 22 | myElements.add(new MinKeyElement(nextIndex())); |
371 | 22 | return this; |
372 | |
} |
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
@Override |
378 | |
public ArrayBuilder addMongoTimestamp(final long value) { |
379 | 18 | myElements.add(new MongoTimestampElement(nextIndex(), value)); |
380 | 18 | return this; |
381 | |
} |
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
@Override |
387 | |
public ArrayBuilder addNull() { |
388 | 28 | myElements.add(new NullElement(nextIndex())); |
389 | 28 | return this; |
390 | |
} |
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
@Override |
396 | |
public ArrayBuilder addObjectId(final ObjectId id) |
397 | |
throws IllegalArgumentException { |
398 | 33 | myElements.add(new ObjectIdElement(nextIndex(), id)); |
399 | 33 | return this; |
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
@Override |
406 | |
public ArrayBuilder addRegularExpression(final Pattern pattern) |
407 | |
throws IllegalArgumentException { |
408 | 19 | myElements.add(new RegularExpressionElement(nextIndex(), pattern)); |
409 | 19 | return this; |
410 | |
} |
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
@Override |
416 | |
public ArrayBuilder addRegularExpression(final String pattern, |
417 | |
final String options) throws IllegalArgumentException { |
418 | 21 | myElements.add(new RegularExpressionElement(nextIndex(), pattern, |
419 | |
options)); |
420 | 21 | return this; |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
@Override |
427 | |
public ArrayBuilder addString(final String value) |
428 | |
throws IllegalArgumentException { |
429 | 160 | myElements.add(new StringElement(nextIndex(), value)); |
430 | 160 | return this; |
431 | |
} |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
@Override |
437 | |
public ArrayBuilder addSymbol(final String symbol) |
438 | |
throws IllegalArgumentException { |
439 | 35 | myElements.add(new SymbolElement(nextIndex(), symbol)); |
440 | 35 | return this; |
441 | |
} |
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
@Override |
447 | |
public ArrayBuilder addTimestamp(final long timestamp) { |
448 | 46 | myElements.add(new TimestampElement(nextIndex(), timestamp)); |
449 | 46 | return this; |
450 | |
} |
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
@Override |
456 | |
public ArrayBuilder addUuid(final UUID uuid) |
457 | |
throws IllegalArgumentException { |
458 | 15 | myElements.add(new UuidElement(nextIndex(), UuidElement.UUID_SUBTTYPE, |
459 | |
uuid)); |
460 | 14 | return this; |
461 | |
} |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
@Override |
470 | |
public Element[] build() { |
471 | 353 | final List<Element> elements = subElements(); |
472 | 353 | return elements.toArray(new Element[elements.size()]); |
473 | |
} |
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
@Override |
482 | |
public ArrayElement build(final String name) { |
483 | 4232 | return new ArrayElement(name, subElements()); |
484 | |
} |
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
@Override |
490 | |
public DocumentBuilder push() { |
491 | 700402 | return doPush(nextIndex()); |
492 | |
} |
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
@Override |
498 | |
public ArrayBuilder pushArray() { |
499 | 1413 | return doPushArray(nextIndex()); |
500 | |
} |
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
@Override |
506 | |
public ArrayBuilder reset() { |
507 | 127 | super.reset(); |
508 | 127 | return this; |
509 | |
} |
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
@Override |
519 | |
public String toString() { |
520 | 1 | final StringWriter writer = new StringWriter(); |
521 | 1 | final JsonSerializationVisitor visitor = new JsonSerializationVisitor( |
522 | |
writer, false); |
523 | |
|
524 | 1 | visitor.visitArray("elements", myElements); |
525 | |
|
526 | 1 | return writer.toString(); |
527 | |
} |
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
private String nextIndex() { |
535 | 906036 | return ArrayElement.nameFor(myElements.size()); |
536 | |
} |
537 | |
} |