17
17
18
18
import java .util .ArrayList ;
19
19
import java .util .Arrays ;
20
+ import java .util .Collection ;
20
21
import java .util .Collections ;
21
22
import java .util .List ;
22
23
@@ -282,19 +283,29 @@ public Document toDocument(AggregationOperationContext context) {
282
283
283
284
List <Object > list = new ArrayList <Object >();
284
285
285
- if (condition instanceof Field ) {
286
- list . add ( context . getReference (( Field ) condition ). toString ());
287
- } else if ( condition instanceof AggregationExpression ) {
288
- list . add ((( AggregationExpression ) condition ). toDocument ( context ));
286
+ if (condition instanceof Collection ) {
287
+ for ( Object val : (( Collection ) this . condition )) {
288
+ list . add ( mapCondition ( val , context ));
289
+ }
289
290
} else {
290
- list .add (condition );
291
+ list .add (mapCondition ( condition , context ) );
291
292
}
292
293
293
294
list .add (resolve (value , context ));
294
-
295
295
return new Document ("$ifNull" , list );
296
296
}
297
297
298
+ private Object mapCondition (Object condition , AggregationOperationContext context ) {
299
+
300
+ if (condition instanceof Field ) {
301
+ return context .getReference ((Field ) condition ).toString ();
302
+ } else if (condition instanceof AggregationExpression ) {
303
+ return ((AggregationExpression ) condition ).toDocument (context );
304
+ } else {
305
+ return condition ;
306
+ }
307
+ }
308
+
298
309
private Object resolve (Object value , AggregationOperationContext context ) {
299
310
300
311
if (value instanceof Field ) {
@@ -323,15 +334,34 @@ public interface IfNullBuilder {
323
334
/**
324
335
* @param expression the expression to check for a {@literal null} value, field name must not be {@literal null}
325
336
* or empty.
326
- * @return the {@link ThenBuilder}
337
+ * @return the {@link ThenBuilder}.
327
338
*/
328
339
ThenBuilder ifNull (AggregationExpression expression );
329
340
}
330
341
342
+ /**
343
+ * @author Christoph Strobl
344
+ * @since 3.3
345
+ */
346
+ public interface OrBuilder {
347
+
348
+ /**
349
+ * @param fieldReference the field to check for a {@literal null} value, field reference must not be {@literal null}.
350
+ * @return the {@link ThenBuilder}
351
+ */
352
+ ThenBuilder orIfNull (String fieldReference );
353
+
354
+ /**
355
+ * @param expression the expression to check for a {@literal null} value,
356
+ * @return the {@link ThenBuilder}.
357
+ */
358
+ ThenBuilder orIfNull (AggregationExpression expression );
359
+ }
360
+
331
361
/**
332
362
* @author Mark Paluch
333
363
*/
334
- public interface ThenBuilder {
364
+ public interface ThenBuilder extends OrBuilder {
335
365
336
366
/**
337
367
* @param value the value to be used if the {@code $ifNull} condition evaluates {@literal true}. Can be a
@@ -361,9 +391,10 @@ public interface ThenBuilder {
361
391
*/
362
392
static final class IfNullOperatorBuilder implements IfNullBuilder , ThenBuilder {
363
393
364
- private @ Nullable Object condition ;
394
+ private @ Nullable List < Object > conditions ;
365
395
366
396
private IfNullOperatorBuilder () {
397
+ conditions = new ArrayList <>();
367
398
}
368
399
369
400
/**
@@ -381,7 +412,7 @@ public static IfNullOperatorBuilder newBuilder() {
381
412
public ThenBuilder ifNull (String fieldReference ) {
382
413
383
414
Assert .hasText (fieldReference , "FieldReference name must not be null or empty!" );
384
- this .condition = Fields .field (fieldReference );
415
+ this .conditions . add ( Fields .field (fieldReference ) );
385
416
return this ;
386
417
}
387
418
@@ -392,15 +423,25 @@ public ThenBuilder ifNull(String fieldReference) {
392
423
public ThenBuilder ifNull (AggregationExpression expression ) {
393
424
394
425
Assert .notNull (expression , "AggregationExpression name must not be null or empty!" );
395
- this .condition = expression ;
426
+ this .conditions . add ( expression ) ;
396
427
return this ;
397
428
}
398
429
430
+ @ Override
431
+ public ThenBuilder orIfNull (String fieldReference ) {
432
+ return ifNull (fieldReference );
433
+ }
434
+
435
+ @ Override
436
+ public ThenBuilder orIfNull (AggregationExpression expression ) {
437
+ return ifNull (expression );
438
+ }
439
+
399
440
/* (non-Javadoc)
400
441
* @see org.springframework.data.mongodb.core.aggregation.ConditionalOperators.IfNull.ThenBuilder#then(java.lang.Object)
401
442
*/
402
443
public IfNull then (Object value ) {
403
- return new IfNull (condition , value );
444
+ return new IfNull (conditions , value );
404
445
}
405
446
406
447
/* (non-Javadoc)
@@ -409,7 +450,7 @@ public IfNull then(Object value) {
409
450
public IfNull thenValueOf (String fieldReference ) {
410
451
411
452
Assert .notNull (fieldReference , "FieldReference must not be null!" );
412
- return new IfNull (condition , Fields .field (fieldReference ));
453
+ return new IfNull (conditions , Fields .field (fieldReference ));
413
454
}
414
455
415
456
/* (non-Javadoc)
@@ -418,7 +459,7 @@ public IfNull thenValueOf(String fieldReference) {
418
459
public IfNull thenValueOf (AggregationExpression expression ) {
419
460
420
461
Assert .notNull (expression , "Expression must not be null!" );
421
- return new IfNull (condition , expression );
462
+ return new IfNull (conditions , expression );
422
463
}
423
464
}
424
465
}
0 commit comments