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;
21
22 import java.math.BigInteger;
23 import java.util.Calendar;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.Map;
27 import java.util.UUID;
28 import java.util.regex.Pattern;
29
30 import com.allanbank.mongodb.bson.DocumentAssignable;
31 import com.allanbank.mongodb.bson.Element;
32 import com.allanbank.mongodb.bson.ElementAssignable;
33 import com.allanbank.mongodb.bson.builder.impl.ArrayBuilderImpl;
34 import com.allanbank.mongodb.bson.builder.impl.DocumentBuilderImpl;
35 import com.allanbank.mongodb.bson.element.ArrayElement;
36 import com.allanbank.mongodb.bson.element.BinaryElement;
37 import com.allanbank.mongodb.bson.element.BooleanElement;
38 import com.allanbank.mongodb.bson.element.DocumentElement;
39 import com.allanbank.mongodb.bson.element.DoubleElement;
40 import com.allanbank.mongodb.bson.element.IntegerElement;
41 import com.allanbank.mongodb.bson.element.LongElement;
42 import com.allanbank.mongodb.bson.element.NullElement;
43 import com.allanbank.mongodb.bson.element.ObjectId;
44 import com.allanbank.mongodb.bson.element.ObjectIdElement;
45 import com.allanbank.mongodb.bson.element.RegularExpressionElement;
46 import com.allanbank.mongodb.bson.element.StringElement;
47 import com.allanbank.mongodb.bson.element.TimestampElement;
48 import com.allanbank.mongodb.bson.element.UuidElement;
49 import com.allanbank.mongodb.bson.impl.RootDocument;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public class BuilderFactory {
126
127
128
129
130
131
132
133
134 public static final ArrayElement a() throws IllegalArgumentException {
135 return new ArrayElement("");
136 }
137
138
139
140
141
142
143
144
145
146
147 public static final ArrayElement a(final Boolean... values)
148 throws IllegalArgumentException {
149 final ArrayBuilder subArray = BuilderFactory.startArray();
150 for (final Boolean entry : values) {
151 if (entry != null) {
152 subArray.add(entry.booleanValue());
153 }
154 else {
155 subArray.addNull();
156 }
157 }
158 return new ArrayElement("", subArray.build());
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public static final ArrayElement a(final byte[]... datas)
176 throws IllegalArgumentException {
177 final ArrayBuilder subArray = BuilderFactory.startArray();
178 for (final Object entry : datas) {
179 subArray.add(entry);
180 }
181 return new ArrayElement("", subArray.build());
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public static final ArrayElement a(final Date... timestamps)
199 throws IllegalArgumentException {
200 final ArrayBuilder subArray = BuilderFactory.startArray();
201 for (final Object entry : timestamps) {
202 subArray.add(entry);
203 }
204 return new ArrayElement("", subArray.build());
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 public static final ArrayElement a(final DocumentAssignable... documents)
221 throws IllegalArgumentException {
222 final ArrayBuilder subArray = BuilderFactory.startArray();
223 for (final Object entry : documents) {
224 subArray.add(entry);
225 }
226 return new ArrayElement("", subArray.build());
227 }
228
229
230
231
232
233
234
235
236
237
238 public static final ArrayElement a(final Double... values)
239 throws IllegalArgumentException {
240 final ArrayBuilder subArray = BuilderFactory.startArray();
241 for (final Double entry : values) {
242 if (entry != null) {
243 subArray.add(entry.doubleValue());
244 }
245 else {
246 subArray.addNull();
247 }
248 }
249 return new ArrayElement("", subArray.build());
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 public static final ArrayElement a(final ElementAssignable... elements)
266 throws IllegalArgumentException {
267 final ArrayBuilder subArray = BuilderFactory.startArray();
268 for (final Object entry : elements) {
269 subArray.add(entry);
270 }
271 return new ArrayElement("", subArray.build());
272 }
273
274
275
276
277
278
279
280
281
282
283 public static final ArrayElement a(final Integer... values)
284 throws IllegalArgumentException {
285 final ArrayBuilder subArray = BuilderFactory.startArray();
286 for (final Integer entry : values) {
287 if (entry != null) {
288 subArray.add(entry.intValue());
289 }
290 else {
291 subArray.addNull();
292 }
293 }
294 return new ArrayElement("", subArray.build());
295 }
296
297
298
299
300
301
302
303
304
305
306 public static final ArrayElement a(final Long... values)
307 throws IllegalArgumentException {
308 final ArrayBuilder subArray = BuilderFactory.startArray();
309 for (final Long entry : values) {
310 if (entry != null) {
311 subArray.add(entry.longValue());
312 }
313 else {
314 subArray.addNull();
315 }
316 }
317 return new ArrayElement("", subArray.build());
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 public static final ArrayElement a(final Object... values) {
334 final ArrayBuilder subArray = BuilderFactory.startArray();
335 for (final Object entry : values) {
336 subArray.add(entry);
337 }
338 return new ArrayElement("", subArray.build());
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353 public static final ArrayElement a(final ObjectId... ids)
354 throws IllegalArgumentException {
355 final ArrayBuilder subArray = BuilderFactory.startArray();
356 for (final Object entry : ids) {
357 subArray.add(entry);
358 }
359 return new ArrayElement("", subArray.build());
360 }
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375 public static final ArrayElement a(final Pattern... patterns)
376 throws IllegalArgumentException {
377 final ArrayBuilder subArray = BuilderFactory.startArray();
378 for (final Object entry : patterns) {
379 subArray.add(entry);
380 }
381 return new ArrayElement("", subArray.build());
382 }
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397 public static final ArrayElement a(final String... values)
398 throws IllegalArgumentException {
399 final ArrayBuilder subArray = BuilderFactory.startArray();
400 for (final Object entry : values) {
401 subArray.add(entry);
402 }
403 return new ArrayElement("", subArray.build());
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419 public static final ArrayElement a(final UUID... uuids)
420 throws IllegalArgumentException {
421 final ArrayBuilder subArray = BuilderFactory.startArray();
422 for (final Object entry : uuids) {
423 subArray.add(entry);
424 }
425 return new ArrayElement("", subArray.build());
426 }
427
428
429
430
431
432
433
434
435
436 public static final DocumentBuilder d(final Element... elements) {
437 return new DocumentBuilderImpl(new RootDocument(elements));
438 }
439
440
441
442
443
444
445
446
447
448
449
450
451 public static final BooleanElement e(final String name, final boolean value)
452 throws IllegalArgumentException {
453 return new BooleanElement(name, value);
454 }
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471 public static final Element e(final String name, final byte[] data)
472 throws IllegalArgumentException {
473 if (data != null) {
474 return new BinaryElement(name, data);
475 }
476 return new NullElement(name);
477 }
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495 public static final Element e(final String name, final Date timestamp)
496 throws IllegalArgumentException {
497 if (timestamp != null) {
498 return new TimestampElement(name, timestamp.getTime());
499 }
500 return new NullElement(name);
501 }
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518 public static final Element e(final String name,
519 final DocumentAssignable document) throws IllegalArgumentException {
520 if (document != null) {
521 return new DocumentElement(name, document.asDocument());
522 }
523 return new NullElement(name);
524 }
525
526
527
528
529
530
531
532
533
534
535
536
537 public static final DoubleElement e(final String name, final double value)
538 throws IllegalArgumentException {
539 return new DoubleElement(name, value);
540 }
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557 public static final Element e(final String name,
558 final ElementAssignable element) throws IllegalArgumentException {
559 if (element != null) {
560 return element.asElement().withName(name);
561 }
562 return new NullElement(name);
563 }
564
565
566
567
568
569
570
571
572
573
574
575
576 public static final IntegerElement e(final String name, final int value)
577 throws IllegalArgumentException {
578 return new IntegerElement(name, value);
579 }
580
581
582
583
584
585
586
587
588
589
590
591
592 public static final LongElement e(final String name, final long value)
593 throws IllegalArgumentException {
594 return new LongElement(name, value);
595 }
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615 public static final Element e(final String name, final Object value) {
616 if (value == null) {
617 return new NullElement(name);
618 }
619 else if (value instanceof Boolean) {
620 return new BooleanElement(name, ((Boolean) value).booleanValue());
621 }
622 else if ((value instanceof Long) || (value instanceof BigInteger)) {
623 return new LongElement(name, ((Number) value).longValue());
624 }
625 else if ((value instanceof Double) || (value instanceof Float)) {
626 return new DoubleElement(name, ((Number) value).doubleValue());
627 }
628 else if (value instanceof Number) {
629 return new IntegerElement(name, ((Number) value).intValue());
630 }
631 else if (value instanceof byte[]) {
632 return new BinaryElement(name, (byte[]) value);
633 }
634 else if (value instanceof ObjectId) {
635 return new ObjectIdElement(name, (ObjectId) value);
636 }
637 else if (value instanceof Pattern) {
638 return new RegularExpressionElement(name, (Pattern) value);
639 }
640 else if (value instanceof String) {
641 return new StringElement(name, (String) value);
642 }
643 else if (value instanceof Date) {
644 return new TimestampElement(name, ((Date) value).getTime());
645 }
646 else if (value instanceof Calendar) {
647 return new TimestampElement(name, ((Calendar) value).getTime()
648 .getTime());
649 }
650 else if (value instanceof UUID) {
651 return new UuidElement(name, (UUID) value);
652 }
653 else if (value instanceof DocumentAssignable) {
654 return new DocumentElement(name,
655 ((DocumentAssignable) value).asDocument());
656 }
657 else if (value instanceof ElementAssignable) {
658 return ((ElementAssignable) value).asElement().withName(name);
659 }
660 else if (value instanceof Map) {
661 final DocumentBuilder subDoc = BuilderFactory.start();
662 for (final Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
663 subDoc.add(entry.getKey().toString(), entry.getValue());
664 }
665 return new DocumentElement(name, subDoc.build());
666 }
667 else if (value instanceof Collection) {
668 final ArrayBuilder subArray = BuilderFactory.startArray();
669 for (final Object entry : (Collection<?>) value) {
670 subArray.add(entry);
671 }
672 return new ArrayElement(name, subArray.build());
673 }
674 else if (value instanceof Object[]) {
675 final ArrayBuilder subArray = BuilderFactory.startArray();
676 for (final Object entry : (Object[]) value) {
677 subArray.add(entry);
678 }
679 return new ArrayElement(name, subArray.build());
680 }
681
682 throw new IllegalArgumentException("Could not coerce the type '"
683 + value.getClass().getName()
684 + "' into a valid BSON element type.");
685 }
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701 public static final Element e(final String name, final ObjectId id)
702 throws IllegalArgumentException {
703 if (id != null) {
704 return new ObjectIdElement(name, id);
705 }
706 return new NullElement(name);
707 }
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724 public static final Element e(final String name, final Pattern pattern)
725 throws IllegalArgumentException {
726 if (pattern != null) {
727 return new RegularExpressionElement(name, pattern);
728 }
729 return new NullElement(name);
730 }
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747 public static final Element e(final String name, final String value)
748 throws IllegalArgumentException {
749 if (value != null) {
750 return new StringElement(name, value);
751 }
752 return new NullElement(name);
753 }
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770 public static final Element e(final String name, final UUID uuid)
771 throws IllegalArgumentException {
772 if (uuid != null) {
773 return new UuidElement(name, UuidElement.UUID_SUBTTYPE, uuid);
774 }
775 return new NullElement(name);
776 }
777
778
779
780
781
782
783 public static final DocumentBuilder start() {
784 return new DocumentBuilderImpl();
785 }
786
787
788
789
790
791
792
793
794
795
796
797 public static final DocumentBuilder start(
798 final DocumentAssignable seedDocument) {
799 return new DocumentBuilderImpl(seedDocument);
800 }
801
802
803
804
805
806
807
808
809
810
811
812 public static final DocumentBuilder start(
813 final DocumentAssignable... seedDocuments) {
814 final DocumentBuilderImpl builder = new DocumentBuilderImpl();
815 for (final DocumentAssignable seedDocument : seedDocuments) {
816 for (final Element element : seedDocument.asDocument()) {
817 builder.remove(element.getName());
818 builder.add(element);
819 }
820 }
821 return builder;
822 }
823
824
825
826
827
828
829 public static final ArrayBuilder startArray() {
830 return new ArrayBuilderImpl();
831 }
832
833
834
835
836 private BuilderFactory() {
837
838 }
839 }