Skip to content

Commit 1a776bf

Browse files
committed
Fixes
1 parent acda629 commit 1a776bf

File tree

5 files changed

+50
-34
lines changed

5 files changed

+50
-34
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.time.Instant;
3333
import java.util.ArrayList;
3434
import java.util.Arrays;
35-
import java.util.Date;
3635
import java.util.List;
3736
import java.util.stream.Collectors;
3837

@@ -84,9 +83,7 @@ public static DateExpression of(final Instant of) {
8483
* @return the string expression
8584
*/
8685
public static StringExpression of(final String of) {
87-
if (of == null) {
88-
throw new IllegalArgumentException("string cannot be null");
89-
}
86+
Assertions.notNull("String", of);
9087
return new MqlExpression<>((codecRegistry) -> new BsonString(of));
9188
}
9289

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import java.util.function.BinaryOperator;
2626
import java.util.function.Function;
2727

28-
import static com.mongodb.client.model.expressions.Expressions.of;
29-
3028
final class MqlExpression<T extends Expression>
3129
implements Expression, BooleanExpression, IntegerExpression, NumberExpression,
3230
StringExpression, DateExpression, DocumentExpression, ArrayExpression<T> {
@@ -206,17 +204,17 @@ public IntegerExpression strLen() {
206204
}
207205

208206
@Override
209-
public Expression strLenBytes() {
207+
public IntegerExpression strLenBytes() {
210208
return new MqlExpression<>(ast("$strLenBytes"));
211209
}
212210

213211
@Override
214-
public Expression substr(final int start, final int length) {
215-
return new MqlExpression<>(ast("$substrCP", of(start), of(length)));
212+
public StringExpression substr(final IntegerExpression start, final IntegerExpression length) {
213+
return new MqlExpression<>(ast("$substrCP", start, length));
216214
}
217215

218216
@Override
219-
public Expression substrBytes(final int start, final int length) {
220-
return new MqlExpression<>(ast("$substrBytes", of(start), of(length)));
217+
public StringExpression substrBytes(final IntegerExpression start, final IntegerExpression length) {
218+
return new MqlExpression<>(ast("$substrBytes", start, length));
221219
}
222220
}

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

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

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

19+
import static com.mongodb.client.model.expressions.Expressions.of;
20+
1921
/**
2022
* Expresses a string value.
2123
*/
@@ -25,13 +27,21 @@ public interface StringExpression extends Expression {
2527

2628
StringExpression toUpper();
2729

28-
Expression concat(StringExpression concat);
30+
StringExpression concat(StringExpression concat);
31+
32+
IntegerExpression strLen();
33+
34+
IntegerExpression strLenBytes();
2935

30-
Expression strLen();
36+
StringExpression substr(IntegerExpression start, IntegerExpression length);
3137

32-
Expression strLenBytes();
38+
default StringExpression substr(int start, int length) {
39+
return this.substr(of(start), of(length));
40+
}
3341

34-
Expression substr(int start, int length);
42+
StringExpression substrBytes(IntegerExpression start, IntegerExpression length);
3543

36-
Expression substrBytes(int start, int length);
44+
default StringExpression substrBytes(int start, int length) {
45+
return this.substrBytes(of(start), of(length));
46+
}
3747
}

driver-core/src/test/functional/com/mongodb/client/model/expressions/ComparisonExpressionsFunctionalTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.time.Instant;
2626
import java.util.Arrays;
27-
import java.util.Date;
2827
import java.util.List;
2928

3029
import static com.mongodb.client.model.expressions.Expressions.of;

driver-core/src/test/functional/com/mongodb/client/model/expressions/StringExpressionsFunctionalTest.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@
2828
class StringExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest {
2929
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/#string-expression-operators
3030

31-
private final String sushi = "\u5BFF\u53F8";
3231
private final String jalapeno = "jalape\u00F1o";
32+
private final String sushi = "\u5BFF\u53F8";
33+
private final String fish = "\uD83D\uDC1F";
3334

3435
@Test
3536
public void literalsTest() {
3637
assertExpression("", of(""), "''");
3738
assertExpression("abc", of("abc"), "'abc'");
3839
assertThrows(IllegalArgumentException.class, () -> of((String) null));
39-
assertExpression(sushi, of(sushi), "'" + sushi + "'");
40+
assertExpression(fish, of(fish), "'" + fish + "'");
4041
}
4142

4243
@Test
@@ -69,21 +70,24 @@ public void toUpperTest() {
6970
@Test
7071
public void strLenTest() {
7172
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenCP/ (?)
72-
// TODO naming: just strLen, lengthCP, lengthCodePoints, stringLengthInCodePoints?...
7373
assertExpression(
74-
"abc".length(),
74+
"abc".codePointCount(0, 3),
7575
of("abc").strLen(),
7676
"{'$strLenCP': 'abc'}");
7777

7878
// unicode
7979
assertExpression(
80-
jalapeno.length(),
80+
jalapeno.codePointCount(0, jalapeno.length()),
8181
of(jalapeno).strLen(),
8282
"{'$strLenCP': '" + jalapeno + "'}");
8383
assertExpression(
84-
sushi.length(),
84+
sushi.codePointCount(0, sushi.length()),
8585
of(sushi).strLen(),
8686
"{'$strLenCP': '" + sushi + "'}");
87+
assertExpression(
88+
fish.codePointCount(0, fish.length()),
89+
of(fish).strLen(),
90+
"{'$strLenCP': '" + fish + "'}");
8791
}
8892

8993
@Test
@@ -103,35 +107,40 @@ public void strLenBytesTest() {
103107
sushi.getBytes(StandardCharsets.UTF_8).length,
104108
of(sushi).strLenBytes(),
105109
"{'$strLenBytes': '" + sushi + "'}");
110+
assertExpression(
111+
fish.getBytes(StandardCharsets.UTF_8).length,
112+
of(fish).strLenBytes(),
113+
"{'$strLenBytes': '" + fish + "'}");
106114

107115
// comparison
108-
assertExpression(2, of(sushi).strLen());
109-
assertExpression(6, of(sushi).strLenBytes());
110116
assertExpression(8, of(jalapeno).strLen());
111117
assertExpression(9, of(jalapeno).strLenBytes());
118+
assertExpression(2, of(sushi).strLen());
119+
assertExpression(6, of(sushi).strLenBytes());
120+
assertExpression(1, of(fish).strLen());
121+
assertExpression(4, of(fish).strLenBytes());
112122
}
113123

114124
@Test
115125
public void substrTest() {
116126
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/substr/
117-
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/substrBytes/ (?)
118127
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/substrCP/ (?)
119128
// substr is deprecated, an alias for bytes
120-
// TODO here, it is an alias for code-points
121129
assertExpression(
122130
"abc".substring(1, 1 + 1),
123-
of("abc").substr(1, 1),
131+
of("abc").substr(of(1), of(1)),
124132
"{'$substrCP': ['abc', 1, 1]}");
125133

126134
// unicode
127135
assertExpression(
128136
jalapeno.substring(5, 5 + 3),
129-
of(jalapeno).substr(5, 3),
137+
of(jalapeno).substr(of(5), of(3)),
130138
"{'$substrCP': ['" + jalapeno + "', 5, 3]}");
131139
assertExpression(
132140
"e\u00F1o",
133-
of(jalapeno).substr(5, 3));
141+
of(jalapeno).substr(of(5), of(3)));
134142

143+
// bounds; convenience
135144
assertExpression("abc", of("abc").substr(0, 99));
136145
assertExpression("ab", of("abc").substr(0, 2));
137146
assertExpression("b", of("abc").substr(1, 1));
@@ -143,14 +152,17 @@ public void substrBytesTest() {
143152
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/substrBytes/ (?)
144153
assertExpression(
145154
"b",
146-
of("abc").substrBytes(1, 1),
155+
of("abc").substrBytes(of(1), of(1)),
147156
"{'$substrBytes': ['abc', 1, 1]}");
148157

149158
// unicode
150-
byte[] bytes = Arrays.copyOfRange(sushi.getBytes(), 0, 3);
159+
byte[] bytes = Arrays.copyOfRange(sushi.getBytes(StandardCharsets.UTF_8), 0, 3);
151160
String expected = new String(bytes, StandardCharsets.UTF_8);
152161
assertExpression(expected,
153-
of(sushi).substrBytes(0, 3));
154-
// "starting index is a UTF-8 continuation byte" if from 1 length 1
162+
of(sushi).substrBytes(of(0), of(3)));
163+
// server returns "starting index is a UTF-8 continuation byte" error when substrBytes(1, 1)
164+
165+
// convenience
166+
assertExpression("b", of("abc").substrBytes(1, 1));
155167
}
156168
}

0 commit comments

Comments
 (0)