Skip to content

Commit 347ab45

Browse files
stIncMalekatcharov
authored andcommitted
Add @MqlUnchecked and a few usage examples (#1059)
JAVA-4799
1 parent 1d9964b commit 347ab45

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
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
@@ -19,6 +19,8 @@
1919
import java.util.function.Function;
2020

2121
import static com.mongodb.client.model.expressions.Expressions.of;
22+
import static com.mongodb.client.model.expressions.MqlUnchecked.Unchecked.PRESENT;
23+
import static com.mongodb.client.model.expressions.MqlUnchecked.Unchecked.NON_EMPTY;
2224

2325
/**
2426
* An array {@link Expression value} in the context of the MongoDB Query
@@ -85,6 +87,7 @@ public interface ArrayExpression<T extends Expression> extends Expression {
8587
* @param i
8688
* @return
8789
*/
90+
@MqlUnchecked(PRESENT)
8891
T elementAt(IntegerExpression i);
8992

9093
default T elementAt(final int i) {
@@ -95,6 +98,7 @@ default T elementAt(final int i) {
9598
* user asserts that array is not empty
9699
* @return
97100
*/
101+
@MqlUnchecked(NON_EMPTY)
98102
T first();
99103

100104
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121
import java.util.function.Function;
2222

23+
import static com.mongodb.client.model.expressions.MqlUnchecked.Unchecked.TYPE_ARGUMENT;
24+
2325
public final class Branches<T extends Expression> {
2426

2527
Branches() {
@@ -78,7 +80,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isDate(final Function<?
7880
}
7981

8082
@SuppressWarnings("unchecked")
81-
public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> isArray(final Function<? super ArrayExpression<Q>, ? extends R> r) {
83+
public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> isArray(final Function<? super ArrayExpression<@MqlUnchecked(TYPE_ARGUMENT) Q>, ? extends R> r) {
8284
return is(v -> mqlEx(v).isArray(), v -> r.apply((ArrayExpression<Q>) v));
8385
}
8486

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

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

2424
import static com.mongodb.client.model.expressions.Expressions.of;
2525
import static com.mongodb.client.model.expressions.Expressions.ofMap;
26+
import static com.mongodb.client.model.expressions.MqlUnchecked.Unchecked.PRESENT;
27+
import static com.mongodb.client.model.expressions.MqlUnchecked.Unchecked.TYPE;
2628

2729
/**
2830
* Expresses a document value. A document is an ordered set of fields, where the
@@ -34,8 +36,10 @@ public interface DocumentExpression extends Expression {
3436

3537
DocumentExpression unsetField(String fieldName);
3638

39+
@MqlUnchecked(PRESENT)
3740
Expression getField(String fieldName);
3841

42+
@MqlUnchecked({PRESENT, TYPE})
3943
BooleanExpression getBoolean(String fieldName);
4044

4145
BooleanExpression getBoolean(String fieldName, BooleanExpression other);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import java.util.function.Function;
2222

23+
import static com.mongodb.client.model.expressions.MqlUnchecked.Unchecked.TYPE_ARGUMENT;
24+
2325
/**
2426
* A value in the context of the MongoDB Query Language (MQL).
2527
*
@@ -215,7 +217,7 @@ public interface Expression {
215217
* @return the resulting value.
216218
* @param <T> the type of the elements of the resulting array.
217219
*/
218-
<T extends Expression> ArrayExpression<T> isArrayOr(ArrayExpression<? extends T> other);
220+
<T extends Expression> ArrayExpression<@MqlUnchecked(TYPE_ARGUMENT) T> isArrayOr(ArrayExpression<? extends T> other);
219221

220222
/**
221223
* {@code this} value as a {@linkplain DocumentExpression document} if

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.function.Function;
2020

2121
import static com.mongodb.client.model.expressions.Expressions.of;
22+
import static com.mongodb.client.model.expressions.MqlUnchecked.Unchecked.PRESENT;
2223

2324
public interface MapExpression<T extends Expression> extends Expression {
2425

@@ -29,6 +30,7 @@ default BooleanExpression has(String key) {
2930
}
3031

3132
// TODO-END doc "user asserts"
33+
@MqlUnchecked(PRESENT)
3234
T get(StringExpression key);
3335

3436
// TODO-END doc "user asserts"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.mongodb.client.model.expressions;
17+
18+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Documents places where the API relies on a user asserting
26+
* something that is not checked at run-time.
27+
* If the assertion turns out to be false, the API behavior is unspecified.
28+
*
29+
* <p>This class is not part of the public API and may be removed or changed at any time</p>
30+
*/
31+
@Documented
32+
@Retention(RetentionPolicy.SOURCE)
33+
@Target({ElementType.METHOD, ElementType.TYPE_USE})
34+
public @interface MqlUnchecked {
35+
/**
36+
* @return A hint on the user assertion the API relies on.
37+
*/
38+
Unchecked[] value();
39+
40+
/**
41+
* @see MqlUnchecked#value()
42+
*/
43+
enum Unchecked {
44+
/**
45+
* The API relies on the values it encounters being of the type
46+
* implied/specified by or inferred from the user code.
47+
* For example, {@link com.mongodb.client.model.expressions.DocumentExpression#getBoolean(String)}
48+
* relies on the values of the document field being of the
49+
* {@linkplain com.mongodb.client.model.expressions.BooleanExpression boolean} type.
50+
*/
51+
TYPE,
52+
/**
53+
* The API checks the raw type, but relies on the type argument
54+
* implied/specified by or inferred from the user code being correct.
55+
* For example, {@link com.mongodb.client.model.expressions.Expression#isArrayOr(ArrayExpression)}
56+
* checks that the value is of the
57+
* {@linkplain com.mongodb.client.model.expressions.ArrayExpression array} raw type,
58+
* but relies on the elements of the array being of the type derived from the user code.
59+
*
60+
* <p>One may think of it as a more specific version of {@link #TYPE}.</p>
61+
*/
62+
TYPE_ARGUMENT,
63+
/**
64+
* The API relies on the array being non-empty.
65+
* For example, {@link com.mongodb.client.model.expressions.ArrayExpression#first()}.
66+
*/
67+
NON_EMPTY,
68+
/**
69+
* The API relies on the element identified by index, name, etc., being present in the
70+
* {@linkplain com.mongodb.client.model.expressions.DocumentExpression document} involved.
71+
* For example, {@link com.mongodb.client.model.expressions.DocumentExpression#getField(String)}.
72+
*/
73+
PRESENT,
74+
}
75+
}

0 commit comments

Comments
 (0)