Skip to content

Improve BsonNumber support for Decimal128 #1283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bson/src/main/org/bson/BsonDecimal128.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ public String toString() {

@Override
public int intValue() {
return value.bigDecimalValue().intValue();
return value.intValue();
}

@Override
public long longValue() {
return value.bigDecimalValue().longValue();
return value.longValue();
}

@Override
public double doubleValue() {
return value.bigDecimalValue().doubleValue();
return value.doubleValue();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions bson/src/main/org/bson/BsonValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public BsonString asString() {
* @throws org.bson.BsonInvalidOperationException if this value is not of the expected type
*/
public BsonNumber asNumber() {
if (getBsonType() != BsonType.INT32 && getBsonType() != BsonType.INT64 && getBsonType() != BsonType.DOUBLE) {
if (!isNumber()) {
throw new BsonInvalidOperationException(format("Value expected to be of a numerical BSON type is of unexpected type %s",
getBsonType()));
}
Expand Down Expand Up @@ -282,7 +282,7 @@ public boolean isString() {
* @return true if this is a BsonNumber, false otherwise
*/
public boolean isNumber() {
return isInt32() || isInt64() || isDouble();
return this instanceof BsonNumber;
}

/**
Expand Down
22 changes: 21 additions & 1 deletion bson/src/test/unit/org/bson/BsonValueSpecification.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class BsonValueSpecification extends Specification {
new BsonInt64(52L).isInt64()
new BsonInt64(52L).isNumber()
new BsonDecimal128(Decimal128.parse('1')).isDecimal128()
new BsonDecimal128(Decimal128.parse('1')).isNumber()
new BsonDouble(62.0).isDouble()
new BsonDouble(62.0).isNumber()
new BsonBoolean(true).isBoolean()
Expand Down Expand Up @@ -71,7 +72,26 @@ class BsonValueSpecification extends Specification {
!new BsonNull().isDocument()
}

def 'as methods should return false for the incorrect type'() {
def 'support BsonNumber interface for all number types'() {
expect:
bsonValue.asNumber() == bsonValue
bsonValue.asNumber().intValue()== intValue
bsonValue.asNumber().longValue() == longValue
bsonValue.asNumber().doubleValue() == doubleValue
bsonValue.asNumber().decimal128Value() == decimal128Value

where:
bsonValue | intValue | longValue | doubleValue | decimal128Value
new BsonInt32(42) | 42 | 42L | 42.0 | Decimal128.parse('42')
new BsonInt64(42) | 42 | 42L | 42.0 | Decimal128.parse('42')
new BsonDouble(42) | 42 | 42L | 42.0 | Decimal128.parse('42')
new BsonDecimal128(Decimal128.parse('42')) | 42 | 42L | 42.0 | Decimal128.parse('42')
new BsonDecimal128(Decimal128.POSITIVE_INFINITY) | Integer.MAX_VALUE | Long.MAX_VALUE | Double.POSITIVE_INFINITY | Decimal128.POSITIVE_INFINITY
new BsonDecimal128(Decimal128.NEGATIVE_INFINITY) | Integer.MIN_VALUE | Long.MIN_VALUE | Double.NEGATIVE_INFINITY | Decimal128.NEGATIVE_INFINITY
new BsonDecimal128(Decimal128.NaN) | 0 | 0L | Double.NaN | Decimal128.NaN
}

def 'as methods should return throw for the incorrect type'() {
when:
new BsonNull().asInt32()

Expand Down
1 change: 1 addition & 0 deletions config/codenarc/codenarc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<rule-config name='LineLength'>
<property name='length' value='140'/>
<property name='doNotApplyToFileNames' value='ClientSideEncryptionProseTestSpecification.groovy'/>
<property name='doNotApplyToFileNames' value='BsonValueSpecification.groovy'/>
</rule-config>
<!-- this check is failing '})' when it shouldn't -->
<exclude name='SpaceAfterClosingBrace'/>
Expand Down