18
18
19
19
import org .bson .BsonArray ;
20
20
import org .bson .BsonDocument ;
21
+ import org .bson .BsonInt32 ;
21
22
import org .bson .BsonString ;
22
23
import org .bson .BsonValue ;
23
24
import org .bson .codecs .configuration .CodecRegistry ;
@@ -385,7 +386,12 @@ public ArrayExpression<T> filter(final Function<? super T, ? extends BooleanExpr
385
386
.append ("cond" , extractBsonValue (cr , cond .apply (varThis )))));
386
387
}
387
388
388
- @ Override
389
+ public ArrayExpression <T > sort () {
390
+ return new MqlExpression <>((cr ) -> astDoc ("$sortArray" , new BsonDocument ()
391
+ .append ("input" , this .toBsonValue (cr ))
392
+ .append ("sortBy" , new BsonInt32 (1 ))));
393
+ }
394
+
389
395
public T reduce (final T initialValue , final BinaryOperator <T > in ) {
390
396
T varThis = variable ("$$this" );
391
397
T varValue = variable ("$$value" );
@@ -395,6 +401,77 @@ public T reduce(final T initialValue, final BinaryOperator<T> in) {
395
401
.append ("in" , extractBsonValue (cr , in .apply (varValue , varThis )))));
396
402
}
397
403
404
+ @ Override
405
+ public BooleanExpression any (final Function <? super T , BooleanExpression > predicate ) {
406
+ MqlExpression <BooleanExpression > array = (MqlExpression <BooleanExpression >) this .map (predicate );
407
+ return array .reduce (of (false ), (a , b ) -> a .or (b ));
408
+ }
409
+
410
+ @ Override
411
+ public BooleanExpression all (final Function <? super T , BooleanExpression > predicate ) {
412
+ MqlExpression <BooleanExpression > array = (MqlExpression <BooleanExpression >) this .map (predicate );
413
+ return array .reduce (of (true ), (a , b ) -> a .and (b ));
414
+ }
415
+
416
+ @ SuppressWarnings ("unchecked" )
417
+ @ Override
418
+ public NumberExpression sum (final Function <? super T , ? extends NumberExpression > mapper ) {
419
+ // no sum that returns IntegerExpression, both have same erasure
420
+ MqlExpression <NumberExpression > array = (MqlExpression <NumberExpression >) this .map (mapper );
421
+ return array .reduce (of (0 ), (a , b ) -> a .add (b ));
422
+ }
423
+
424
+ @ SuppressWarnings ("unchecked" )
425
+ @ Override
426
+ public NumberExpression multiply (final Function <? super T , ? extends NumberExpression > mapper ) {
427
+ MqlExpression <NumberExpression > array = (MqlExpression <NumberExpression >) this .map (mapper );
428
+ return array .reduce (of (0 ), (NumberExpression a , NumberExpression b ) -> a .multiply (b ));
429
+ }
430
+
431
+ @ Override
432
+ public T max (final T other ) {
433
+ return this .size ().eq (of (0 )).cond (other , this .maxN (of (1 )).first ());
434
+ }
435
+
436
+ @ Override
437
+ public T min (final T other ) {
438
+ return this .size ().eq (of (0 )).cond (other , this .minN (of (1 )).first ());
439
+ }
440
+
441
+ @ Override
442
+ public ArrayExpression <T > maxN (final IntegerExpression n ) {
443
+ return newMqlExpression ((CodecRegistry cr ) -> astDoc ("$maxN" , new BsonDocument ()
444
+ .append ("input" , extractBsonValue (cr , this ))
445
+ .append ("n" , extractBsonValue (cr , n ))));
446
+ }
447
+
448
+ @ Override
449
+ public ArrayExpression <T > minN (final IntegerExpression n ) {
450
+ return newMqlExpression ((CodecRegistry cr ) -> astDoc ("$minN" , new BsonDocument ()
451
+ .append ("input" , extractBsonValue (cr , this ))
452
+ .append ("n" , extractBsonValue (cr , n ))));
453
+ }
454
+
455
+ @ Override
456
+ public StringExpression join (final Function <? super T , StringExpression > mapper ) {
457
+ MqlExpression <StringExpression > array = (MqlExpression <StringExpression >) this .map (mapper );
458
+ return array .reduce (of ("" ), (a , b ) -> a .concat (b ));
459
+ }
460
+
461
+ @ SuppressWarnings ("unchecked" )
462
+ @ Override
463
+ public <R extends Expression > ArrayExpression <R > concat (final Function <? super T , ? extends ArrayExpression <? extends R >> mapper ) {
464
+ MqlExpression <ArrayExpression <R >> array = (MqlExpression <ArrayExpression <R >>) this .map (mapper );
465
+ return array .reduce (Expressions .ofArray (), (a , b ) -> a .concat (b ));
466
+ }
467
+
468
+ @ SuppressWarnings ("unchecked" )
469
+ @ Override
470
+ public <R extends Expression > ArrayExpression <R > union (final Function <? super T , ? extends ArrayExpression <? extends R >> mapper ) {
471
+ MqlExpression <ArrayExpression <R >> array = (MqlExpression <ArrayExpression <R >>) this .map (mapper );
472
+ return array .reduce (Expressions .ofArray (), (a , b ) -> a .union (b ));
473
+ }
474
+
398
475
@ Override
399
476
public IntegerExpression size () {
400
477
return new MqlExpression <>(astWrapped ("$size" ));
0 commit comments