Skip to content

Commit f4a3299

Browse files
committed
Fixes
1 parent 9e725fb commit f4a3299

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ public static BooleanExpression of(final boolean of) {
6666
* @return the boolean array expression
6767
*/
6868
public static ArrayExpression<BooleanExpression> ofBooleanArray(final boolean... array) {
69-
List<BsonValue> result = new ArrayList<>();
69+
Assertions.notNull("array", array);
70+
List<BsonValue> list = new ArrayList<>();
7071
for (boolean b : array) {
71-
result.add(new BsonBoolean(b));
72+
list.add(new BsonBoolean(b));
7273
}
73-
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(result)));
74+
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list)));
7475
}
7576

7677
/**
@@ -85,9 +86,11 @@ public static IntegerExpression of(final int of) {
8586
}
8687

8788
public static ArrayExpression<IntegerExpression> ofIntegerArray(final int... array) {
88-
List<BsonValue> list = Arrays.stream(array)
89-
.mapToObj(BsonInt32::new)
90-
.collect(Collectors.toList());
89+
Assertions.notNull("array", array);
90+
List<BsonValue> list = new ArrayList<>();
91+
for (int i : array) {
92+
list.add(new BsonInt32(i));
93+
}
9194
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list)));
9295
}
9396

@@ -96,9 +99,11 @@ public static IntegerExpression of(final long of) {
9699
}
97100

98101
public static ArrayExpression<IntegerExpression> ofIntegerArray(final long... array) {
99-
List<BsonValue> list = Arrays.stream(array)
100-
.mapToObj(BsonInt64::new)
101-
.collect(Collectors.toList());
102+
Assertions.notNull("array", array);
103+
List<BsonValue> list = new ArrayList<>();
104+
for (long i : array) {
105+
list.add(new BsonInt64(i));
106+
}
102107
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list)));
103108
}
104109

@@ -107,9 +112,11 @@ public static NumberExpression of(final double of) {
107112
}
108113

109114
public static ArrayExpression<NumberExpression> ofNumberArray(final double... array) {
110-
List<BsonValue> list = Arrays.stream(array)
111-
.mapToObj(BsonDouble::new)
112-
.collect(Collectors.toList());
115+
Assertions.notNull("array", array);
116+
List<BsonValue> list = new ArrayList<>();
117+
for (double n : array) {
118+
list.add(new BsonDouble(n));
119+
}
113120
return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list)));
114121
}
115122

@@ -119,6 +126,7 @@ public static NumberExpression of(final Decimal128 of) {
119126
}
120127

121128
public static ArrayExpression<NumberExpression> ofNumberArray(final Decimal128... array) {
129+
Assertions.notNull("array", array);
122130
List<BsonValue> result = new ArrayList<>();
123131
for (Decimal128 e : array) {
124132
Assertions.notNull("elements of array", e);
@@ -133,6 +141,7 @@ public static DateExpression of(final Instant of) {
133141
}
134142

135143
public static ArrayExpression<DateExpression> ofDateArray(final Instant... array) {
144+
Assertions.notNull("array", array);
136145
List<BsonValue> result = new ArrayList<>();
137146
for (Instant e : array) {
138147
Assertions.notNull("elements of array", e);
@@ -155,6 +164,7 @@ public static StringExpression of(final String of) {
155164

156165

157166
public static ArrayExpression<StringExpression> ofStringArray(final String... array) {
167+
Assertions.notNull("array", array);
158168
List<BsonValue> result = new ArrayList<>();
159169
for (String e : array) {
160170
Assertions.notNull("elements of array", e);
@@ -167,10 +177,12 @@ public static ArrayExpression<StringExpression> ofStringArray(final String... ar
167177
public static <T extends Expression> ArrayExpression<T> ofArray(final T... array) {
168178
Assertions.notNull("array", array);
169179
return new MqlExpression<>((cr) -> {
170-
List<BsonValue> array2 = Arrays.stream(array)
171-
.map(v -> ((MqlExpression<?>) v).toBsonValue(cr))
172-
.collect(Collectors.toList());
173-
return new AstPlaceholder(new BsonArray(array2));
180+
List<BsonValue> list = new ArrayList<>();
181+
for (T v : array) {
182+
Assertions.notNull("elements of array", v);
183+
list.add(((MqlExpression<?>) v).toBsonValue(cr));
184+
}
185+
return new AstPlaceholder(new BsonArray(list));
174186
});
175187
}
176188

@@ -189,6 +201,7 @@ public static <R extends Expression> R ofNull() {
189201
}
190202

191203
static NumberExpression numberToExpression(final Number number) {
204+
Assertions.notNull("number", number);
192205
if (number instanceof Integer) {
193206
return of((int) number);
194207
} else if (number instanceof Long) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.time.Instant;
2323
import java.util.Arrays;
2424
import java.util.Collections;
25+
import java.util.LinkedList;
2526
import java.util.stream.Collectors;
2627
import java.util.stream.Stream;
2728

@@ -32,6 +33,7 @@
3233
import static com.mongodb.client.model.expressions.Expressions.ofIntegerArray;
3334
import static com.mongodb.client.model.expressions.Expressions.ofNumberArray;
3435
import static com.mongodb.client.model.expressions.Expressions.ofStringArray;
36+
import static org.testng.internal.collections.Ints.asList;
3537

3638
@SuppressWarnings({"ConstantConditions", "Convert2MethodRef"})
3739
class ArrayExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest {
@@ -160,13 +162,19 @@ public void sizeTest() {
160162
array123.size(),
161163
// MQL:
162164
"{'$size': [[1, 2, 3]]}");
165+
assertExpression(
166+
0,
167+
ofIntegerArray().size(),
168+
// MQL:
169+
"{'$size': [[]]}");
163170
}
164171

165172
@Test
166173
public void elementAtTest() {
167174
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayElemAt/
168175
assertExpression(
169176
Arrays.asList(1, 2, 3).get(0),
177+
// 0.0 is a valid integer value
170178
array123.elementAt((IntegerExpression) of(0.0)),
171179
// MQL:
172180
"{'$arrayElemAt': [[1, 2, 3], 0.0]}");
@@ -188,7 +196,7 @@ public void firstTest() {
188196
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/first/
189197
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/first-array-element/
190198
assertExpression(
191-
Arrays.asList(1, 2, 3).get(0),
199+
new LinkedList<>(asList(1, 2, 3)).getFirst(),
192200
array123.first(),
193201
// MQL:
194202
"{'$first': [[1, 2, 3]]}");
@@ -198,7 +206,7 @@ public void firstTest() {
198206
public void lastTest() {
199207
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/last-array-element/
200208
assertExpression(
201-
Arrays.asList(1, 2, 3).get(2),
209+
new LinkedList<>(asList(1, 2, 3)).getLast(),
202210
array123.last(),
203211
// MQL:
204212
"{'$last': [[1, 2, 3]]}");

0 commit comments

Comments
 (0)