Skip to content

Commit 6588406

Browse files
committed
Update naming, terms, and missing checks and annotations (#1073)
JAVA-3879
1 parent 6bbb231 commit 6588406

File tree

17 files changed

+53
-86
lines changed

17 files changed

+53
-86
lines changed

driver-core/src/main/com/mongodb/client/model/mql/MqlArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public interface MqlArray<T extends MqlValue> extends MqlValue {
209209
* necessary, then the identity function {@code array.union(v -> v)} should
210210
* be used.
211211
*
212-
* @see MqlMap#entrySet()
212+
* @see MqlMap#entries()
213213
* @param mapper the mapper function.
214214
* @return the resulting value.
215215
* @param <R> the type of the resulting map's values.

driver-core/src/main/com/mongodb/client/model/mql/MqlBoolean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public interface MqlBoolean extends MqlValue {
6161
* @param ifTrue the ifTrue value.
6262
* @param ifFalse the ifFalse value.
6363
* @return the resulting value.
64-
* @param <T> The type of the resulting expression.
64+
* @param <T> The type of the resulting value.
6565
*/
6666
<T extends MqlValue> T cond(T ifTrue, T ifFalse);
6767

driver-core/src/main/com/mongodb/client/model/mql/MqlDocument.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ default MqlBoolean getBoolean(final String fieldName, final boolean other) {
186186
default MqlNumber getNumber(final String fieldName, final Number other) {
187187
Assertions.notNull("fieldName", fieldName);
188188
Assertions.notNull("other", other);
189-
return getNumber(fieldName, MqlValues.numberToExpression(other));
189+
return getNumber(fieldName, MqlValues.numberToMqlNumber(other));
190190
}
191191

192192
/**
@@ -521,7 +521,7 @@ default MqlDocument getDocument(final String fieldName, final Bson other) {
521521
* @param <T> the type.
522522
*/
523523

524-
<T extends MqlValue> MqlMap<T> asMap();
524+
<T extends MqlValue> MqlMap<@MqlUnchecked(TYPE_ARGUMENT) T> asMap();
525525

526526
/**
527527
* The result of passing {@code this} value to the provided function.

driver-core/src/main/com/mongodb/client/model/mql/MqlEntry.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.mongodb.annotations.Beta;
2020
import com.mongodb.annotations.Sealed;
2121

22-
import static com.mongodb.client.model.mql.MqlValues.of;
23-
2422
/**
2523
* A map entry {@linkplain MqlValue value} in the context
2624
* of the MongoDB Query Language (MQL). An entry has a
@@ -74,16 +72,4 @@ public interface MqlEntry<T extends MqlValue> extends MqlValue {
7472
* @return the resulting entry.
7573
*/
7674
MqlEntry<T> setKey(MqlString key);
77-
78-
/**
79-
* An entry with the same value as {@code this} entry, and the
80-
* specified {@code key}.
81-
*
82-
* @mongodb.server.release 5.0
83-
* @param key the key.
84-
* @return the resulting entry.
85-
*/
86-
default MqlEntry<T> setKey(final String key) {
87-
return setKey(of(key));
88-
}
8975
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ final class MqlExpression<T extends MqlValue>
4343
}
4444

4545
/**
46-
* Exposes the evaluated BsonValue so that expressions may be used in
47-
* aggregations. Non-public, as it is intended to be used only by the
46+
* Exposes the evaluated BsonValue so that this mql expression may be used
47+
* in aggregations. Non-public, as it is intended to be used only by the
4848
* {@link MqlExpressionCodec}.
4949
*/
5050
BsonValue toBsonValue(final CodecRegistry codecRegistry) {
@@ -67,11 +67,13 @@ public T getValue() {
6767

6868
@Override
6969
public MqlEntry<T> setValue(final T value) {
70+
Assertions.notNull("value", value);
7071
return setFieldInternal("v", value);
7172
}
7273

7374
@Override
7475
public MqlEntry<T> setKey(final MqlString key) {
76+
Assertions.notNull("key", key);
7577
return setFieldInternal("k", key);
7678
}
7779

@@ -1081,7 +1083,7 @@ public MqlMap<T> merge(final MqlMap<? extends T> other) {
10811083
}
10821084

10831085
@Override
1084-
public MqlArray<MqlEntry<T>> entrySet() {
1086+
public MqlArray<MqlEntry<T>> entries() {
10851087
return newMqlExpression(ast("$objectToArray"));
10861088
}
10871089

driver-core/src/main/com/mongodb/client/model/mql/MqlExpressionCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class MqlExpressionCodec implements Codec<MqlExpression> {
3434

3535
@Override
3636
public MqlExpression decode(final BsonReader reader, final DecoderContext decoderContext) {
37-
throw new UnsupportedOperationException("Decoding to an expression is not supported");
37+
throw new UnsupportedOperationException("Decoding to an MqlExpression is not supported");
3838
}
3939

4040
@Override

driver-core/src/main/com/mongodb/client/model/mql/MqlMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public interface MqlMap<T extends MqlValue> extends MqlValue {
5555
* @return the resulting value.
5656
*/
5757
default MqlBoolean has(final String key) {
58+
Assertions.notNull("key", key);
5859
return has(of(key));
5960
}
6061

@@ -188,7 +189,7 @@ default MqlMap<T> unset(final String key) {
188189
* @see MqlArray#asMap
189190
* @return the resulting value.
190191
*/
191-
MqlArray<MqlEntry<T>> entrySet();
192+
MqlArray<MqlEntry<T>> entries();
192193

193194
/**
194195
* {@code this} map as a {@linkplain MqlDocument document}.

driver-core/src/main/com/mongodb/client/model/mql/MqlNumber.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public interface MqlNumber extends MqlValue {
5050
*/
5151
default MqlNumber multiply(final Number other) {
5252
Assertions.notNull("other", other);
53-
return this.multiply(MqlValues.numberToExpression(other));
53+
return this.multiply(MqlValues.numberToMqlNumber(other));
5454
}
5555

5656
/**
@@ -73,7 +73,7 @@ default MqlNumber multiply(final Number other) {
7373
*/
7474
default MqlNumber divide(final Number other) {
7575
Assertions.notNull("other", other);
76-
return this.divide(MqlValues.numberToExpression(other));
76+
return this.divide(MqlValues.numberToMqlNumber(other));
7777
}
7878

7979
/**
@@ -92,7 +92,7 @@ default MqlNumber divide(final Number other) {
9292
*/
9393
default MqlNumber add(final Number other) {
9494
Assertions.notNull("other", other);
95-
return this.add(MqlValues.numberToExpression(other));
95+
return this.add(MqlValues.numberToMqlNumber(other));
9696
}
9797

9898
/**
@@ -111,7 +111,7 @@ default MqlNumber add(final Number other) {
111111
*/
112112
default MqlNumber subtract(final Number other) {
113113
Assertions.notNull("other", other);
114-
return this.subtract(MqlValues.numberToExpression(other));
114+
return this.subtract(MqlValues.numberToMqlNumber(other));
115115
}
116116

117117
/**

driver-core/src/main/com/mongodb/client/model/mql/MqlString.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public interface MqlString extends MqlValue {
4848
MqlString toUpper();
4949

5050
/**
51-
* The concatenation of {@code this} string, followed by
52-
* the {@code other} string.
51+
* The result of appending the {@code other} string to the end of
52+
* {@code this} string (strict concatenation).
5353
*
5454
* @param other the other value.
5555
* @return the resulting value.

driver-core/src/main/com/mongodb/client/model/mql/MqlValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* filtered and summed, in a style similar to that of the Java Stream API:
3939
*
4040
* <pre>{@code
41-
* import static com.mongodb.client.model.expressions.Expressions.current;
41+
* import static com.mongodb.client.model.mql.MqlValues.current;
4242
* MongoCollection<Document> col = ...;
4343
* AggregateIterable<Document> result = col.aggregate(Arrays.asList(
4444
* addFields(new Field<>("result", current()
@@ -254,7 +254,7 @@ public interface MqlValue {
254254
* @return the resulting value.
255255
* @param <T> the type of the values of the resulting map.
256256
*/
257-
<T extends MqlValue> MqlMap<T> isMapOr(MqlMap<@MqlUnchecked(TYPE_ARGUMENT) ? extends T> other);
257+
<T extends MqlValue> MqlMap<@MqlUnchecked(TYPE_ARGUMENT) T> isMapOr(MqlMap<? extends T> other);
258258

259259
/**
260260
* The {@linkplain MqlString string} representation of {@code this} value.

driver-core/src/main/com/mongodb/client/model/mql/MqlValues.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,7 @@ public static <T extends MqlValue> MqlMap<T> ofMap() {
368368
*/
369369
public static MqlDocument of(final Bson document) {
370370
Assertions.notNull("document", document);
371-
// All documents are wrapped in a $literal. If we don't wrap, we need to
372-
// check for empty documents and documents that are actually expressions
373-
// (and need to be wrapped in $literal anyway). This would be brittle.
371+
// All documents are wrapped in a $literal; this is the least brittle approach.
374372
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonDocument("$literal",
375373
document.toBsonDocument(BsonDocument.class, cr))));
376374
}
@@ -391,13 +389,13 @@ public static MqlDocument of(final Bson document) {
391389
* @return the null value
392390
*/
393391
public static MqlValue ofNull() {
394-
// There is no specific expression type corresponding to Null,
395-
// and Null is not a value in any other expression type.
392+
// There is no specific mql type corresponding to Null,
393+
// and Null is not a value in any other mql type.
396394
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonNull()))
397395
.assertImplementsAllExpressions();
398396
}
399397

400-
static MqlNumber numberToExpression(final Number number) {
398+
static MqlNumber numberToMqlNumber(final Number number) {
401399
Assertions.notNull("number", number);
402400
if (number instanceof Integer) {
403401
return of((int) number);

driver-core/src/test/functional/com/mongodb/client/model/mql/ArithmeticMqlValuesFunctionalTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.math.RoundingMode;
2424

2525
import static com.mongodb.ClusterFixture.serverVersionAtLeast;
26-
import static com.mongodb.client.model.mql.MqlValues.numberToExpression;
26+
import static com.mongodb.client.model.mql.MqlValues.numberToMqlNumber;
2727
import static com.mongodb.client.model.mql.MqlValues.of;
2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -55,12 +55,12 @@ public void literalsTest() {
5555
assertNotEquals(toBsonValue(1.0), evaluate(of(1L)));
5656

5757
// Number conversions; used internally
58-
assertExpression(1, numberToExpression(1));
59-
assertExpression(1L, numberToExpression(1L));
60-
assertExpression(1.0, numberToExpression(1.0));
61-
assertExpression(Decimal128.parse("1.0"), numberToExpression(Decimal128.parse("1.0")));
58+
assertExpression(1, numberToMqlNumber(1));
59+
assertExpression(1L, numberToMqlNumber(1L));
60+
assertExpression(1.0, numberToMqlNumber(1.0));
61+
assertExpression(Decimal128.parse("1.0"), numberToMqlNumber(Decimal128.parse("1.0")));
6262
assertThrows(IllegalArgumentException.class,
63-
() -> assertExpression("n/a", numberToExpression(BigDecimal.valueOf(1))));
63+
() -> assertExpression("n/a", numberToMqlNumber(BigDecimal.valueOf(1))));
6464
}
6565

6666
@Test

driver-core/src/test/functional/com/mongodb/client/model/mql/BooleanMqlValuesFunctionalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
@SuppressWarnings({"PointlessBooleanExpression", "ConstantConditions", "ConstantConditionalExpression"})
21+
@SuppressWarnings({"PointlessBooleanExpression", "ConstantConditions", "ConstantConditionalExpression", "SimplifyBooleanExpression"})
2222
class BooleanMqlValuesFunctionalTest extends AbstractMqlValuesFunctionalTest {
2323
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/#boolean-expression-operators
2424
// (Complete as of 6.0)

driver-core/src/test/functional/com/mongodb/client/model/mql/ControlMqlValuesFunctionalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void switchTypesTest() {
140140
assertExpression(
141141
"12 - map",
142142
ofMap(Document.parse("{a: '1', b: '2'}")).switchOn(on -> on
143-
.isMap((MqlMap<MqlString> v) -> v.entrySet()
143+
.isMap((MqlMap<MqlString> v) -> v.entries()
144144
.joinStrings(e -> e.getValue()).append(of(" - map")))));
145145
// arrays via isArray, and tests signature:
146146
assertExpression(

driver-core/src/test/functional/com/mongodb/client/model/mql/MapMqlValuesFunctionalTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ public void getSetEntryTest() {
109109
assertExpression(
110110
Document.parse("{k: 'keyB', 'v': 1}"),
111111
entryA1.setKey(of("keyB")));
112-
assertExpression(
113-
Document.parse("{k: 'keyB', 'v': 1}"),
114-
entryA1.setKey("keyB"));
115112
}
116113

117114
@Test
@@ -159,23 +156,23 @@ public void entrySetTest() {
159156
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/objectToArray/ (23)
160157
assertExpression(
161158
Arrays.asList(Document.parse("{'k': 'k1', 'v': 1}")),
162-
MqlValues.<MqlInteger>ofMap().set("k1", of(1)).entrySet(),
159+
MqlValues.<MqlInteger>ofMap().set("k1", of(1)).entries(),
163160
"{'$objectToArray': {'$setField': "
164161
+ "{'field': 'k1', 'input': {'$literal': {}}, 'value': 1}}}");
165162

166163
// key/value usage
167164
assertExpression(
168165
"keyA|keyB|",
169-
mapA1B2.entrySet().map(v -> v.getKey().append(of("|"))).joinStrings(v -> v));
166+
mapA1B2.entries().map(v -> v.getKey().append(of("|"))).joinStrings(v -> v));
170167
assertExpression(
171168
23,
172-
mapA1B2.entrySet().map(v -> v.getValue().add(10)).sum(v -> v));
169+
mapA1B2.entries().map(v -> v.getValue().add(10)).sum(v -> v));
173170

174171
// combined entrySet-buildMap usage
175172
assertExpression(
176173
Document.parse("{'keyA': 2, 'keyB': 3}"),
177174
mapA1B2
178-
.entrySet()
175+
.entries()
179176
.map(v -> v.setValue(v.getValue().add(1)))
180177
.asMap(v -> v));
181178

@@ -185,7 +182,7 @@ public void entrySetTest() {
185182
Arrays.asList(
186183
Document.parse("{'k': 'warehouse1', 'v': 2500}"),
187184
Document.parse("{'k': 'warehouse2', 'v': 500}")),
188-
doc.getMap("instock").entrySet(),
185+
doc.getMap("instock").entries(),
189186
"{'$objectToArray': {'$getField': {'input': {'$literal': "
190187
+ "{'instock': {'warehouse1': 2500, 'warehouse2': 500}}}, 'field': 'instock'}}}");
191188
}

driver-core/src/test/functional/com/mongodb/client/model/mql/NotNullApiTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static com.mongodb.assertions.Assertions.fail;
3636
import static com.mongodb.client.model.mql.MqlValues.of;
3737
import static com.mongodb.client.model.mql.MqlValues.ofArray;
38+
import static com.mongodb.client.model.mql.MqlValues.ofEntry;
3839
import static com.mongodb.client.model.mql.MqlValues.ofMap;
3940
import static com.mongodb.client.model.mql.MqlValues.ofNull;
4041

@@ -56,6 +57,7 @@ public void notNullApiTest() {
5657
mapping.put(MqlMap.class, ofMap(BsonDocument.parse("{}")));
5758
mapping.put(MqlArray.class, ofArray());
5859
mapping.put(MqlValue.class, ofNull());
60+
mapping.put(MqlEntry.class, ofEntry(of(""), of("")));
5961
mapping.put(Branches.class, new Branches<>());
6062
mapping.put(BranchesIntermediary.class, new BranchesIntermediary<>(Collections.emptyList()));
6163
mapping.put(BranchesTerminal.class, new BranchesTerminal<>(Collections.emptyList(), null));

0 commit comments

Comments
 (0)