Skip to content

Commit ca7ec2a

Browse files
committed
Add of(BigDecimal)
1 parent b8e5ef3 commit ca7ec2a

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.bson.BsonArray;
2222
import org.bson.BsonBoolean;
2323
import org.bson.BsonDateTime;
24+
import org.bson.BsonDecimal128;
2425
import org.bson.BsonDocument;
2526
import org.bson.BsonDouble;
2627
import org.bson.BsonInt32;
@@ -29,7 +30,9 @@
2930
import org.bson.BsonString;
3031
import org.bson.BsonValue;
3132
import org.bson.conversions.Bson;
33+
import org.bson.types.Decimal128;
3234

35+
import java.math.BigDecimal;
3336
import java.time.Instant;
3437
import java.util.ArrayList;
3538
import java.util.Arrays;
@@ -73,6 +76,10 @@ public static IntegerExpression of(final long of) {
7376
public static NumberExpression of(final double of) {
7477
return new MqlExpression<>((codecRegistry) -> new AstPlaceholder(new BsonDouble(of)));
7578
}
79+
public static NumberExpression of(@NonNull final BigDecimal of) {
80+
Assertions.notNull("BigDecimal", of);
81+
return new MqlExpression<>((codecRegistry) -> new AstPlaceholder(new BsonDecimal128(new Decimal128(of))));
82+
}
7683
public static DateExpression of(@NonNull final Instant of) {
7784
Assertions.notNull("Instant", of);
7885
return new MqlExpression<>((codecRegistry) -> new AstPlaceholder(new BsonDateTime(of.toEpochMilli())));

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
*/
2222
public interface NumberExpression extends Expression {
2323

24-
// TODO this must not return <T extends NumberExpression> T
25-
// since it allows: IntegerEx result = oneNum.multiply(oneInt)
2624
NumberExpression multiply(NumberExpression n);
2725

2826
default NumberExpression multiply(final double multiply) {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616

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

19+
import org.bson.types.Decimal128;
1920
import org.junit.jupiter.api.Test;
2021

22+
import java.math.BigDecimal;
23+
2124
import static com.mongodb.client.model.expressions.Expressions.of;
2225
import static org.junit.jupiter.api.Assertions.assertEquals;
2326
import static org.junit.jupiter.api.Assertions.assertNotEquals;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
2428

29+
@SuppressWarnings("ConstantConditions")
2530
class ArithmeticExpressionsFunctionalTest extends AbstractExpressionsFunctionalTest {
2631
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/#arithmetic-expression-operators
2732

@@ -30,13 +35,15 @@ public void literalsTest() {
3035
assertExpression(1, of(1), "1");
3136
assertExpression(1L, of(1L));
3237
assertExpression(1.0, of(1.0));
38+
assertExpression(BigDecimal.valueOf(1.0), of(BigDecimal.valueOf(1.0)));
39+
assertThrows(IllegalArgumentException.class, () -> of((BigDecimal) null));
3340

3441
// expression equality differs from bson equality
3542
assertExpression(true, of(1L).eq(of(1.0)));
3643
assertExpression(true, of(1L).eq(of(1)));
3744

3845
// bson equality; underlying type is preserved
39-
// this is not defined by the API, but tested for clarity
46+
// this behaviour is not defined by the API, but tested for clarity
4047
assertEquals(toBsonValue(1), evaluate(of(1)));
4148
assertEquals(toBsonValue(1L), evaluate(of(1L)));
4249
assertEquals(toBsonValue(1.0), evaluate(of(1.0)));
@@ -89,8 +96,6 @@ public void divideTest() {
8996
// convenience
9097
assertExpression(0.5, of(1.0).divide(2.0));
9198
assertExpression(0.5, of(1).divide(2.0));
92-
// TODO these convenience methods are all for doubles, so while they
93-
// TODO allow longs and ints, they do not carry that through to the server:
9499
assertExpression(0.5, of(1).divide(2L));
95100
assertExpression(0.5, of(1).divide(2));
96101
}

0 commit comments

Comments
 (0)