|
15 | 15 | */
|
16 | 16 | package org.springframework.data.mongodb.core.convert;
|
17 | 17 |
|
18 |
| -import static org.springframework.data.convert.ConverterBuilder.*; |
| 18 | +import static org.springframework.data.convert.ConverterBuilder.reading; |
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 |
| - |
51 | 50 | import org.springframework.core.convert.ConversionFailedException;
|
52 | 51 | import org.springframework.core.convert.TypeDescriptor;
|
53 | 52 | import org.springframework.core.convert.converter.ConditionalConverter;
|
54 | 53 | import org.springframework.core.convert.converter.Converter;
|
55 | 54 | import org.springframework.core.convert.converter.ConverterFactory;
|
56 |
| -import org.springframework.core.env.Environment; |
57 |
| -import org.springframework.core.env.StandardEnvironment; |
58 | 55 | import org.springframework.data.convert.ReadingConverter;
|
59 | 56 | import org.springframework.data.convert.WritingConverter;
|
60 | 57 | import org.springframework.data.domain.Vector;
|
@@ -174,20 +171,12 @@ public ObjectId convert(BigInteger source) {
|
174 | 171 | }
|
175 | 172 | }
|
176 | 173 |
|
177 |
| - enum BigDecimalToStringConverter implements Converter<BigDecimal, String>, ConditionalConverter { |
| 174 | + enum BigDecimalToStringConverter implements Converter<BigDecimal, String> { |
178 | 175 | INSTANCE;
|
179 | 176 |
|
180 |
| - Environment env = new StandardEnvironment(); |
181 |
| - |
182 | 177 | public String convert(BigDecimal source) {
|
183 | 178 | return source.toString();
|
184 | 179 | }
|
185 |
| - |
186 |
| - @Override |
187 |
| - public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { |
188 |
| - boolean flagPresent = env.containsProperty("mongo.decimal128.representation"); |
189 |
| - return !flagPresent || env.getProperty("mongo.decimal128.representation", String.class, "string").equals("string"); |
190 |
| - } |
191 | 180 | }
|
192 | 181 |
|
193 | 182 | /**
|
@@ -220,23 +209,16 @@ public BigDecimal convert(Decimal128 source) {
|
220 | 209 | }
|
221 | 210 | }
|
222 | 211 |
|
223 |
| - enum BigIntegerToStringConverter implements Converter<BigInteger, String>, ConditionalConverter { |
| 212 | + @WritingConverter |
| 213 | + enum BigIntegerToStringConverter implements Converter<BigInteger, String> { |
224 | 214 | INSTANCE;
|
225 | 215 |
|
226 |
| - Environment env = new StandardEnvironment(); |
227 |
| - |
228 | 216 | public String convert(BigInteger source) {
|
229 | 217 | return source.toString();
|
230 | 218 | }
|
231 |
| - |
232 |
| - @Override |
233 |
| - public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { |
234 |
| - |
235 |
| - boolean flagPresent = env.containsProperty("mongo.decimal128.representation"); |
236 |
| - return !flagPresent || env.getProperty("mongo.decimal128.representation", String.class, "string").equals("string"); |
237 |
| - } |
238 | 219 | }
|
239 | 220 |
|
| 221 | + @ReadingConverter |
240 | 222 | enum StringToBigIntegerConverter implements Converter<String, BigInteger> {
|
241 | 223 | INSTANCE;
|
242 | 224 |
|
@@ -431,13 +413,13 @@ public NumberToNumberConverter(Class<T> targetType) {
|
431 | 413 | @Override
|
432 | 414 | public T convert(Number source) {
|
433 | 415 |
|
434 |
| - if(targetType == Decimal128.class) { |
| 416 | + if (targetType == Decimal128.class) { |
435 | 417 |
|
436 | 418 | if (source instanceof BigDecimal bigDecimal) {
|
437 | 419 | return targetType.cast(BigDecimalToDecimal128Converter.INSTANCE.convert(bigDecimal));
|
438 | 420 | }
|
439 | 421 |
|
440 |
| - if(source instanceof BigInteger bigInteger) { |
| 422 | + if (source instanceof BigInteger bigInteger) { |
441 | 423 | return targetType.cast(new Decimal128(bigInteger.longValueExact()));
|
442 | 424 | }
|
443 | 425 | }
|
|
0 commit comments