Skip to content

Commit 88bc69a

Browse files
committed
JAVA-2296: Support Decimal128 for BasicBSONDecoder, BasicBSONDecoder, and BSONCallback
Note: this breaks binary compatibility for any implementors of BSONCallback
1 parent ab31bbf commit 88bc69a

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

driver/src/main/org/bson/BSONCallback.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2016 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.bson;
1818

19+
import org.bson.types.Decimal128;
1920
import org.bson.types.ObjectId;
2021

2122
/**
@@ -135,6 +136,16 @@ public interface BSONCallback {
135136
*/
136137
void gotDouble(String name, double value);
137138

139+
/**
140+
* Called when reading a field with a {@link org.bson.BsonType#DECIMAL128} value.
141+
*
142+
* @param name the field name
143+
* @param value the Decimal128 field value
144+
* @since 3.4
145+
* @mongodb.server.release 3.4
146+
*/
147+
void gotDecimal128(String name, Decimal128 value);
148+
138149
/**
139150
* Called when reading a field with a {@link org.bson.BsonType#INT32} value.
140151
*

driver/src/main/org/bson/BSONCallbackAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2016 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -125,7 +125,7 @@ protected void doWriteInt64(final long value) {
125125

126126
@Override
127127
protected void doWriteDecimal128(final Decimal128 value) {
128-
throw new UnsupportedOperationException("Decimal128 not support for legacy API");
128+
bsonCallback.gotDecimal128(getName(), value);
129129
}
130130

131131
@Override

driver/src/main/org/bson/BasicBSONCallback.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2016 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.bson.types.Binary;
2424
import org.bson.types.Code;
2525
import org.bson.types.CodeWScope;
26+
import org.bson.types.Decimal128;
2627
import org.bson.types.MaxKey;
2728
import org.bson.types.MinKey;
2829
import org.bson.types.ObjectId;
@@ -177,6 +178,11 @@ public void gotLong(final String name, final long value) {
177178
_put(name, value);
178179
}
179180

181+
@Override
182+
public void gotDecimal128(final String name, final Decimal128 value) {
183+
_put(name, value);
184+
}
185+
180186
@Override
181187
public void gotDate(final String name, final long millis) {
182188
_put(name, new Date(millis));

driver/src/main/org/bson/EmptyBSONCallback.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2016 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.bson;
1818

19+
import org.bson.types.Decimal128;
1920
import org.bson.types.ObjectId;
2021

2122
/**
@@ -108,6 +109,11 @@ public void gotLong(final String name, final long value) {
108109
throw new UnsupportedOperationException("Operation is not supported");
109110
}
110111

112+
@Override
113+
public void gotDecimal128(final String name, final Decimal128 value) {
114+
throw new UnsupportedOperationException("Operation is not supported");
115+
}
116+
111117
@Override
112118
public void gotDate(final String name, final long millis) {
113119
throw new UnsupportedOperationException("Operation is not supported");

driver/src/test/unit/org/bson/BasicBSONDecoderSpecification.groovy

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2016 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import org.bson.types.BSONTimestamp
2121
import org.bson.types.Binary
2222
import org.bson.types.Code
2323
import org.bson.types.CodeWScope
24+
import org.bson.types.Decimal128
2425
import org.bson.types.MaxKey
2526
import org.bson.types.MinKey
2627
import org.bson.types.ObjectId
@@ -90,6 +91,7 @@ class BasicBSONDecoderSpecification extends Specification {
9091
['i4': Long.MAX_VALUE] | [17, 0, 0, 0, 18, 105, 52, 0, -1, -1, -1, -1, -1, -1, -1, 127, 0]
9192
['k1': new MinKey()] | [9, 0, 0, 0, -1, 107, 49, 0, 0]
9293
['k2': new MaxKey()] | [9, 0, 0, 0, 127, 107, 50, 0, 0]
94+
['f': Decimal128.parse('0E-6176')] | [24, 0, 0, 0, 19, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
9395

9496
type = BsonType.findByValue(bytes[4])
9597
}
@@ -120,22 +122,23 @@ class BasicBSONDecoderSpecification extends Specification {
120122
1 * callback.objectDone()
121123

122124
where:
123-
method | args || bytes
124-
'gotDouble' | ['d1', -1.01d] || [17, 0, 0, 0, 1, 100, 49, 0, 41, 92, -113, -62, -11, 40, -16, -65, 0]
125-
'gotString' | ['s2', 'danke'] || [19, 0, 0, 0, 2, 115, 50, 0, 6, 0, 0, 0, 100, 97, 110, 107, 101, 0, 0]
126-
'gotBinary' | ['b2', 0, [102, 111, 111] as byte[]] || [17, 0, 0, 0, 5, 98, 50, 0, 3, 0, 0, 0, 0, 102, 111, 111, 0]
127-
'gotObjectId' | ['_id', new ObjectId('50d3332018c6a1d8d1662b61')] || [22, 0, 0, 0, 7, 95, 105, 100, 0, 80, -45, 51, 32, 24, -58, -95, -40, -47, 102, 43, 97, 0]
128-
'gotBoolean' | ['b1', true] || [10, 0, 0, 0, 8, 98, 49, 0, 1, 0]
129-
'gotDate' | ['d', 582163200] || [16, 0, 0, 0, 9, 100, 0, 0, 27, -77, 34, 0, 0, 0, 0, 0]
130-
'gotNull' | ['n'] || [8, 0, 0, 0, 10, 110, 0, 0]
131-
'gotRegex' | ['r', '[a]*', 'i'] || [15, 0, 0, 0, 11, 114, 0, 91, 97, 93, 42, 0, 105, 0, 0]
132-
'gotCode' | ['js1', 'var i = 0'] || [24, 0, 0, 0, 13, 106, 115, 49, 0, 10, 0, 0, 0, 118, 97, 114, 32, 105, 32, 61, 32, 48, 0, 0]
133-
'gotSymbol' | ['s', 'c'] || [14, 0, 0, 0, 14, 115, 0, 2, 0, 0, 0, 99, 0, 0]
134-
'gotInt' | ['i1', -12] || [13, 0, 0, 0, 16, 105, 49, 0, -12, -1, -1, -1, 0]
135-
'gotLong' | ['i4', Long.MAX_VALUE] || [17, 0, 0, 0, 18, 105, 52, 0, -1, -1, -1, -1, -1, -1, -1, 127, 0]
136-
'gotTimestamp' | ['t', 123999401, 44332] || [16, 0, 0, 0, 17, 116, 0, 44, -83, 0, 0, -87, 20, 100, 7, 0]
137-
'gotMinKey' | ['k1'] || [9, 0, 0, 0, -1, 107, 49, 0, 0]
138-
'gotMaxKey' | ['k2'] || [9, 0, 0, 0, 127, 107, 50, 0, 0]
125+
method | args || bytes
126+
'gotDouble' | ['d1', -1.01d] || [17, 0, 0, 0, 1, 100, 49, 0, 41, 92, -113, -62, -11, 40, -16, -65, 0]
127+
'gotString' | ['s2', 'danke'] || [19, 0, 0, 0, 2, 115, 50, 0, 6, 0, 0, 0, 100, 97, 110, 107, 101, 0, 0]
128+
'gotBinary' | ['b2', 0, [102, 111, 111] as byte[]] || [17, 0, 0, 0, 5, 98, 50, 0, 3, 0, 0, 0, 0, 102, 111, 111, 0]
129+
'gotObjectId' | ['_id', new ObjectId('50d3332018c6a1d8d1662b61')] || [22, 0, 0, 0, 7, 95, 105, 100, 0, 80, -45, 51, 32, 24, -58, -95, -40, -47, 102, 43, 97, 0]
130+
'gotBoolean' | ['b1', true] || [10, 0, 0, 0, 8, 98, 49, 0, 1, 0]
131+
'gotDate' | ['d', 582163200] || [16, 0, 0, 0, 9, 100, 0, 0, 27, -77, 34, 0, 0, 0, 0, 0]
132+
'gotNull' | ['n'] || [8, 0, 0, 0, 10, 110, 0, 0]
133+
'gotRegex' | ['r', '[a]*', 'i'] || [15, 0, 0, 0, 11, 114, 0, 91, 97, 93, 42, 0, 105, 0, 0]
134+
'gotCode' | ['js1', 'var i = 0'] || [24, 0, 0, 0, 13, 106, 115, 49, 0, 10, 0, 0, 0, 118, 97, 114, 32, 105, 32, 61, 32, 48, 0, 0]
135+
'gotSymbol' | ['s', 'c'] || [14, 0, 0, 0, 14, 115, 0, 2, 0, 0, 0, 99, 0, 0]
136+
'gotInt' | ['i1', -12] || [13, 0, 0, 0, 16, 105, 49, 0, -12, -1, -1, -1, 0]
137+
'gotLong' | ['i4', Long.MAX_VALUE] || [17, 0, 0, 0, 18, 105, 52, 0, -1, -1, -1, -1, -1, -1, -1, 127, 0]
138+
'gotTimestamp' | ['t', 123999401, 44332] || [16, 0, 0, 0, 17, 116, 0, 44, -83, 0, 0, -87, 20, 100, 7, 0]
139+
'gotMinKey' | ['k1'] || [9, 0, 0, 0, -1, 107, 49, 0, 0]
140+
'gotMaxKey' | ['k2'] || [9, 0, 0, 0, 127, 107, 50, 0, 0]
141+
'gotDecimal128' | ['f', Decimal128.parse('0E-6176')] || [24, 0, 0, 0, 19, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
139142

140143
//gotDBRef
141144
//arrayStart

0 commit comments

Comments
 (0)