-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
DateOperators.java
3270 lines (2832 loc) · 102 KB
/
DateOperators.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.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Gateway to {@literal Date} aggregation operations.
*
* @author Christoph Strobl
* @author Matt Morrissette
* @since 1.10
*/
public class DateOperators {
/**
* Take the date referenced by given {@literal fieldReference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link DateOperatorFactory}.
*/
public static DateOperatorFactory dateOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new DateOperatorFactory(fieldReference);
}
/**
* Take the date referenced by given {@literal fieldReference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link DateOperatorFactory}.
* @since 3.3
*/
public static DateOperatorFactory zonedDateOf(String fieldReference, Timezone timezone) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new DateOperatorFactory(fieldReference).withTimezone(timezone);
}
/**
* Take the date resulting from the given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link DateOperatorFactory}.
*/
public static DateOperatorFactory dateOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new DateOperatorFactory(expression);
}
/**
* Take the date resulting from the given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link DateOperatorFactory}.
* @since 3.3
*/
public static DateOperatorFactory zonedDateOf(AggregationExpression expression, Timezone timezone) {
Assert.notNull(expression, "Expression must not be null");
return new DateOperatorFactory(expression).withTimezone(timezone);
}
/**
* Take the given value as date. <br />
* This can be one of:
* <ul>
* <li>{@link java.util.Date}</li>
* <li>{@link java.util.Calendar}</li>
* <li>{@link java.time.Instant}</li>
* <li>{@link java.time.ZonedDateTime}</li>
* <li>{@link java.lang.Long}</li>
* <li>{@link Field}</li>
* <li>{@link AggregationExpression}</li>
* </ul>
*
* @param value must not be {@literal null}.
* @return new instance of {@link DateOperatorFactory}.
* @since 2.1
*/
public static DateOperatorFactory dateValue(Object value) {
Assert.notNull(value, "Value must not be null");
return new DateOperatorFactory(value);
}
/**
* Construct a Date object by providing the date’s constituent properties.<br />
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @return new instance of {@link DateFromPartsOperatorFactory}.
* @since 2.1
*/
public static DateFromPartsOperatorFactory dateFromParts() {
return new DateFromPartsOperatorFactory(Timezone.none());
}
/**
* Construct a Date object from the given date {@link String}.<br />
* To use a {@link Field field reference} or {@link AggregationExpression} as source of the date string consider
* {@link DateOperatorFactory#fromString()} or {@link DateFromString#fromStringOf(AggregationExpression)}.<br />
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @return new instance of {@link DateFromPartsOperatorFactory}.
* @since 2.1
*/
public static DateFromString dateFromString(String value) {
return DateFromString.fromString(value);
}
/**
* Timezone represents a MongoDB timezone abstraction which can be represented with a timezone ID or offset as a
* {@link String}. Also accepts a {@link AggregationExpression} or {@link Field} that resolves to a {@link String} of
* either Olson Timezone Identifier or a UTC Offset.<br />
* <table>
* <tr>
* <th>Format</th>
* <th>Example</th>
* </tr>
* <tr>
* <td>Olson Timezone Identifier</td>
* <td>"America/New_York"<br />
* "Europe/London"<br />
* "GMT"</td>
* </tr>
* <tr>
* <td>UTC Offset</td>
* <td>+/-[hh]:[mm], e.g. "+04:45"<br />
* -[hh][mm], e.g. "-0530"<br />
* +/-[hh], e.g. "+03"</td>
* </tr>
* </table>
* <strong>NOTE: </strong>Support for timezones in aggregations Requires MongoDB 3.6 or later.
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 2.1
*/
public static class Timezone {
private static final Timezone NONE = new Timezone(null);
private final @Nullable Object value;
private Timezone(@Nullable Object value) {
this.value = value;
}
/**
* Return an empty {@link Timezone}.
*
* @return never {@literal null}.
*/
public static Timezone none() {
return NONE;
}
/**
* Create a {@link Timezone} for the given value which must be a valid expression that resolves to a {@link String}
* representing an Olson Timezone Identifier or UTC Offset.
*
* @param value the plain timezone {@link String}, a {@link Field} holding the timezone or an
* {@link AggregationExpression} resulting in the timezone.
* @return new instance of {@link Timezone}.
*/
public static Timezone valueOf(Object value) {
Assert.notNull(value, "Value must not be null");
return new Timezone(value);
}
/**
* Create a {@link Timezone} for the given {@link TimeZone} rendering the offset as UTC offset.
*
* @param timeZone {@link TimeZone} rendering the offset as UTC offset.
* @return new instance of {@link Timezone}.
* @since 3.3
*/
public static Timezone fromOffset(TimeZone timeZone) {
Assert.notNull(timeZone, "TimeZone must not be null");
return fromOffset(
ZoneOffset.ofTotalSeconds(Math.toIntExact(TimeUnit.MILLISECONDS.toSeconds(timeZone.getRawOffset()))));
}
/**
* Create a {@link Timezone} for the given {@link ZoneOffset} rendering the offset as UTC offset.
*
* @param offset {@link ZoneOffset} rendering the offset as UTC offset.
* @return new instance of {@link Timezone}.
* @since 3.3
*/
public static Timezone fromOffset(ZoneOffset offset) {
Assert.notNull(offset, "ZoneOffset must not be null");
return new Timezone(offset.toString());
}
/**
* Create a {@link Timezone} for the given {@link TimeZone} rendering the offset as UTC offset.
*
* @param timeZone {@link Timezone} rendering the offset as zone identifier.
* @return new instance of {@link Timezone}.
* @since 3.3
*/
public static Timezone fromZone(TimeZone timeZone) {
Assert.notNull(timeZone, "TimeZone must not be null");
return valueOf(timeZone.getID());
}
/**
* Create a {@link Timezone} for the given {@link java.time.ZoneId} rendering the offset as UTC offset.
*
* @param zoneId {@link ZoneId} rendering the offset as zone identifier.
* @return new instance of {@link Timezone}.
* @since 3.3
*/
public static Timezone fromZone(ZoneId zoneId) {
Assert.notNull(zoneId, "ZoneId must not be null");
return new Timezone(zoneId.toString());
}
/**
* Create a {@link Timezone} for the {@link Field} reference holding the Olson Timezone Identifier or UTC Offset.
*
* @param fieldReference the {@link Field} holding the timezone.
* @return new instance of {@link Timezone}.
*/
public static Timezone ofField(String fieldReference) {
return valueOf(Fields.field(fieldReference));
}
/**
* Create a {@link Timezone} for the {@link AggregationExpression} resulting in the Olson Timezone Identifier or UTC
* Offset.
*
* @param expression the {@link AggregationExpression} resulting in the timezone.
* @return new instance of {@link Timezone}.
*/
public static Timezone ofExpression(AggregationExpression expression) {
return valueOf(expression);
}
@Nullable
Object getValue() {
return value;
}
}
/**
* @author Christoph Strobl
* @author Matt Morrissette
*/
public static class DateOperatorFactory {
private final @Nullable String fieldReference;
private final @Nullable Object dateValue;
private final @Nullable AggregationExpression expression;
private final Timezone timezone;
/**
* @param fieldReference
* @param expression
* @param value
* @param timezone
* @since 2.1
*/
private DateOperatorFactory(@Nullable String fieldReference, @Nullable AggregationExpression expression,
@Nullable Object value, Timezone timezone) {
this.fieldReference = fieldReference;
this.expression = expression;
this.dateValue = value;
this.timezone = timezone;
}
/**
* Creates new {@link DateOperatorFactory} for given {@literal fieldReference}.
*
* @param fieldReference must not be {@literal null}.
*/
public DateOperatorFactory(String fieldReference) {
this(fieldReference, null, null, Timezone.none());
Assert.notNull(fieldReference, "FieldReference must not be null");
}
/**
* Creates new {@link DateOperatorFactory} for given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
*/
public DateOperatorFactory(AggregationExpression expression) {
this(null, expression, null, Timezone.none());
Assert.notNull(expression, "Expression must not be null");
}
/**
* Creates new {@link DateOperatorFactory} for given {@code value} that resolves to a Date. <br />
* <ul>
* <li>{@link java.util.Date}</li>
* <li>{@link java.util.Calendar}</li>
* <li>{@link java.time.Instant}</li>
* <li>{@link java.time.ZonedDateTime}</li>
* <li>{@link java.lang.Long}</li>
* </ul>
*
* @param value must not be {@literal null}.
* @since 2.1
*/
public DateOperatorFactory(Object value) {
this(null, null, value, Timezone.none());
Assert.notNull(value, "Value must not be null");
}
/**
* Create a new {@link DateOperatorFactory} bound to a given {@link Timezone}.<br />
* <strong>NOTE:</strong> Requires Mongo 3.6 or later.
*
* @param timezone must not be {@literal null}. Use {@link Timezone#none()} instead.
* @return new instance of {@link DateOperatorFactory}.
* @throws IllegalArgumentException if given {@literal timezone} is {@literal null}.
* @since 2.1
*/
public DateOperatorFactory withTimezone(Timezone timezone) {
Assert.notNull(timezone, "Timezone must not be null");
return new DateOperatorFactory(fieldReference, expression, dateValue, timezone);
}
/**
* Creates new {@link AggregationExpression} that adds the value of the given {@link AggregationExpression
* expression} (in {@literal units}).
*
* @param expression must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateAdd addValueOf(AggregationExpression expression, String unit) {
return applyTimezone(DateAdd.addValueOf(expression, unit).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that adds the value of the given {@link AggregationExpression
* expression} (in {@literal units}).
*
* @param expression must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateAdd addValueOf(AggregationExpression expression, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(DateAdd.addValueOf(expression, unit.name().toLowerCase(Locale.ROOT)).toDate(dateReference()),
timezone);
}
/**
* Creates new {@link AggregationExpression} that adds the value stored at the given {@literal field} (in
* {@literal units}).
*
* @param fieldReference must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateAdd addValueOf(String fieldReference, String unit) {
return applyTimezone(DateAdd.addValueOf(fieldReference, unit).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that adds the value stored at the given {@literal field} (in
* {@literal units}).
*
* @param fieldReference must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateAdd addValueOf(String fieldReference, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(
DateAdd.addValueOf(fieldReference, unit.name().toLowerCase(Locale.ROOT)).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that adds the given value (in {@literal units}).
*
* @param value must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return
* @since 3.3 new instance of {@link DateAdd}.
*/
public DateAdd add(Object value, String unit) {
return applyTimezone(DateAdd.addValue(value, unit).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that adds the given value (in {@literal units}).
*
* @param value must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return
* @since 3.3 new instance of {@link DateAdd}.
*/
public DateAdd add(Object value, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(DateAdd.addValue(value, unit.name().toLowerCase(Locale.ROOT)).toDate(dateReference()),
timezone);
}
/**
* Creates new {@link AggregationExpression} that subtracts the value of the given {@link AggregationExpression
* expression} (in {@literal units}).
*
* @param expression must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateSubtract}.
* @since 4.0
*/
public DateSubtract subtractValueOf(AggregationExpression expression, String unit) {
return applyTimezone(DateSubtract.subtractValueOf(expression, unit).fromDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that subtracts the value of the given {@link AggregationExpression
* expression} (in {@literal units}).
*
* @param expression must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateSubtract}.
* @since 4.0
*/
public DateSubtract subtractValueOf(AggregationExpression expression, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(
DateSubtract.subtractValueOf(expression, unit.name().toLowerCase(Locale.ROOT)).fromDate(dateReference()),
timezone);
}
/**
* Creates new {@link AggregationExpression} that subtracts the value stored at the given {@literal field} (in
* {@literal units}).
*
* @param fieldReference must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateSubtract}.
* @since 4.0
*/
public DateSubtract subtractValueOf(String fieldReference, String unit) {
return applyTimezone(DateSubtract.subtractValueOf(fieldReference, unit).fromDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that subtracts the value stored at the given {@literal field} (in
* {@literal units}).
*
* @param fieldReference must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateSubtract}.
* @since 4.0
*/
public DateSubtract subtractValueOf(String fieldReference, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(
DateSubtract.subtractValueOf(fieldReference, unit.name().toLowerCase(Locale.ROOT)).fromDate(dateReference()),
timezone);
}
/**
* Creates new {@link AggregationExpression} that subtracts the given value (in {@literal units}).
*
* @param value must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateSubtract}.
* @since 4.0
*/
public DateSubtract subtract(Object value, String unit) {
return applyTimezone(DateSubtract.subtractValue(value, unit).fromDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that subtracts the given value (in {@literal units}).
*
* @param value must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateSubtract}.
* @since 4.0
*/
public DateSubtract subtract(Object value, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(
DateSubtract.subtractValue(value, unit.name().toLowerCase(Locale.ROOT)).fromDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that truncates a date to the given {@literal unit}.
*
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateTrunc}.
* @since 4.0
*/
public DateTrunc truncate(String unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(DateTrunc.truncateValue(dateReference()).to(unit), timezone);
}
/**
* Creates new {@link AggregationExpression} that truncates a date to the given {@literal unit}.
*
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateTrunc}.
* @since 4.0
*/
public DateTrunc truncate(TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return truncate(unit.name().toLowerCase(Locale.ROOT));
}
/**
* Creates new {@link AggregationExpression} that returns the day of the year for a date as a number between 1 and
* 366.
*
* @return new instance of {@link DayOfYear}.
*/
public DayOfYear dayOfYear() {
return applyTimezone(DayOfYear.dayOfYear(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the day of the month for a date as a number between 1 and
* 31.
*
* @return new instance of {@link DayOfMonth}.
*/
public DayOfMonth dayOfMonth() {
return applyTimezone(DayOfMonth.dayOfMonth(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the day of the week for a date as a number between 1
* (Sunday) and 7 (Saturday).
*
* @return new instance of {@link DayOfWeek}.
*/
public DayOfWeek dayOfWeek() {
return applyTimezone(DayOfWeek.dayOfWeek(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that calculates the difference (in {@literal units}) to the date
* computed by the given {@link AggregationExpression expression}.
*
* @param expression must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateDiff diffValueOf(AggregationExpression expression, String unit) {
return applyTimezone(DateDiff.diffValueOf(expression, unit).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that calculates the difference (in {@literal units}) to the date
* computed by the given {@link AggregationExpression expression}.
*
* @param expression must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateDiff diffValueOf(AggregationExpression expression, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(
DateDiff.diffValueOf(expression, unit.name().toLowerCase(Locale.ROOT)).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that calculates the difference (in {@literal units}) to the date stored
* at the given {@literal field}.
*
* @param fieldReference must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateDiff diffValueOf(String fieldReference, String unit) {
return applyTimezone(DateDiff.diffValueOf(fieldReference, unit).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that calculates the difference (in {@literal units}) to the date stored
* at the given {@literal field}.
*
* @param fieldReference must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateDiff diffValueOf(String fieldReference, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(
DateDiff.diffValueOf(fieldReference, unit.name().toLowerCase(Locale.ROOT)).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that calculates the difference (in {@literal units}) to the date given
* {@literal value}.
*
* @param value anything the resolves to a valid date. Must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateDiff diff(Object value, String unit) {
return applyTimezone(DateDiff.diffValue(value, unit).toDate(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that calculates the difference (in {@literal units}) to the date given
* {@literal value}.
*
* @param value anything the resolves to a valid date. Must not be {@literal null}.
* @param unit the unit of measure. Must not be {@literal null}.
* @return new instance of {@link DateAdd}. @since 3.3
*/
public DateDiff diff(Object value, TemporalUnit unit) {
Assert.notNull(unit, "TemporalUnit must not be null");
return applyTimezone(DateDiff.diffValue(value, unit.name().toLowerCase(Locale.ROOT)).toDate(dateReference()),
timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the year portion of a date.
*
* @return new instance of {@link Year}.
*/
public Year year() {
return applyTimezone(Year.year(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the month of a date as a number between 1 and 12.
*
* @return new instance of {@link Month}.
*/
public Month month() {
return applyTimezone(Month.month(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the week of the year for a date as a number between 0 and
* 53.
*
* @return new instance of {@link Week}.
*/
public Week week() {
return applyTimezone(Week.week(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the hour portion of a date as a number between 0 and 23.
*
* @return new instance of {@link Hour}.
*/
public Hour hour() {
return applyTimezone(Hour.hour(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the minute portion of a date as a number between 0 and 59.
*
* @return new instance of {@link Minute}.
*/
public Minute minute() {
return applyTimezone(Minute.minute(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the second portion of a date as a number between 0 and 59,
* but can be 60 to account for leap seconds.
*
* @return new instance of {@link Second}.
*/
public Second second() {
return applyTimezone(Second.second(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the millisecond portion of a date as an integer between 0
* and 999.
*
* @return new instance of {@link Millisecond}.
*/
public Millisecond millisecond() {
return applyTimezone(Millisecond.millisecond(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that converts a date object to a string according to a user-specified
* {@literal format}.
*
* @param format must not be {@literal null}.
* @return new instance of {@link DateToString}.
*/
public DateToString toString(String format) {
return applyTimezone(DateToString.dateToString(dateReference()).toString(format), timezone);
}
/**
* Creates new {@link AggregationExpression} that converts a date object to a string according to the server default
* format.
*
* @return new instance of {@link DateToString}.
* @since 2.1
*/
public DateToString toStringWithDefaultFormat() {
return applyTimezone(DateToString.dateToString(dateReference()).defaultFormat(), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the weekday number in ISO 8601-2018 format, ranging from 1
* (for Monday) to 7 (for Sunday).
*
* @return new instance of {@link IsoDayOfWeek}.
*/
public IsoDayOfWeek isoDayOfWeek() {
return applyTimezone(IsoDayOfWeek.isoDayWeek(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the week number in ISO 8601-2018 format, ranging from 1 to
* 53.
*
* @return new instance of {@link IsoWeek}.
*/
public IsoWeek isoWeek() {
return applyTimezone(IsoWeek.isoWeek(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns the year number in ISO 8601-2018 format.
*
* @return new instance of {@link IsoWeekYear}.
*/
public IsoWeekYear isoWeekYear() {
return applyTimezone(IsoWeekYear.isoWeekYear(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that returns a document containing the constituent parts of the date as
* individual properties.<br />
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @return new instance of {@link DateToParts}.
* @since 2.1
*/
public DateToParts toParts() {
return applyTimezone(DateToParts.dateToParts(dateReference()), timezone);
}
/**
* Creates new {@link AggregationExpression} that converts a date/time string to a date object.<br />
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @return new instance of {@link DateFromString}.
* @since 2.1
*/
public DateFromString fromString() {
return applyTimezone(DateFromString.fromString(dateReference()), timezone);
}
private Object dateReference() {
if (usesFieldRef()) {
return Fields.field(fieldReference);
}
return usesExpression() ? expression : dateValue;
}
private boolean usesFieldRef() {
return fieldReference != null;
}
private boolean usesExpression() {
return expression != null;
}
}
/**
* @author Matt Morrissette
* @author Christoph Strobl
* @since 2.1
*/
public static class DateFromPartsOperatorFactory {
private final Timezone timezone;
private DateFromPartsOperatorFactory(Timezone timezone) {
this.timezone = timezone;
}
/**
* Set the {@literal week date year} to the given value which must resolve to a weekday in range {@code 0 - 9999}.
* Can be a simple value, {@link Field field reference} or {@link AggregationExpression expression}.
*
* @param isoWeekYear must not be {@literal null}.
* @return new instance of {@link IsoDateFromParts} with {@link Timezone} if set.
* @throws IllegalArgumentException if given {@literal isoWeekYear} is {@literal null}.
*/
public IsoDateFromParts isoWeekYear(Object isoWeekYear) {
return applyTimezone(IsoDateFromParts.dateFromParts().isoWeekYear(isoWeekYear), timezone);
}
/**
* Set the {@literal week date year} to the value resolved by following the given {@link Field field reference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link IsoDateFromParts} with {@link Timezone} if set.
* @throws IllegalArgumentException if given {@literal fieldReference} is {@literal null}.
*/
public IsoDateFromParts isoWeekYearOf(String fieldReference) {
return isoWeekYear(Fields.field(fieldReference));
}
/**
* Set the {@literal week date year} to the result of the given {@link AggregationExpression expression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link IsoDateFromParts} with {@link Timezone} if set.
* @throws IllegalArgumentException if given {@literal expression} is {@literal null}.
*/
public IsoDateFromParts isoWeekYearOf(AggregationExpression expression) {
return isoWeekYear(expression);
}
/**
* Set the {@literal year} to the given value which must resolve to a calendar year. Can be a simple value,
* {@link Field field reference} or {@link AggregationExpression expression}.
*
* @param year must not be {@literal null}.
* @return new instance of {@link DateFromParts} with {@link Timezone} if set.
* @throws IllegalArgumentException if given {@literal year} is {@literal null}
*/
public DateFromParts year(Object year) {
return applyTimezone(DateFromParts.dateFromParts().year(year), timezone);
}
/**
* Set the {@literal year} to the value resolved by following the given {@link Field field reference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link DateFromParts} with {@link Timezone} if set.
* @throws IllegalArgumentException if given {@literal fieldReference} is {@literal null}.
*/
public DateFromParts yearOf(String fieldReference) {
return year(Fields.field(fieldReference));
}
/**
* Set the {@literal year} to the result of the given {@link AggregationExpression expression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link DateFromParts} with {@link Timezone} if set.
* @throws IllegalArgumentException if given {@literal expression} is {@literal null}.
*/
public DateFromParts yearOf(AggregationExpression expression) {
return year(expression);
}
/**
* Create a new {@link DateFromPartsOperatorFactory} bound to a given {@link Timezone}.<br />
*
* @param timezone must not be {@literal null}. Use {@link Timezone#none()} instead.
* @return new instance of {@link DateFromPartsOperatorFactory}.
* @throws IllegalArgumentException if given {@literal timezone} is {@literal null}.
*/
public DateFromPartsOperatorFactory withTimezone(Timezone timezone) {
Assert.notNull(timezone, "Timezone must not be null");
return new DateFromPartsOperatorFactory(timezone);
}
}
/**
* {@link AggregationExpression} capable of setting a given {@link Timezone}.
*
* @author Christoph Strobl
* @since 2.1
*/
public static abstract class TimezonedDateAggregationExpression extends AbstractAggregationExpression {
protected TimezonedDateAggregationExpression(Object value) {
super(value);
}
/**
* Append the {@code timezone} to a given source. The source itself can be a {@link Map} of already set properties
* or a single value. In case of single value {@code source} the value will be added as {@code date} property.
*
* @param source must not be {@literal null}.
* @param timezone must not be {@literal null} use {@link Timezone#none()} instead.
* @return
*/
protected static java.util.Map<String, Object> appendTimezone(Object source, Timezone timezone) {
java.util.Map<String, Object> args;
if (source instanceof Map) {
args = new LinkedHashMap<>((Map) source);
} else {
args = new LinkedHashMap<>(2);
args.put("date", source);
}
if (!ObjectUtils.nullSafeEquals(Timezone.none(), timezone)) {
args.put("timezone", timezone.value);
} else if (args.containsKey("timezone")) {
args.remove("timezone");
}
return args;
}
/**
* Optionally set the {@link Timezone} to use. If not specified {@literal UTC} is used.<br />
* <strong>NOTE:</strong> Requires MongoDB 3.6 or later.
*
* @param timezone must not be {@literal null}. Consider {@link Timezone#none()} instead.
* @return new instance.
* @throws IllegalArgumentException if given {@literal timezone} is {@literal null}.
*/
protected abstract TimezonedDateAggregationExpression withTimezone(Timezone timezone);
protected boolean hasTimezone() {
return contains("timezone");
}
}
/**
* {@link AggregationExpression} for {@code $dayOfYear}.
*
* @author Christoph Strobl
* @author Matt Morrissette
*/
public static class DayOfYear extends TimezonedDateAggregationExpression {
private DayOfYear(Object value) {
super(value);
}