@@ -29,9 +29,9 @@ final class MqlExpression<T extends Expression>
29
29
implements Expression , BooleanExpression , IntegerExpression , NumberExpression ,
30
30
StringExpression , DateExpression , DocumentExpression , ArrayExpression <T > {
31
31
32
- private final Function <CodecRegistry , BsonValue > fn ;
32
+ private final Function <CodecRegistry , AstPlaceholder > fn ;
33
33
34
- MqlExpression (final Function <CodecRegistry , BsonValue > fn ) {
34
+ MqlExpression (final Function <CodecRegistry , AstPlaceholder > fn ) {
35
35
this .fn = fn ;
36
36
}
37
37
@@ -41,33 +41,41 @@ final class MqlExpression<T extends Expression>
41
41
* {@link MqlExpressionCodec}.
42
42
*/
43
43
BsonValue toBsonValue (final CodecRegistry codecRegistry ) {
44
- return fn .apply (codecRegistry );
44
+ return fn .apply (codecRegistry ). bsonValue ;
45
45
}
46
46
47
- private Function < CodecRegistry , BsonValue > astDoc (final String name , final BsonDocument value ) {
48
- return ( cr ) -> new BsonDocument (name , value );
47
+ private AstPlaceholder astDoc (final String name , final BsonDocument value ) {
48
+ return new AstPlaceholder ( new BsonDocument (name , value ) );
49
49
}
50
50
51
- private Function <CodecRegistry , BsonValue > ast (final String name ) {
52
- return (cr ) -> new BsonDocument (name , this .toBsonValue (cr ));
51
+ static final class AstPlaceholder {
52
+ private final BsonValue bsonValue ;
53
+
54
+ AstPlaceholder (final BsonValue bsonValue ) {
55
+ this .bsonValue = bsonValue ;
56
+ }
57
+ }
58
+
59
+ private Function <CodecRegistry , AstPlaceholder > ast (final String name ) {
60
+ return (cr ) -> new AstPlaceholder (new BsonDocument (name , this .toBsonValue (cr )));
53
61
}
54
62
55
- private Function <CodecRegistry , BsonValue > ast (final String name , final Expression param1 ) {
63
+ private Function <CodecRegistry , AstPlaceholder > ast (final String name , final Expression param1 ) {
56
64
return (cr ) -> {
57
65
BsonArray value = new BsonArray ();
58
66
value .add (this .toBsonValue (cr ));
59
67
value .add (extractBsonValue (cr , param1 ));
60
- return new BsonDocument (name , value );
68
+ return new AstPlaceholder ( new BsonDocument (name , value ) );
61
69
};
62
70
}
63
71
64
- private Function <CodecRegistry , BsonValue > ast (final String name , final Expression param1 , final Expression param2 ) {
72
+ private Function <CodecRegistry , AstPlaceholder > ast (final String name , final Expression param1 , final Expression param2 ) {
65
73
return (cr ) -> {
66
74
BsonArray value = new BsonArray ();
67
75
value .add (this .toBsonValue (cr ));
68
76
value .add (extractBsonValue (cr , param1 ));
69
77
value .add (extractBsonValue (cr , param2 ));
70
- return new BsonDocument (name , value );
78
+ return new AstPlaceholder ( new BsonDocument (name , value ) );
71
79
};
72
80
}
73
81
@@ -89,12 +97,12 @@ <R extends Expression> R assertImplementsAllExpressions() {
89
97
return (R ) this ;
90
98
}
91
99
92
- private static <R extends Expression > R newMqlExpression (final Function <CodecRegistry , BsonValue > ast ) {
100
+ private static <R extends Expression > R newMqlExpression (final Function <CodecRegistry , AstPlaceholder > ast ) {
93
101
return new MqlExpression <>(ast ).assertImplementsAllExpressions ();
94
102
}
95
103
96
104
private <R extends Expression > R variable (final String variable ) {
97
- return newMqlExpression ((cr ) -> new BsonString (variable ));
105
+ return newMqlExpression ((cr ) -> new AstPlaceholder ( new BsonString (variable ) ));
98
106
}
99
107
100
108
/** @see BooleanExpression */
@@ -159,15 +167,15 @@ public <R extends Expression> ArrayExpression<R> map(final Function<? super T, ?
159
167
T varThis = variable ("$$this" );
160
168
return new MqlExpression <>((cr ) -> astDoc ("$map" , new BsonDocument ()
161
169
.append ("input" , this .toBsonValue (cr ))
162
- .append ("in" , extractBsonValue (cr , in .apply (varThis )))). apply ( cr ) );
170
+ .append ("in" , extractBsonValue (cr , in .apply (varThis )))));
163
171
}
164
172
165
173
@ Override
166
174
public ArrayExpression <T > filter (final Function <? super T , ? extends BooleanExpression > cond ) {
167
175
T varThis = variable ("$$this" );
168
176
return new MqlExpression <T >((cr ) -> astDoc ("$filter" , new BsonDocument ()
169
177
.append ("input" , this .toBsonValue (cr ))
170
- .append ("cond" , extractBsonValue (cr , cond .apply (varThis )))). apply ( cr ) );
178
+ .append ("cond" , extractBsonValue (cr , cond .apply (varThis )))));
171
179
}
172
180
173
181
@ Override
@@ -177,7 +185,81 @@ public T reduce(final T initialValue, final BinaryOperator<T> in) {
177
185
return newMqlExpression ((cr ) -> astDoc ("$reduce" , new BsonDocument ()
178
186
.append ("input" , this .toBsonValue (cr ))
179
187
.append ("initialValue" , extractBsonValue (cr , initialValue ))
180
- .append ("in" , extractBsonValue (cr , in .apply (varThis , varValue )))).apply (cr ));
188
+ .append ("in" , extractBsonValue (cr , in .apply (varThis , varValue )))));
189
+ }
190
+
191
+
192
+ /** @see IntegerExpression
193
+ * @see NumberExpression */
194
+
195
+ @ Override
196
+ public IntegerExpression multiply (final NumberExpression n ) {
197
+ return newMqlExpression (ast ("$multiply" , n ));
198
+ }
199
+
200
+ @ Override
201
+ public NumberExpression add (final NumberExpression n ) {
202
+ return new MqlExpression <>(ast ("$add" , n ));
203
+ }
204
+
205
+ @ Override
206
+ public NumberExpression divide (final NumberExpression n ) {
207
+ return new MqlExpression <>(ast ("$divide" , n ));
208
+ }
209
+
210
+ @ Override
211
+ public NumberExpression max (final NumberExpression n ) {
212
+ return new MqlExpression <>(ast ("$max" , n ));
213
+ }
214
+
215
+ @ Override
216
+ public NumberExpression min (final NumberExpression n ) {
217
+ return new MqlExpression <>(ast ("$min" , n ));
218
+ }
219
+
220
+ @ Override
221
+ public IntegerExpression round () {
222
+ return new MqlExpression <>(ast ("$round" ));
223
+ }
224
+
225
+ @ Override
226
+ public NumberExpression round (final IntegerExpression place ) {
227
+ return new MqlExpression <>(ast ("$round" , place ));
228
+ }
229
+
230
+ @ Override
231
+ public IntegerExpression multiply (final IntegerExpression i ) {
232
+ return new MqlExpression <>(ast ("$multiply" , i ));
233
+ }
234
+
235
+ @ Override
236
+ public IntegerExpression abs () {
237
+ return newMqlExpression (ast ("$abs" ));
238
+ }
239
+
240
+ @ Override
241
+ public NumberExpression subtract (final NumberExpression n ) {
242
+ return new MqlExpression <>(ast ("$subtract" , n ));
243
+ }
244
+
245
+ @ Override
246
+ public IntegerExpression add (final IntegerExpression i ) {
247
+ return new MqlExpression <>(ast ("$add" , i ));
248
+ }
249
+
250
+ @ Override
251
+ public IntegerExpression subtract (final IntegerExpression i ) {
252
+ return new MqlExpression <>(ast ("$subtract" , i ));
253
+ }
254
+
255
+ @ Override
256
+ public IntegerExpression max (final IntegerExpression i ) {
257
+ return new MqlExpression <>(ast ("$max" , i ));
258
+ }
259
+
260
+ @ Override
261
+ public IntegerExpression min (final IntegerExpression i ) {
262
+ return new MqlExpression <>(ast ("$min" , i ));
181
263
}
182
264
183
265
0 commit comments