|
15 | 15 | */
|
16 | 16 | package org.springframework.data.mongodb.core.convert;
|
17 | 17 |
|
18 |
| -import static org.springframework.data.convert.ConverterBuilder.reading; |
| 18 | +import static org.springframework.data.convert.ConverterBuilder.*; |
19 | 19 |
|
20 | 20 | import java.math.BigDecimal;
|
21 | 21 | import java.math.BigInteger;
|
|
47 | 47 | import org.bson.types.Code;
|
48 | 48 | import org.bson.types.Decimal128;
|
49 | 49 | import org.bson.types.ObjectId;
|
| 50 | + |
50 | 51 | import org.springframework.core.convert.ConversionFailedException;
|
51 | 52 | import org.springframework.core.convert.TypeDescriptor;
|
52 | 53 | import org.springframework.core.convert.converter.ConditionalConverter;
|
@@ -92,6 +93,7 @@ static Collection<Object> getConvertersToRegister() {
|
92 | 93 |
|
93 | 94 | converters.add(BigDecimalToDecimal128Converter.INSTANCE);
|
94 | 95 | converters.add(Decimal128ToBigDecimalConverter.INSTANCE);
|
| 96 | + converters.add(BigIntegerToDecimal128Converter.INSTANCE); |
95 | 97 |
|
96 | 98 | converters.add(URLToStringConverter.INSTANCE);
|
97 | 99 | converters.add(StringToURLConverter.INSTANCE);
|
@@ -190,6 +192,17 @@ public Decimal128 convert(BigDecimal source) {
|
190 | 192 | }
|
191 | 193 | }
|
192 | 194 |
|
| 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 | + |
193 | 206 | enum StringToBigDecimalConverter implements Converter<String, BigDecimal> {
|
194 | 207 | INSTANCE;
|
195 | 208 |
|
@@ -413,17 +426,6 @@ public NumberToNumberConverter(Class<T> targetType) {
|
413 | 426 | @Override
|
414 | 427 | public T convert(Number source) {
|
415 | 428 |
|
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 |
| - |
427 | 429 | if (source instanceof AtomicInteger atomicInteger) {
|
428 | 430 | return NumberUtils.convertNumberToTargetClass(atomicInteger.get(), this.targetType);
|
429 | 431 | }
|
|
0 commit comments