Skip to content

Commit 89512cd

Browse files
jyeminstIncMale
andauthored
Improve BsonNumber support for Decimal128 (#1283)
* Change implementation of BsonDecimal128#intValue/longValue/doubleValue to just call the corresponding methods in Decimal128, which handle infinity and NaN similarly to JDK classes * Change BsonValue#isNumber/asNumber to treat BsonDecimal128 as a number JAVA-5265 Co-authored-by: Valentin Kovalenko <[email protected]>
1 parent a03649c commit 89512cd

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

bson/src/main/org/bson/BsonDecimal128.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ public String toString() {
8484

8585
@Override
8686
public int intValue() {
87-
return value.bigDecimalValue().intValue();
87+
return value.intValue();
8888
}
8989

9090
@Override
9191
public long longValue() {
92-
return value.bigDecimalValue().longValue();
92+
return value.longValue();
9393
}
9494

9595
@Override
9696
public double doubleValue() {
97-
return value.bigDecimalValue().doubleValue();
97+
return value.doubleValue();
9898
}
9999

100100
@Override

bson/src/main/org/bson/BsonValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public BsonString asString() {
7777
* @throws org.bson.BsonInvalidOperationException if this value is not of the expected type
7878
*/
7979
public BsonNumber asNumber() {
80-
if (getBsonType() != BsonType.INT32 && getBsonType() != BsonType.INT64 && getBsonType() != BsonType.DOUBLE) {
80+
if (!isNumber()) {
8181
throw new BsonInvalidOperationException(format("Value expected to be of a numerical BSON type is of unexpected type %s",
8282
getBsonType()));
8383
}
@@ -282,7 +282,7 @@ public boolean isString() {
282282
* @return true if this is a BsonNumber, false otherwise
283283
*/
284284
public boolean isNumber() {
285-
return isInt32() || isInt64() || isDouble();
285+
return this instanceof BsonNumber;
286286
}
287287

288288
/**

bson/src/test/unit/org/bson/BsonValueSpecification.groovy

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class BsonValueSpecification extends Specification {
2929
new BsonInt64(52L).isInt64()
3030
new BsonInt64(52L).isNumber()
3131
new BsonDecimal128(Decimal128.parse('1')).isDecimal128()
32+
new BsonDecimal128(Decimal128.parse('1')).isNumber()
3233
new BsonDouble(62.0).isDouble()
3334
new BsonDouble(62.0).isNumber()
3435
new BsonBoolean(true).isBoolean()
@@ -71,7 +72,26 @@ class BsonValueSpecification extends Specification {
7172
!new BsonNull().isDocument()
7273
}
7374

74-
def 'as methods should return false for the incorrect type'() {
75+
def 'support BsonNumber interface for all number types'() {
76+
expect:
77+
bsonValue.asNumber() == bsonValue
78+
bsonValue.asNumber().intValue()== intValue
79+
bsonValue.asNumber().longValue() == longValue
80+
bsonValue.asNumber().doubleValue() == doubleValue
81+
bsonValue.asNumber().decimal128Value() == decimal128Value
82+
83+
where:
84+
bsonValue | intValue | longValue | doubleValue | decimal128Value
85+
new BsonInt32(42) | 42 | 42L | 42.0 | Decimal128.parse('42')
86+
new BsonInt64(42) | 42 | 42L | 42.0 | Decimal128.parse('42')
87+
new BsonDouble(42) | 42 | 42L | 42.0 | Decimal128.parse('42')
88+
new BsonDecimal128(Decimal128.parse('42')) | 42 | 42L | 42.0 | Decimal128.parse('42')
89+
new BsonDecimal128(Decimal128.POSITIVE_INFINITY) | Integer.MAX_VALUE | Long.MAX_VALUE | Double.POSITIVE_INFINITY | Decimal128.POSITIVE_INFINITY
90+
new BsonDecimal128(Decimal128.NEGATIVE_INFINITY) | Integer.MIN_VALUE | Long.MIN_VALUE | Double.NEGATIVE_INFINITY | Decimal128.NEGATIVE_INFINITY
91+
new BsonDecimal128(Decimal128.NaN) | 0 | 0L | Double.NaN | Decimal128.NaN
92+
}
93+
94+
def 'as methods should return throw for the incorrect type'() {
7595
when:
7696
new BsonNull().asInt32()
7797

config/codenarc/codenarc.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<rule-config name='LineLength'>
8383
<property name='length' value='140'/>
8484
<property name='doNotApplyToFileNames' value='ClientSideEncryptionProseTestSpecification.groovy'/>
85+
<property name='doNotApplyToFileNames' value='BsonValueSpecification.groovy'/>
8586
</rule-config>
8687
<!-- this check is failing '})' when it shouldn't -->
8788
<exclude name='SpaceAfterClosingBrace'/>

0 commit comments

Comments
 (0)