-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
EvaluationOperators.java
206 lines (174 loc) · 5.7 KB
/
EvaluationOperators.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
/*
* Copyright 2021-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 org.bson.Document;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.util.Assert;
/**
* Gateway to {@literal evaluation operators} such as {@literal $expr}.
*
* @author Divya Srivastava
* @author Christoph Strobl
* @since 3.3
*/
public class EvaluationOperators {
/**
* Take the value resulting from the given fieldReference.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link EvaluationOperatorFactory}.
*/
public static EvaluationOperatorFactory valueOf(String fieldReference) {
return new EvaluationOperatorFactory(fieldReference);
}
/**
* Take the value resulting from the given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link EvaluationOperatorFactory}.
*/
public static EvaluationOperatorFactory valueOf(AggregationExpression expression) {
return new EvaluationOperatorFactory(expression);
}
public static class EvaluationOperatorFactory {
private final String fieldReference;
private final AggregationExpression expression;
/**
* Creates new {@link EvaluationOperatorFactory} for given {@literal fieldReference}.
*
* @param fieldReference must not be {@literal null}.
*/
public EvaluationOperatorFactory(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
this.fieldReference = fieldReference;
this.expression = null;
}
/**
* Creates new {@link EvaluationOperatorFactory} for given {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
*/
public EvaluationOperatorFactory(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
this.fieldReference = null;
this.expression = expression;
}
/**
* Creates new {@link AggregationExpression} that is a valid aggregation expression.
*
* @return new instance of {@link Expr}.
*/
public Expr expr() {
return usesFieldRef() ? Expr.valueOf(fieldReference) : Expr.valueOf(expression);
}
/**
* Creates new {@link AggregationExpression} that is a valid aggregation expression.
*
* @return new instance of {@link Expr}.
*/
public LastObservationCarriedForward locf() {
return usesFieldRef() ? LastObservationCarriedForward.locfValueOf(fieldReference)
: LastObservationCarriedForward.locfValueOf(expression);
}
private boolean usesFieldRef() {
return fieldReference != null;
}
}
/**
* Allows the use of aggregation expressions within the query language.
*/
public static class Expr extends AbstractAggregationExpression {
private Expr(Object value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$expr";
}
/**
* Creates new {@link Expr}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link Expr}.
*/
public static Expr valueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new Expr(Fields.field(fieldReference));
}
/**
* Creates new {@link Expr}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Expr}.
*/
public static Expr valueOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new Expr(expression);
}
/**
* Creates {@code $expr} as {@link CriteriaDefinition}.
*
* @return the {@link CriteriaDefinition} from this expression.
*/
public CriteriaDefinition toCriteriaDefinition(AggregationOperationContext context) {
Document criteriaObject = toDocument(context);
return new CriteriaDefinition() {
@Override
public Document getCriteriaObject() {
return criteriaObject;
}
@Override
public String getKey() {
return getMongoMethod();
}
};
}
}
/**
* Sets {@literal null} and missing values to the last non-null value.
*
* @since 4.0
*/
public static class LastObservationCarriedForward extends AbstractAggregationExpression {
private LastObservationCarriedForward(Object value) {
super(value);
}
@Override
protected String getMongoMethod() {
return "$locf";
}
/**
* Creates new {@link EvaluationOperatorFactory.Expr}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link EvaluationOperatorFactory.Expr}.
*/
public static LastObservationCarriedForward locfValueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null");
return new LastObservationCarriedForward(Fields.field(fieldReference));
}
/**
* Creates new {@link LastObservationCarriedForward}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link LastObservationCarriedForward}.
*/
public static LastObservationCarriedForward locfValueOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null");
return new LastObservationCarriedForward(expression);
}
}
}