-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ArrayOperators.java
1996 lines (1657 loc) · 53.9 KB
/
ArrayOperators.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.aggregation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.bson.Document;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Gateway to {@literal array} aggregation operations.
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Shashank Sharma
* @author Divya Srivastava
* @since 1.0
*/
public class ArrayOperators {
/**
* Take the array referenced by given {@literal fieldReference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link ArrayOperatorFactory}.
*/
public static ArrayOperatorFactory arrayOf(String fieldReference) {
return new ArrayOperatorFactory(fieldReference);
}
/**
* Take the array referenced resulting from the given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ArrayOperatorFactory}.
*/
public static ArrayOperatorFactory arrayOf(AggregationExpression expression) {
return new ArrayOperatorFactory(expression);
}
/**
* Take the given {@link Collection values} {@link AggregationExpression}.
*
* @param values must not be {@literal null}.
* @return new instance of {@link ArrayOperatorFactory}.
* @since 2.2
*/
public static ArrayOperatorFactory arrayOf(Collection<?> values) {
return new ArrayOperatorFactory(values);
}
/**
* @author Christoph Strobl
*/
public static class ArrayOperatorFactory {
private final @Nullable String fieldReference;
private final @Nullable AggregationExpression expression;
private final @Nullable Collection values;
/**
* Creates new {@link ArrayOperatorFactory} for given {@literal fieldReference}.
*
* @param fieldReference must not be {@literal null}.
*/
public ArrayOperatorFactory(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
this.fieldReference = fieldReference;
this.expression = null;
this.values = null;
}
/**
* Creates new {@link ArrayOperatorFactory} for given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
*/
public ArrayOperatorFactory(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
this.fieldReference = null;
this.expression = expression;
this.values = null;
}
/**
* Creates new {@link ArrayOperatorFactory} for given values.
*
* @param values must not be {@literal null}.
* @since 2.2
*/
public ArrayOperatorFactory(Collection<?> values) {
Assert.notNull(values, "Values must not be null");
this.fieldReference = null;
this.expression = null;
this.values = values;
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and returns the element at the
* specified array {@literal position}.
*
* @param position the element index.
* @return new instance of {@link ArrayElemAt}.
*/
public ArrayElemAt elementAt(int position) {
return createArrayElemAt().elementAt(position);
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and returns the element at the position
* resulting form the given {@literal expression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ArrayElemAt}.
*/
public ArrayElemAt elementAt(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return createArrayElemAt().elementAt(expression);
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and returns the element at the position
* defined by the referenced {@literal field}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link ArrayElemAt}.
*/
public ArrayElemAt elementAt(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return createArrayElemAt().elementAt(fieldReference);
}
private ArrayElemAt createArrayElemAt() {
if (usesFieldRef()) {
return ArrayElemAt.arrayOf(fieldReference);
}
return usesExpression() ? ArrayElemAt.arrayOf(expression) : ArrayElemAt.arrayOf(values);
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and concats the given
* {@literal arrayFieldReference} to it.
*
* @param arrayFieldReference must not be {@literal null}.
* @return new instance of {@link ConcatArrays}.
*/
public ConcatArrays concat(String arrayFieldReference) {
Assert.notNull(arrayFieldReference, "ArrayFieldReference must not be null");
return createConcatArrays().concat(arrayFieldReference);
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and concats the array resulting form
* the given {@literal expression} to it.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ConcatArrays}.
*/
public ConcatArrays concat(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return createConcatArrays().concat(expression);
}
private ConcatArrays createConcatArrays() {
if (usesFieldRef()) {
return ConcatArrays.arrayOf(fieldReference);
}
return usesExpression() ? ConcatArrays.arrayOf(expression) : ConcatArrays.arrayOf(values);
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and selects a subset of the array to
* return based on the specified condition.
*
* @return new instance of {@link AsBuilder} to create a {@link Filter}.
*/
public AsBuilder filter() {
if (usesFieldRef()) {
return Filter.filter(fieldReference);
}
Assert.state(values != null, "Values must not be null");
return Filter.filter(new ArrayList<>(values));
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and an check if its an array.
*
* @return new instance of {@link IsArray}.
*/
public IsArray isArray() {
Assert.state(values == null, "Does it make sense to call isArray on an array; Maybe just skip it");
return usesFieldRef() ? IsArray.isArray(fieldReference) : IsArray.isArray(expression);
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and retrieves its length.
*
* @return new instance of {@link Size}.
*/
public Size length() {
if (usesFieldRef()) {
return Size.lengthOfArray(fieldReference);
}
return usesExpression() ? Size.lengthOfArray(expression) : Size.lengthOfArray(values);
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and selects a subset from it.
*
* @return new instance of {@link Slice}.
*/
public Slice slice() {
if (usesFieldRef()) {
return Slice.sliceArrayOf(fieldReference);
}
return usesExpression() ? Slice.sliceArrayOf(expression) : Slice.sliceArrayOf(values);
}
/**
* Creates new {@link AggregationExpression} that searches the associated array for an occurrence of a specified
* value and returns the array index (zero-based) of the first occurrence.
*
* @param value must not be {@literal null}.
* @return new instance of {@link IndexOfArray}.
*/
public IndexOfArray indexOf(Object value) {
if (usesFieldRef()) {
return IndexOfArray.arrayOf(fieldReference).indexOf(value);
}
return usesExpression() ? IndexOfArray.arrayOf(expression).indexOf(value)
: IndexOfArray.arrayOf(values).indexOf(value);
}
/**
* Creates new {@link AggregationExpression} that returns an array with the elements in reverse order.
*
* @return new instance of {@link ReverseArray}.
*/
public ReverseArray reverse() {
if (usesFieldRef()) {
return ReverseArray.reverseArrayOf(fieldReference);
}
return usesExpression() ? ReverseArray.reverseArrayOf(expression)
: ReverseArray.reverseArrayOf(Collections.singletonList(values));
}
/**
* Start creating new {@link AggregationExpression} that applies an {@link AggregationExpression} to each element in
* an array and combines them into a single value.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ReduceInitialValueBuilder} to create {@link Reduce}.
*/
public ArrayOperatorFactory.ReduceInitialValueBuilder reduce(AggregationExpression expression) {
return initialValue -> (usesFieldRef() ? Reduce.arrayOf(fieldReference)
: Reduce.arrayOf(ArrayOperatorFactory.this.expression)).withInitialValue(initialValue).reduce(expression);
}
/**
* Start creating new {@link AggregationExpression} that applies an {@link AggregationExpression} to each element in
* an array and combines them into a single value.
*
* @param expressions must not be {@literal null}.
* @return new instance of {@link ReduceInitialValueBuilder} to create {@link Reduce}.
*/
public ArrayOperatorFactory.ReduceInitialValueBuilder reduce(PropertyExpression... expressions) {
return initialValue -> (usesFieldRef() ? Reduce.arrayOf(fieldReference) : Reduce.arrayOf(expression))
.withInitialValue(initialValue).reduce(expressions);
}
/**
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort order}.
*
* @return new instance of {@link SortArray}.
* @since 4.0
*/
public SortArray sort(Sort sort) {
if (usesFieldRef()) {
return SortArray.sortArrayOf(fieldReference).by(sort);
}
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort);
}
/**
* Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
* the output array would be an array containing, the first element of the first input array, the first element of
* the second input array, etc.
*
* @param arrays must not be {@literal null}.
* @return new instance of {@link Zip}.
*/
public Zip zipWith(Object... arrays) {
if (usesFieldRef()) {
return Zip.arrayOf(fieldReference).zip(arrays);
}
return (usesExpression() ? Zip.arrayOf(expression) : Zip.arrayOf(values)).zip(arrays);
}
/**
* Creates new {@link AggregationExpression} that returns a boolean indicating whether a specified value is in the
* associated array.
*
* @param value must not be {@literal null}.
* @return new instance of {@link In}.
*/
public In containsValue(Object value) {
if (usesFieldRef()) {
return In.arrayOf(fieldReference).containsValue(value);
}
return (usesExpression() ? In.arrayOf(expression) : In.arrayOf(values)).containsValue(value);
}
/**
* Creates new {@link AggregationExpression} that converts the associated expression into an object.
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @return new instance of {@link ArrayToObject}.
* @since 2.1
*/
public ArrayToObject toObject() {
if (usesFieldRef()) {
return ArrayToObject.arrayValueOfToObject(fieldReference);
}
return usesExpression() ? ArrayToObject.arrayValueOfToObject(expression) : ArrayToObject.arrayToObject(values);
}
/**
* Creates new {@link AggregationExpression} that return the first element in the associated array.
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
*
* @return new instance of {@link First}.
* @since 3.4
*/
public First first() {
if (usesFieldRef()) {
return First.firstOf(fieldReference);
}
return usesExpression() ? First.firstOf(expression) : First.first(values);
}
/**
* Creates new {@link AggregationExpression} that return the last element in the given array.
* <strong>NOTE:</strong> Requires MongoDB 4.4 or later.
*
* @return new instance of {@link Last}.
* @since 3.4
*/
public Last last() {
if (usesFieldRef()) {
return Last.lastOf(fieldReference);
}
return usesExpression() ? Last.lastOf(expression) : Last.last(values);
}
/**
* @author Christoph Strobl
*/
public interface ReduceInitialValueBuilder {
/**
* Define the initial cumulative value set before in is applied to the first element of the input array.
*
* @param initialValue must not be {@literal null}.
* @return
*/
Reduce startingWith(Object initialValue);
}
/**
* @return {@literal true} if {@link #fieldReference} is not {@literal null}.
*/
private boolean usesFieldRef() {
return fieldReference != null;
}
/**
* @return {@literal true} if {@link #expression} is not {@literal null}.
* @since 2.2
*/
private boolean usesExpression() {
return expression != null;
}
}
/**
* {@link AggregationExpression} for {@code $arrayElementAt}.
*
* @author Christoph Strobl
*/
public static class ArrayElemAt extends AbstractAggregationExpression {
private ArrayElemAt(List<?> value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$arrayElemAt";
}
/**
* Creates new {@link ArrayElemAt}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link ArrayElemAt}.
*/
public static ArrayElemAt arrayOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new ArrayElemAt(asFields(fieldReference));
}
/**
* Creates new {@link ArrayElemAt}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ArrayElemAt}.
*/
public static ArrayElemAt arrayOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new ArrayElemAt(Collections.singletonList(expression));
}
/**
* Creates new {@link ArrayElemAt}.
*
* @param values The array members. Must not be {@literal null}.
* @return new instance of {@link ArrayElemAt}.
* @since 2.2
*/
public static ArrayElemAt arrayOf(Collection<?> values) {
Assert.notNull(values, "Values must not be null");
return new ArrayElemAt(Collections.singletonList(values));
}
/**
* Use the element with given index number.
*
* @param index the index number
* @return new instance of {@link ArrayElemAt}.
*/
public ArrayElemAt elementAt(int index) {
return new ArrayElemAt(append(index));
}
/**
* Use the element at the index number evaluated from the given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ArrayElemAt}.
*/
public ArrayElemAt elementAt(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new ArrayElemAt(append(expression));
}
/**
* Use the element at the index number traken from the given field.
*
* @param arrayFieldReference the field name.
* @return new instance of {@link ArrayElemAt}.
*/
public ArrayElemAt elementAt(String arrayFieldReference) {
Assert.notNull(arrayFieldReference, "ArrayReference must not be null");
return new ArrayElemAt(append(Fields.field(arrayFieldReference)));
}
}
/**
* {@link AggregationExpression} for {@code $concatArrays}.
*
* @author Christoph Strobl
*/
public static class ConcatArrays extends AbstractAggregationExpression {
private ConcatArrays(List<?> value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$concatArrays";
}
/**
* Creates new {@link ConcatArrays}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link ConcatArrays}.
*/
public static ConcatArrays arrayOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new ConcatArrays(asFields(fieldReference));
}
/**
* Creates new {@link ConcatArrays}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ConcatArrays}.
*/
public static ConcatArrays arrayOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new ConcatArrays(Collections.singletonList(expression));
}
/**
* Creates new {@link ConcatArrays}.
*
* @param values The array members. Must not be {@literal null}.
* @return new instance of {@link ConcatArrays}.
* @since 2.2
*/
public static ConcatArrays arrayOf(Collection<?> values) {
Assert.notNull(values, "Values must not be null");
return new ConcatArrays(Collections.singletonList(values));
}
/**
* Concat with the array stored at the given field.
*
* @param arrayFieldReference must not be {@literal null}.
* @return new instance of {@link ConcatArrays}.
*/
public ConcatArrays concat(String arrayFieldReference) {
Assert.notNull(arrayFieldReference, "ArrayFieldReference must not be null");
return new ConcatArrays(append(Fields.field(arrayFieldReference)));
}
/**
* Concat with the array resulting from the given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link ConcatArrays}.
*/
public ConcatArrays concat(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new ConcatArrays(append(expression));
}
}
/**
* {@code $filter} {@link AggregationExpression} allows to select a subset of the array to return based on the
* specified condition.
*
* @author Christoph Strobl
* @since 1.10
*/
public static class Filter implements AggregationExpression {
private @Nullable Object input;
private @Nullable ExposedField as;
private @Nullable Object condition;
private Filter() {
// used by builder
}
/**
* Set the {@literal field} to apply the {@code $filter} to.
*
* @param field must not be {@literal null}.
* @return never {@literal null}.
*/
public static AsBuilder filter(String field) {
Assert.notNull(field, "Field must not be null");
return filter(Fields.field(field));
}
/**
* Set the {@literal field} to apply the {@code $filter} to.
*
* @param field must not be {@literal null}.
* @return never {@literal null}.
*/
public static AsBuilder filter(Field field) {
Assert.notNull(field, "Field must not be null");
return new FilterExpressionBuilder().filter(field);
}
/**
* Set the {@literal values} to apply the {@code $filter} to.
*
* @param values must not be {@literal null}.
* @return new instance of {@link AsBuilder} to create the {@link Filter}.
*/
public static AsBuilder filter(List<?> values) {
Assert.notNull(values, "Values must not be null");
return new FilterExpressionBuilder().filter(values);
}
@Override
public Document toDocument(final AggregationOperationContext context) {
return toFilter(ExposedFields.from(as), context);
}
private Document toFilter(ExposedFields exposedFields, AggregationOperationContext context) {
Document filterExpression = new Document();
InheritingExposedFieldsAggregationOperationContext operationContext = new InheritingExposedFieldsAggregationOperationContext(
exposedFields, context);
filterExpression.putAll(context.getMappedObject(new Document("input", getMappedInput(context))));
filterExpression.put("as", as.getTarget());
filterExpression.putAll(context.getMappedObject(new Document("cond", getMappedCondition(operationContext))));
return new Document("$filter", filterExpression);
}
private Object getMappedInput(AggregationOperationContext context) {
return input instanceof Field ? context.getReference((Field) input).toString() : input;
}
private Object getMappedCondition(AggregationOperationContext context) {
if (!(condition instanceof AggregationExpression)) {
return condition;
}
NestedDelegatingExpressionAggregationOperationContext nea = new NestedDelegatingExpressionAggregationOperationContext(
context, Collections.singleton(as));
return ((AggregationExpression) condition).toDocument(nea);
}
/**
* @author Christoph Strobl
*/
public interface InputBuilder {
/**
* Set the {@literal values} to apply the {@code $filter} to.
*
* @param array must not be {@literal null}.
* @return
*/
AsBuilder filter(List<?> array);
/**
* Set the {@literal field} holding an array to apply the {@code $filter} to.
*
* @param field must not be {@literal null}.
* @return
*/
AsBuilder filter(Field field);
}
/**
* @author Christoph Strobl
*/
public interface AsBuilder {
/**
* Set the {@literal variableName} for the elements in the input array.
*
* @param variableName must not be {@literal null}.
* @return never {@literal null}.
*/
ConditionBuilder as(String variableName);
}
/**
* @author Christoph Strobl
*/
public interface ConditionBuilder {
/**
* Set the {@link AggregationExpression} that determines whether to include the element in the resulting array.
*
* @param expression must not be {@literal null}.
* @return never {@literal null}.
*/
Filter by(AggregationExpression expression);
/**
* Set the {@literal expression} that determines whether to include the element in the resulting array.
*
* @param expression must not be {@literal null}.
* @return never {@literal null}.
*/
Filter by(String expression);
/**
* Set the {@literal expression} that determines whether to include the element in the resulting array.
*
* @param expression must not be {@literal null}.
* @return never {@literal null}.
*/
Filter by(Document expression);
}
/**
* @author Christoph Strobl
*/
static final class FilterExpressionBuilder implements InputBuilder, AsBuilder, ConditionBuilder {
private final Filter filter;
FilterExpressionBuilder() {
this.filter = new Filter();
}
/**
* Creates new {@link InputBuilder}.
*
* @return new instance of {@link FilterExpressionBuilder}.
*/
public static InputBuilder newBuilder() {
return new FilterExpressionBuilder();
}
@Override
public AsBuilder filter(List<?> array) {
Assert.notNull(array, "Array must not be null");
filter.input = new ArrayList<Object>(array);
return this;
}
@Override
public AsBuilder filter(Field field) {
Assert.notNull(field, "Field must not be null");
filter.input = field;
return this;
}
@Override
public ConditionBuilder as(String variableName) {
Assert.notNull(variableName, "Variable name must not be null");
filter.as = new ExposedField(variableName, true);
return this;
}
@Override
public Filter by(AggregationExpression condition) {
Assert.notNull(condition, "Condition must not be null");
filter.condition = condition;
return filter;
}
@Override
public Filter by(String expression) {
Assert.notNull(expression, "Expression must not be null");
filter.condition = expression;
return filter;
}
@Override
public Filter by(Document expression) {
Assert.notNull(expression, "Expression must not be null");
filter.condition = expression;
return filter;
}
}
}
/**
* {@link AggregationExpression} for {@code $isArray}.
*
* @author Christoph Strobl
*/
public static class IsArray extends AbstractAggregationExpression {
private IsArray(Object value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$isArray";
}
/**
* Creates new {@link IsArray}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link IsArray}.
*/
public static IsArray isArray(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new IsArray(Fields.field(fieldReference));
}
/**
* Creates new {@link IsArray}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link IsArray}.
*/
public static IsArray isArray(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new IsArray(expression);
}
}
/**
* {@link AggregationExpression} for {@code $size}.
*
* @author Christoph Strobl
*/
public static class Size extends AbstractAggregationExpression {
private Size(Object value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$size";
}
/**
* Creates new {@link Size}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link Size}.
*/
public static Size lengthOfArray(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new Size(Fields.field(fieldReference));
}
/**
* Creates new {@link Size}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Size}.
*/
public static Size lengthOfArray(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new Size(expression);
}
/**
* Creates new {@link Size}.
*
* @param values must not be {@literal null}.
* @return new instance of {@link Size}.
* @since 2.2
*/
public static Size lengthOfArray(Collection<?> values) {
Assert.notNull(values, "Values must not be null");
return new Size(Collections.singletonList(values));
}
}
/**
* {@link AggregationExpression} for {@code $slice}.
*
* @author Christoph Strobl
*/
public static class Slice extends AbstractAggregationExpression {
private Slice(List<?> value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$slice";
}
/**
* Creates new {@link Slice}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link Slice}.
*/
public static Slice sliceArrayOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new Slice(asFields(fieldReference));
}
/**
* Creates new {@link Slice}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Slice}.
*/
public static Slice sliceArrayOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new Slice(Collections.singletonList(expression));
}
/**
* Creates new {@link Slice}.
*
* @param values must not be {@literal null}.
* @return new instance of {@link Slice}.
* @since 2.2
*/
public static Slice sliceArrayOf(Collection<?> values) {
Assert.notNull(values, "Values must not be null");
return new Slice(Collections.singletonList(values));
}
/**
* Slice the number of elements.
*
* @param nrElements elements to slice.
* @return new instance of {@link Slice}.
*/
public Slice itemCount(int nrElements) {
return new Slice(append(nrElements));
}
/**
* Slice using offset and count.
*
* @param position the start position
* @return new instance of {@link SliceElementsBuilder} to create {@link Slice}.
*/
public SliceElementsBuilder offset(final int position) {
return new SliceElementsBuilder() {