Skip to content

Commit 56e8756

Browse files
committed
Expand pass and switch; add tests; fix broken test
1 parent d6d1eed commit 56e8756

13 files changed

+244
-96
lines changed

driver-core/src/main/com/mongodb/client/model/expressions/ArrayExpression.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,8 @@ default ArrayExpression<T> slice(final int start, final int length) {
112112
ArrayExpression<T> union(ArrayExpression<? extends T> set);
113113

114114
ArrayExpression<T> distinct();
115+
116+
<R extends Expression> R passArrayTo(Function<? super ArrayExpression<T>, R> f);
117+
118+
<R extends Expression> R switchArrayOn(Function<Branches, ? extends BranchesTerminal<? super ArrayExpression<T>, R>> on);
115119
}

driver-core/src/main/com/mongodb/client/model/expressions/BooleanExpression.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb.client.model.expressions;
1818

19+
import java.util.function.Function;
20+
1921
/**
2022
* Expresses a boolean value.
2123
*/
@@ -58,4 +60,8 @@ public interface BooleanExpression extends Expression {
5860
* @param <T> The type of the resulting expression.
5961
*/
6062
<T extends Expression> T cond(T left, T right);
63+
64+
<R extends Expression> R passBooleanTo(Function<? super BooleanExpression, R> f);
65+
66+
<R extends Expression> R switchBooleanOn(Function<Branches, ? extends BranchesTerminal<? super BooleanExpression, R>> on);
6167
}

driver-core/src/main/com/mongodb/client/model/expressions/DateExpression.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb.client.model.expressions;
1818

19+
import java.util.function.Function;
20+
1921
/**
2022
* Expresses a date value.
2123
*/
@@ -33,4 +35,6 @@ public interface DateExpression extends Expression {
3335

3436
StringExpression asString(StringExpression timezone, StringExpression format);
3537

38+
<R extends Expression> R passDateTo(Function<? super DateExpression, R> f);
39+
<R extends Expression> R switchDateOn(Function<Branches, ? extends BranchesTerminal<? super DateExpression, R>> on);
3640
}

driver-core/src/main/com/mongodb/client/model/expressions/DocumentExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.bson.conversions.Bson;
2020

2121
import java.time.Instant;
22+
import java.util.function.Function;
2223

2324
import static com.mongodb.client.model.expressions.Expressions.of;
2425
import static com.mongodb.client.model.expressions.Expressions.ofMap;
@@ -98,4 +99,8 @@ default <T extends Expression> MapExpression<T> getMap(final String fieldName, f
9899
<T extends Expression> ArrayExpression<T> getArray(String fieldName, ArrayExpression<? extends T> other);
99100

100101
DocumentExpression merge(DocumentExpression other);
102+
103+
104+
<R extends Expression> R passDocumentTo(Function<? super DocumentExpression, R> f);
105+
<R extends Expression> R switchDocumentOn(Function<Branches, ? extends BranchesTerminal<? super DocumentExpression, R>> on);
101106
}

driver-core/src/main/com/mongodb/client/model/expressions/Expression.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,8 @@ public interface Expression {
117117

118118
StringExpression asString();
119119

120-
/**
121-
* Applies the given function to this argument. Note that "apply" usually
122-
* applies functions to arguments; here, the parameters are reversed.
123-
*
124-
* @param f
125-
* @return
126-
* @param <T>
127-
* @param <R>
128-
*/
129-
<T extends Expression, R extends Expression> R apply(Function<T, R> f);
120+
<R extends Expression> R passTo(Function<? super Expression, R> f);
121+
122+
<R extends Expression> R switchOn(Function<Branches, ? extends BranchesTerminal<? super Expression, R>> on);
130123

131-
<T0 extends Expression, R0 extends Expression> R0 switchMap(Function<Branches, BranchesTerminal<T0, R0>> switchMap);
132124
}

driver-core/src/main/com/mongodb/client/model/expressions/IntegerExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb.client.model.expressions;
1818

19+
import java.util.function.Function;
20+
1921
/**
2022
* Expresses an integer value.
2123
*/
@@ -42,4 +44,7 @@ default IntegerExpression subtract(final int subtract) {
4244
IntegerExpression min(IntegerExpression i);
4345

4446
IntegerExpression abs();
47+
48+
<R extends Expression> R passIntegerTo(Function<? super IntegerExpression, R> f);
49+
<R extends Expression> R switchIntegerOn(Function<Branches, ? extends BranchesTerminal<? super IntegerExpression, R>> on);
4550
}

driver-core/src/main/com/mongodb/client/model/expressions/MapExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb.client.model.expressions;
1818

19+
import java.util.function.Function;
20+
1921
import static com.mongodb.client.model.expressions.Expressions.of;
2022

2123
public interface MapExpression<T extends Expression> extends Expression {
@@ -47,4 +49,7 @@ default MapExpression<T> unset(final String key) {
4749
MapExpression<T> merge(MapExpression<T> map);
4850

4951
ArrayExpression<EntryExpression<T>> entrySet();
52+
53+
<R extends Expression> R passMapTo(Function<? super MapExpression<T>, R> f);
54+
<R extends Expression> R switchMapOn(Function<Branches, ? extends BranchesTerminal<? super MapExpression<T>, R>> on);
5055
}

driver-core/src/main/com/mongodb/client/model/expressions/MqlExpression.java

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,113 @@ public DocumentExpression unsetField(final String fieldName) {
286286
/** @see Expression */
287287

288288
@Override
289-
public <Q extends Expression, R extends Expression> R apply(final Function<Q, R> f) {
289+
public <R extends Expression> R passTo(final Function<? super Expression, R> f) {
290290
return f.apply(this.assertImplementsAllExpressions());
291291
}
292292

293+
@Override
294+
public <R extends Expression> R switchOn(final Function<Branches, ? extends BranchesTerminal<? super Expression, R>> switchMap) {
295+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
296+
}
297+
298+
@Override
299+
public <R extends Expression> R passBooleanTo(final Function<? super BooleanExpression, R> f) {
300+
return f.apply(this.assertImplementsAllExpressions());
301+
}
302+
303+
@Override
304+
public <R extends Expression> R switchBooleanOn(final Function<Branches, ? extends BranchesTerminal<? super BooleanExpression, R>> switchMap) {
305+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
306+
}
307+
308+
@Override
309+
public <R extends Expression> R passIntegerTo(final Function<? super IntegerExpression, R> f) {
310+
return f.apply(this.assertImplementsAllExpressions());
311+
}
312+
313+
@Override
314+
public <R extends Expression> R switchIntegerOn(final Function<Branches, ? extends BranchesTerminal<? super IntegerExpression, R>> switchMap) {
315+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
316+
}
317+
318+
@Override
319+
public <R extends Expression> R passNumberTo(final Function<? super NumberExpression, R> f) {
320+
return f.apply(this.assertImplementsAllExpressions());
321+
}
322+
323+
@Override
324+
public <R extends Expression> R switchNumberOn(final Function<Branches, ? extends BranchesTerminal<? super NumberExpression, R>> switchMap) {
325+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
326+
}
327+
328+
@Override
329+
public <R extends Expression> R passStringTo(final Function<? super StringExpression, R> f) {
330+
return f.apply(this.assertImplementsAllExpressions());
331+
}
332+
333+
@Override
334+
public <R extends Expression> R switchStringOn(final Function<Branches, ? extends BranchesTerminal<? super StringExpression, R>> switchMap) {
335+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
336+
}
337+
338+
@Override
339+
public <R extends Expression> R passDateTo(final Function<? super DateExpression, R> f) {
340+
return f.apply(this.assertImplementsAllExpressions());
341+
}
342+
343+
@Override
344+
public <R extends Expression> R switchDateOn(final Function<Branches, ? extends BranchesTerminal<? super DateExpression, R>> switchMap) {
345+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
346+
}
347+
348+
@Override
349+
public <R extends Expression> R passArrayTo(final Function<? super ArrayExpression<T>, R> f) {
350+
return f.apply(this.assertImplementsAllExpressions());
351+
}
352+
353+
@Override
354+
public <R extends Expression> R switchArrayOn(final Function<Branches, ? extends BranchesTerminal<? super ArrayExpression<T>, R>> switchMap) {
355+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
356+
}
357+
358+
@Override
359+
public <R extends Expression> R passMapTo(final Function<? super MapExpression<T>, R> f) {
360+
return f.apply(this.assertImplementsAllExpressions());
361+
}
362+
363+
@Override
364+
public <R extends Expression> R switchMapOn(final Function<Branches, ? extends BranchesTerminal<? super MapExpression<T>, R>> switchMap) {
365+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
366+
}
367+
368+
@Override
369+
public <R extends Expression> R passDocumentTo(final Function<? super DocumentExpression, R> f) {
370+
return f.apply(this.assertImplementsAllExpressions());
371+
}
372+
373+
@Override
374+
public <R extends Expression> R switchDocumentOn(final Function<Branches, ? extends BranchesTerminal<? super DocumentExpression, R>> switchMap) {
375+
return switchMapInternal(this.assertImplementsAllExpressions(), switchMap.apply(new Branches()));
376+
}
377+
378+
private <T0 extends Expression, R0 extends Expression> R0 switchMapInternal(
379+
final T0 value, final BranchesTerminal<T0, R0> construct) {
380+
return newMqlExpression((cr) -> {
381+
BsonArray branches = new BsonArray();
382+
for (Function<T0, SwitchCase<R0>> fn : construct.getBranches()) {
383+
SwitchCase<R0> result = fn.apply(value);
384+
branches.add(new BsonDocument()
385+
.append("case", extractBsonValue(cr, result.getCaseValue()))
386+
.append("then", extractBsonValue(cr, result.getThenValue())));
387+
}
388+
BsonDocument switchBson = new BsonDocument().append("branches", branches);
389+
if (construct.getDefaults() != null) {
390+
switchBson = switchBson.append("default", extractBsonValue(cr, construct.getDefaults().apply(value)));
391+
}
392+
return astDoc("$switch", switchBson);
393+
});
394+
}
395+
293396
@Override
294397
public BooleanExpression eq(final Expression eq) {
295398
return new MqlExpression<>(ast("$eq", eq));
@@ -422,32 +525,6 @@ private Function<CodecRegistry, AstPlaceholder> convertInternal(final String to,
422525
.append("to", new BsonString(to)));
423526
}
424527

425-
@Override
426-
public <T0 extends Expression, R0 extends Expression> R0 switchMap(
427-
final Function<Branches, BranchesTerminal<T0, R0>> switchMap) {
428-
T0 value = this.assertImplementsAllExpressions();
429-
BranchesTerminal<T0, R0> construct = switchMap.apply(new Branches());
430-
return switchMapInternal(value, construct);
431-
}
432-
433-
private <T0 extends Expression, R0 extends Expression> R0 switchMapInternal(
434-
final T0 value, final BranchesTerminal<T0, R0> construct) {
435-
return newMqlExpression((cr) -> {
436-
BsonArray branches = new BsonArray();
437-
for (Function<T0, SwitchCase<R0>> fn : construct.getBranches()) {
438-
SwitchCase<R0> result = fn.apply(value);
439-
branches.add(new BsonDocument()
440-
.append("case", extractBsonValue(cr, result.getCaseValue()))
441-
.append("then", extractBsonValue(cr, result.getThenValue())));
442-
}
443-
BsonDocument switchBson = new BsonDocument().append("branches", branches);
444-
if (construct.getDefaults() != null) {
445-
switchBson = switchBson.append("default", extractBsonValue(cr, construct.getDefaults().apply(value)));
446-
}
447-
return astDoc("$switch", switchBson);
448-
});
449-
}
450-
451528
@Override
452529
public IntegerExpression parseInteger() {
453530
Expression asLong = new MqlExpression<>(ast("$toLong"));

driver-core/src/main/com/mongodb/client/model/expressions/NumberExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb.client.model.expressions;
1818

19+
import java.util.function.Function;
20+
1921
/**
2022
* Expresses a numeric value.
2123
*/
@@ -56,4 +58,7 @@ default NumberExpression subtract(final Number subtract) {
5658
NumberExpression abs();
5759

5860
DateExpression millisecondsToDate();
61+
62+
<R extends Expression> R passNumberTo(Function<? super NumberExpression, R> f);
63+
<R extends Expression> R switchNumberOn(Function<Branches, ? extends BranchesTerminal<? super NumberExpression, R>> on);
5964
}

driver-core/src/main/com/mongodb/client/model/expressions/StringExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb.client.model.expressions;
1818

19+
import java.util.function.Function;
20+
1921
import static com.mongodb.client.model.expressions.Expressions.of;
2022

2123
/**
@@ -52,4 +54,7 @@ default StringExpression substrBytes(final int start, final int length) {
5254
DateExpression parseDate(StringExpression format);
5355

5456
DateExpression parseDate(StringExpression timezone, StringExpression format);
57+
58+
<R extends Expression> R passStringTo(Function<? super StringExpression, R> f);
59+
<R extends Expression> R switchStringOn(Function<Branches, ? extends BranchesTerminal<? super StringExpression, R>> on);
5560
}

0 commit comments

Comments
 (0)