Skip to content

Commit 3384440

Browse files
committed
Update and add documentation, add tests, fix minor issues
Rename extractBsonValue Fix access modifiers Remove excess comments Update docs Fix: behaviour of get Add notNull to API, add notNullApi test Fix docs/annotations, tests Fix docs, annotations, since Fix docs Revert external Add missing MqlUnchecked Fix missing null checks Checkstyle
1 parent b602571 commit 3384440

24 files changed

+767
-266
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
* Copyright 2010 The Guava Authors
4+
* Copyright 2011 The Guava Authors
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.mongodb.annotations;
20+
21+
import java.lang.annotation.Documented;
22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* Signifies that the annotated class or interface should be treated as sealed:
29+
* it must not be extended or implemented.
30+
*
31+
* <p>Using such classes and interfaces is no different from using ordinary
32+
* unannotated classes and interfaces.
33+
*
34+
* <p>This annotation does not imply that the API is experimental or
35+
* {@link Beta}, or that the quality or performance of the API is inferior.
36+
*/
37+
@Retention(RetentionPolicy.CLASS)
38+
@Target(ElementType.TYPE)
39+
@Documented
40+
@Sealed
41+
public @interface Sealed {
42+
}

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

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

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

19+
import com.mongodb.annotations.Beta;
20+
import com.mongodb.annotations.Sealed;
21+
1922
import java.util.function.Function;
2023

2124
import static com.mongodb.client.model.expressions.Expressions.of;
@@ -27,7 +30,10 @@
2730
* certain type. It is also known as a finite mathematical sequence.
2831
*
2932
* @param <T> the type of the elements
33+
* @since 4.9.0
3034
*/
35+
@Sealed
36+
@Beta(Beta.Reason.CLIENT)
3137
public interface ArrayExpression<T extends Expression> extends Expression {
3238

3339
/**
@@ -58,15 +64,15 @@ public interface ArrayExpression<T extends Expression> extends Expression {
5864
IntegerExpression size();
5965

6066
/**
61-
* True if any value in {@code this} array satisfies the predicate.
67+
* Whether any value in {@code this} array satisfies the predicate.
6268
*
6369
* @param predicate the predicate.
6470
* @return the resulting value.
6571
*/
6672
BooleanExpression any(Function<? super T, BooleanExpression> predicate);
6773

6874
/**
69-
* True if all values in {@code this} array satisfy the predicate.
75+
* Whether all values in {@code this} array satisfy the predicate.
7076
*
7177
* @param predicate the predicate.
7278
* @return the resulting value.
@@ -79,7 +85,7 @@ public interface ArrayExpression<T extends Expression> extends Expression {
7985
*
8086
* <p>The mapper may be used to transform the values of {@code this} array
8187
* into {@linkplain NumberExpression numbers}. If no transformation is
82-
* necessary, then the identify function {@code array.sum(v -> v)} should
88+
* necessary, then the identity function {@code array.sum(v -> v)} should
8389
* be used.
8490
*
8591
* @param mapper the mapper function.
@@ -93,7 +99,7 @@ public interface ArrayExpression<T extends Expression> extends Expression {
9399
*
94100
* <p>The mapper may be used to transform the values of {@code this} array
95101
* into {@linkplain NumberExpression numbers}. If no transformation is
96-
* necessary, then the identify function {@code array.multiply(v -> v)}
102+
* necessary, then the identity function {@code array.multiply(v -> v)}
97103
* should be used.
98104
*
99105
* @param mapper the mapper function.
@@ -150,7 +156,7 @@ public interface ArrayExpression<T extends Expression> extends Expression {
150156
*
151157
* <p>The mapper may be used to transform the values of {@code this} array
152158
* into {@linkplain StringExpression strings}. If no transformation is
153-
* necessary, then the identify function {@code array.join(v -> v)} should
159+
* necessary, then the identity function {@code array.join(v -> v)} should
154160
* be used.
155161
*
156162
* @param mapper the mapper function.
@@ -159,13 +165,14 @@ public interface ArrayExpression<T extends Expression> extends Expression {
159165
StringExpression join(Function<? super T, StringExpression> mapper);
160166

161167
/**
162-
* The array-concatenation of all the array values of {@code this} array,
168+
* The {@linkplain #concat(ArrayExpression) array-concatenation}
169+
* of all the array values of {@code this} array,
163170
* via the provided {@code mapper}. Returns the empty array if the array
164171
* is empty.
165172
*
166173
* <p>The mapper may be used to transform the values of {@code this} array
167174
* into {@linkplain ArrayExpression arrays}. If no transformation is
168-
* necessary, then the identify function {@code array.concat(v -> v)} should
175+
* necessary, then the identity function {@code array.concat(v -> v)} should
169176
* be used.
170177
*
171178
* @param mapper the mapper function.
@@ -175,13 +182,14 @@ public interface ArrayExpression<T extends Expression> extends Expression {
175182
<R extends Expression> ArrayExpression<R> concat(Function<? super T, ? extends ArrayExpression<? extends R>> mapper);
176183

177184
/**
178-
* The set union of all the array values of {@code this} array,
185+
* The {@linkplain #union(ArrayExpression) set-union}
186+
* of all the array values of {@code this} array,
179187
* via the provided {@code mapper}. Returns the empty array if the array
180188
* is empty.
181189
*
182190
* <p>The mapper may be used to transform the values of {@code this} array
183191
* into {@linkplain ArrayExpression arrays}. If no transformation is
184-
* necessary, then the identify function {@code array.union(v -> v)} should
192+
* necessary, then the identity function {@code array.union(v -> v)} should
185193
* be used.
186194
*
187195
* @param mapper the mapper function.
@@ -193,12 +201,12 @@ public interface ArrayExpression<T extends Expression> extends Expression {
193201
/**
194202
* The {@linkplain MapExpression map} value corresponding to the
195203
* {@linkplain EntryExpression entry} values of {@code this} array,
196-
* via the provided {@code mapper}. Returns the empty array if the array
204+
* via the provided {@code mapper}. Returns the empty map if the array
197205
* is empty.
198206
*
199207
* <p>The mapper may be used to transform the values of {@code this} array
200208
* into {@linkplain EntryExpression entries}. If no transformation is
201-
* necessary, then the identify function {@code array.union(v -> v)} should
209+
* necessary, then the identity function {@code array.union(v -> v)} should
202210
* be used.
203211
*
204212
* @see MapExpression#entrySet()
@@ -215,7 +223,7 @@ public interface ArrayExpression<T extends Expression> extends Expression {
215223
* <p>Warning: The use of this method is an assertion that
216224
* the index {@code i} is in bounds for the array.
217225
* If the index is out of bounds for this array, then
218-
* the behaviour of the API is not defined.
226+
* the behaviour of the API is not specified.
219227
*
220228
* @param i the index.
221229
* @return the resulting value.
@@ -230,7 +238,7 @@ public interface ArrayExpression<T extends Expression> extends Expression {
230238
* <p>Warning: The use of this method is an assertion that
231239
* the index {@code i} is in bounds for the array.
232240
* If the index is out of bounds for this array, then
233-
* the behaviour of the API is not defined.
241+
* the behaviour of the API is not specified.
234242
*
235243
* @param i the index.
236244
* @return the resulting value.
@@ -245,7 +253,7 @@ default T elementAt(final int i) {
245253
*
246254
* <p>Warning: The use of this method is an assertion that
247255
* the array is not empty.
248-
* If the array is empty then the behaviour of the API is not defined.
256+
* If the array is empty then the behaviour of the API is not specified.
249257
*
250258
* @mongodb.server.release 4.4
251259
* @return the resulting value.
@@ -258,15 +266,16 @@ default T elementAt(final int i) {
258266
*
259267
* <p>Warning: The use of this method is an assertion that
260268
* the array is not empty.
261-
* If the array is empty then the behaviour of the API is not defined.
269+
* If the array is empty then the behaviour of the API is not specified.
262270
*
263271
* @mongodb.server.release 4.4
264272
* @return the resulting value.
265273
*/
274+
@MqlUnchecked(PRESENT)
266275
T last();
267276

268277
/**
269-
* True if {@code this} array contains a value that is
278+
* Whether {@code this} array contains a value that is
270279
* {@linkplain #eq equal} to the provided {@code value}.
271280
*
272281
* @param value the value.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@
1616

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

19+
import com.mongodb.annotations.Beta;
20+
import com.mongodb.annotations.Sealed;
21+
1922
import java.util.function.Function;
2023

2124
/**
2225
* A boolean {@linkplain Expression value} in the context of the
2326
* MongoDB Query Language (MQL).
27+
*
28+
* @since 4.9.0
2429
*/
30+
@Sealed
31+
@Beta(Beta.Reason.CLIENT)
2532
public interface BooleanExpression extends Expression {
2633

2734
/**

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

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

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

19+
import com.mongodb.annotations.Beta;
20+
import com.mongodb.assertions.Assertions;
21+
1922
import java.util.ArrayList;
2023
import java.util.List;
2124
import java.util.function.Function;
@@ -28,10 +31,12 @@
2831
* to succeed will produce the value that it specifies. If no check succeeds,
2932
* then the operation
3033
* {@linkplain BranchesIntermediary#defaults(Function) defaults} to a default
31-
* value, or if none is specified, the operation causes an error.
34+
* value, or if none is specified, the operation will cause an error.
3235
*
3336
* @param <T> the type of the values that may be checked.
37+
* @since 4.9.0
3438
*/
39+
@Beta(Beta.Reason.CLIENT)
3540
public final class Branches<T extends Expression> {
3641

3742
Branches() {
@@ -59,6 +64,8 @@ private static <T extends Expression> MqlExpression<?> mqlEx(final T value) {
5964
* @return the appended sequence of checks.
6065
*/
6166
public <R extends Expression> BranchesIntermediary<T, R> is(final Function<? super T, BooleanExpression> predicate, final Function<? super T, ? extends R> mapping) {
67+
Assertions.notNull("predicate", predicate);
68+
Assertions.notNull("mapping", mapping);
6269
return with(value -> new SwitchCase<>(predicate.apply(value), mapping.apply(value)));
6370
}
6471

@@ -74,6 +81,8 @@ public <R extends Expression> BranchesIntermediary<T, R> is(final Function<? sup
7481
* @return the appended sequence of checks.
7582
*/
7683
public <R extends Expression> BranchesIntermediary<T, R> eq(final T v, final Function<? super T, ? extends R> mapping) {
84+
Assertions.notNull("v", v);
85+
Assertions.notNull("mapping", mapping);
7786
return is(value -> value.eq(v), mapping);
7887
}
7988

@@ -89,6 +98,8 @@ public <R extends Expression> BranchesIntermediary<T, R> eq(final T v, final Fun
8998
* @return the appended sequence of checks.
9099
*/
91100
public <R extends Expression> BranchesIntermediary<T, R> lt(final T v, final Function<? super T, ? extends R> mapping) {
101+
Assertions.notNull("v", v);
102+
Assertions.notNull("mapping", mapping);
92103
return is(value -> value.lt(v), mapping);
93104
}
94105

@@ -104,6 +115,8 @@ public <R extends Expression> BranchesIntermediary<T, R> lt(final T v, final Fun
104115
* @return the appended sequence of checks.
105116
*/
106117
public <R extends Expression> BranchesIntermediary<T, R> lte(final T v, final Function<? super T, ? extends R> mapping) {
118+
Assertions.notNull("v", v);
119+
Assertions.notNull("mapping", mapping);
107120
return is(value -> value.lte(v), mapping);
108121
}
109122

@@ -119,12 +132,13 @@ public <R extends Expression> BranchesIntermediary<T, R> lte(final T v, final Fu
119132
* @param <R> the type of the produced value.
120133
*/
121134
public <R extends Expression> BranchesIntermediary<T, R> isBoolean(final Function<? super BooleanExpression, ? extends R> mapping) {
135+
Assertions.notNull("mapping", mapping);
122136
return is(v -> mqlEx(v).isBoolean(), v -> mapping.apply((BooleanExpression) v));
123137
}
124138

125139
/**
126140
* A successful check for
127-
* {@linkplain Expression#isBooleanOr(BooleanExpression) being a boolean}
141+
* {@linkplain Expression#isNumberOr(NumberExpression) being a number}
128142
* produces a value specified by the {@code mapping}.
129143
*
130144
* @mongodb.server.release 4.4
@@ -133,6 +147,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isBoolean(final Functio
133147
* @param <R> the type of the produced value.
134148
*/
135149
public <R extends Expression> BranchesIntermediary<T, R> isNumber(final Function<? super NumberExpression, ? extends R> mapping) {
150+
Assertions.notNull("mapping", mapping);
136151
return is(v -> mqlEx(v).isNumber(), v -> mapping.apply((NumberExpression) v));
137152
}
138153

@@ -147,6 +162,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isNumber(final Function
147162
* @param <R> the type of the produced value.
148163
*/
149164
public <R extends Expression> BranchesIntermediary<T, R> isInteger(final Function<? super IntegerExpression, ? extends R> mapping) {
165+
Assertions.notNull("mapping", mapping);
150166
return is(v -> mqlEx(v).isInteger(), v -> mapping.apply((IntegerExpression) v));
151167
}
152168

@@ -160,6 +176,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isInteger(final Functio
160176
* @param <R> the type of the produced value.
161177
*/
162178
public <R extends Expression> BranchesIntermediary<T, R> isString(final Function<? super StringExpression, ? extends R> mapping) {
179+
Assertions.notNull("mapping", mapping);
163180
return is(v -> mqlEx(v).isString(), v -> mapping.apply((StringExpression) v));
164181
}
165182

@@ -173,6 +190,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isString(final Function
173190
* @param <R> the type of the produced value.
174191
*/
175192
public <R extends Expression> BranchesIntermediary<T, R> isDate(final Function<? super DateExpression, ? extends R> mapping) {
193+
Assertions.notNull("mapping", mapping);
176194
return is(v -> mqlEx(v).isDate(), v -> mapping.apply((DateExpression) v));
177195
}
178196

@@ -192,33 +210,33 @@ public <R extends Expression> BranchesIntermediary<T, R> isDate(final Function<?
192210
*/
193211
@SuppressWarnings("unchecked")
194212
public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> isArray(final Function<? super ArrayExpression<@MqlUnchecked(TYPE_ARGUMENT) Q>, ? extends R> mapping) {
213+
Assertions.notNull("mapping", mapping);
195214
return is(v -> mqlEx(v).isArray(), v -> mapping.apply((ArrayExpression<Q>) v));
196215
}
197216

198217
/**
199218
* A successful check for
200219
* {@linkplain Expression#isDocumentOr(DocumentExpression) being a document}
220+
* (or document-like value, see
221+
* {@link MapExpression} and {@link EntryExpression})
201222
* produces a value specified by the {@code mapping}.
202223
*
203-
* <p>Note: Any value considered to be a document by this API
204-
* will also be considered a map, and vice-versa.
205-
*
206224
* @param mapping the mapping.
207225
* @return the appended sequence of checks.
208226
* @param <R> the type of the produced value.
209227
*/
210228
public <R extends Expression> BranchesIntermediary<T, R> isDocument(final Function<? super DocumentExpression, ? extends R> mapping) {
229+
Assertions.notNull("mapping", mapping);
211230
return is(v -> mqlEx(v).isDocumentOrMap(), v -> mapping.apply((DocumentExpression) v));
212231
}
213232

214233
/**
215234
* A successful check for
216235
* {@linkplain Expression#isMapOr(MapExpression) being a map}
236+
* (or map-like value, see
237+
* {@link DocumentExpression} and {@link EntryExpression})
217238
* produces a value specified by the {@code mapping}.
218239
*
219-
* <p>Note: Any value considered to be a map by this API
220-
* will also be considered a document, and vice-versa.
221-
*
222240
* <p>Warning: The type argument of the map is not
223241
* enforced by the API. The use of this method is an
224242
* unchecked assertion that the type argument is correct.
@@ -230,6 +248,7 @@ public <R extends Expression> BranchesIntermediary<T, R> isDocument(final Functi
230248
*/
231249
@SuppressWarnings("unchecked")
232250
public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> isMap(final Function<? super MapExpression<@MqlUnchecked(TYPE_ARGUMENT) Q>, ? extends R> mapping) {
251+
Assertions.notNull("mapping", mapping);
233252
return is(v -> mqlEx(v).isDocumentOrMap(), v -> mapping.apply((MapExpression<Q>) v));
234253
}
235254

@@ -243,6 +262,7 @@ public <R extends Expression, Q extends Expression> BranchesIntermediary<T, R> i
243262
* @param <R> the type of the produced value.
244263
*/
245264
public <R extends Expression> BranchesIntermediary<T, R> isNull(final Function<? super Expression, ? extends R> mapping) {
265+
Assertions.notNull("mapping", mapping);
246266
return is(v -> mqlEx(v).isNull(), v -> mapping.apply(v));
247267
}
248268
}

0 commit comments

Comments
 (0)