Skip to content

Commit 363e24d

Browse files
committed
Polishing.
Refine documentation. Simplify NumberToNumberConverter.
1 parent 0a7949d commit 363e24d

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java

+14-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.convert;
1717

18-
import static org.springframework.data.convert.ConverterBuilder.reading;
18+
import static org.springframework.data.convert.ConverterBuilder.*;
1919

2020
import java.math.BigDecimal;
2121
import java.math.BigInteger;
@@ -47,6 +47,7 @@
4747
import org.bson.types.Code;
4848
import org.bson.types.Decimal128;
4949
import org.bson.types.ObjectId;
50+
5051
import org.springframework.core.convert.ConversionFailedException;
5152
import org.springframework.core.convert.TypeDescriptor;
5253
import org.springframework.core.convert.converter.ConditionalConverter;
@@ -92,6 +93,7 @@ static Collection<Object> getConvertersToRegister() {
9293

9394
converters.add(BigDecimalToDecimal128Converter.INSTANCE);
9495
converters.add(Decimal128ToBigDecimalConverter.INSTANCE);
96+
converters.add(BigIntegerToDecimal128Converter.INSTANCE);
9597

9698
converters.add(URLToStringConverter.INSTANCE);
9799
converters.add(StringToURLConverter.INSTANCE);
@@ -190,6 +192,17 @@ public Decimal128 convert(BigDecimal source) {
190192
}
191193
}
192194

195+
/**
196+
* @since 5.0
197+
*/
198+
enum BigIntegerToDecimal128Converter implements Converter<BigInteger, Decimal128> {
199+
INSTANCE;
200+
201+
public Decimal128 convert(BigInteger source) {
202+
return new Decimal128(new BigDecimal(source));
203+
}
204+
}
205+
193206
enum StringToBigDecimalConverter implements Converter<String, BigDecimal> {
194207
INSTANCE;
195208

@@ -413,17 +426,6 @@ public NumberToNumberConverter(Class<T> targetType) {
413426
@Override
414427
public T convert(Number source) {
415428

416-
if (targetType == Decimal128.class) {
417-
418-
if (source instanceof BigDecimal bigDecimal) {
419-
return targetType.cast(BigDecimalToDecimal128Converter.INSTANCE.convert(bigDecimal));
420-
}
421-
422-
if (source instanceof BigInteger bigInteger) {
423-
return targetType.cast(new Decimal128(bigInteger.longValueExact()));
424-
}
425-
}
426-
427429
if (source instanceof AtomicInteger atomicInteger) {
428430
return NumberUtils.convertNumberToTargetClass(atomicInteger.get(), this.targetType);
429431
}

src/main/antora/modules/ROOT/pages/mongodb/mapping/custom-conversions.adoc

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Payment {
2727
{
2828
"_id" : ObjectId("5ca4a34fa264a01503b36af8"), <1>
2929
"value" : NumberDecimal(2.099), <2>
30-
"date" : ISODate("2019-04-03T12:11:01.870Z") <3>
30+
"date" : ISODate("2019-04-03T12:11:01.870Z") <3>
3131
}
3232
----
3333
<1> String _id_ values that represent a valid `ObjectId` are converted automatically. See xref:mongodb/template-crud-operations.adoc#mongo-template.id-handling[How the `_id` Field is Handled in the Mapping Layer]
@@ -108,6 +108,6 @@ class MyMongoConfiguration extends AbstractMongoClientConfiguration {
108108
== Big Number Format
109109

110110
MongoDB in its early days did not have support for large numeric values such as `BigDecimal`.
111-
In order to persist values those types got converted into their `String` representation.
112-
Nowadays `org.bson.types.Decimal128` offers a native solution to storing big numbers.
113-
Next to influencing the to be stored numeric representation via the `@Field` annotation you can configure `MongoCustomConversions` to use `Decimal128` instead of `String` via the `MongoConverterConfigurationAdapter#numericFormat(...)` or set the `mongo.numeric.format=decimal128` property.
111+
To persist `BigDecimal` and `BigInteger` values, Spring Data MongoDB converted values their `String` representation.
112+
With MongoDB Server 3.4, `org.bson.types.Decimal128` offers a native representation for `BigDecimal` and `BigInteger`.
113+
You can use the to the native representation by either annotating your properties with `@Field(targetType=DECIMAL128)` or by configuring the big decimal representation in `MongoCustomConversions`.

src/main/antora/modules/ROOT/pages/mongodb/mapping/mapping.adoc

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ calling `get()` before the actual conversion
165165
166166
| `BigInteger`
167167
| converter +
168-
`String`
169-
| `{"value" : "741" }`
168+
`NumberDecimal`, `String`
169+
| `{"value" : NumberDecimal(741) }`, `{"value" : "741" }`
170170
171171
| `BigDecimal`
172172
| converter +
173-
`String`
174-
| `{"value" : "741.99" }`
173+
`NumberDecimal`, `String`
174+
| `{"value" : NumberDecimal(741.99) }`, `{"value" : "741.99" }`
175175
176176
| `URL`
177177
| converter

0 commit comments

Comments
 (0)