Skip to content

Commit b7131b7

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1372 - Add and register Converters for java.util.Currency.
We now support conversion from currency into ISO 4217 String and back. Original pull request: #342.
1 parent ace99c3 commit b7131b7

File tree

4 files changed

+118
-24
lines changed

4 files changed

+118
-24
lines changed

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
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.
@@ -42,16 +42,6 @@
4242
import org.springframework.data.convert.ThreeTenBackPortConverters;
4343
import org.springframework.data.convert.WritingConverter;
4444
import org.springframework.data.mapping.model.SimpleTypeHolder;
45-
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
46-
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToStringConverter;
47-
import org.springframework.data.mongodb.core.convert.MongoConverters.DBObjectToNamedMongoScriptCoverter;
48-
import org.springframework.data.mongodb.core.convert.MongoConverters.DBObjectToStringConverter;
49-
import org.springframework.data.mongodb.core.convert.MongoConverters.NamedMongoScriptToDBObjectConverter;
50-
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
51-
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter;
52-
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToURLConverter;
53-
import org.springframework.data.mongodb.core.convert.MongoConverters.TermToStringConverter;
54-
import org.springframework.data.mongodb.core.convert.MongoConverters.URLToStringConverter;
5545
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
5646
import org.springframework.data.util.CacheValue;
5747
import org.springframework.util.Assert;
@@ -112,16 +102,7 @@ public CustomConversions(List<?> converters) {
112102
// Add user provided converters to make sure they can override the defaults
113103
toRegister.addAll(converters);
114104
toRegister.add(CustomToStringConverter.INSTANCE);
115-
toRegister.add(BigDecimalToStringConverter.INSTANCE);
116-
toRegister.add(StringToBigDecimalConverter.INSTANCE);
117-
toRegister.add(BigIntegerToStringConverter.INSTANCE);
118-
toRegister.add(StringToBigIntegerConverter.INSTANCE);
119-
toRegister.add(URLToStringConverter.INSTANCE);
120-
toRegister.add(StringToURLConverter.INSTANCE);
121-
toRegister.add(DBObjectToStringConverter.INSTANCE);
122-
toRegister.add(TermToStringConverter.INSTANCE);
123-
toRegister.add(NamedMongoScriptToDBObjectConverter.INSTANCE);
124-
toRegister.add(DBObjectToNamedMongoScriptCoverter.INSTANCE);
105+
toRegister.addAll(MongoConverters.getConvertersToRegister());
125106

126107
toRegister.addAll(JodaTimeConverters.getConvertersToRegister());
127108
toRegister.addAll(GeoConverters.getConvertersToRegister());

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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
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.
@@ -19,6 +19,10 @@
1919
import java.math.BigInteger;
2020
import java.net.MalformedURLException;
2121
import java.net.URL;
22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
import java.util.Currency;
25+
import java.util.List;
2226

2327
import org.bson.types.Code;
2428
import org.bson.types.ObjectId;
@@ -49,6 +53,32 @@ abstract class MongoConverters {
4953
*/
5054
private MongoConverters() {}
5155

56+
/**
57+
* Returns the converters to be registered.
58+
*
59+
* @return
60+
* @since 1.9
61+
*/
62+
public static Collection<Converter<?, ?>> getConvertersToRegister() {
63+
64+
List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
65+
66+
converters.add(BigDecimalToStringConverter.INSTANCE);
67+
converters.add(StringToBigDecimalConverter.INSTANCE);
68+
converters.add(BigIntegerToStringConverter.INSTANCE);
69+
converters.add(StringToBigIntegerConverter.INSTANCE);
70+
converters.add(URLToStringConverter.INSTANCE);
71+
converters.add(StringToURLConverter.INSTANCE);
72+
converters.add(DBObjectToStringConverter.INSTANCE);
73+
converters.add(TermToStringConverter.INSTANCE);
74+
converters.add(NamedMongoScriptToDBObjectConverter.INSTANCE);
75+
converters.add(DBObjectToNamedMongoScriptCoverter.INSTANCE);
76+
converters.add(CurrencyToStringConverter.INSTANCE);
77+
converters.add(StringToCurrencyConverter.INSTANCE);
78+
79+
return converters;
80+
}
81+
5282
/**
5383
* Simple singleton to convert {@link ObjectId}s to their {@link String} representation.
5484
*
@@ -228,4 +258,54 @@ public DBObject convert(NamedMongoScript source) {
228258
return builder.get();
229259
}
230260
}
261+
262+
/**
263+
* {@link Converter} implementation converting {@link Currency} into its ISO 4217 {@link String} representation.
264+
*
265+
* @author Christoph Strobl
266+
* @since 1.9
267+
*/
268+
@WritingConverter
269+
public static enum CurrencyToStringConverter implements Converter<Currency, String> {
270+
INSTANCE;
271+
272+
/*
273+
* (non-Javadoc)
274+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
275+
*/
276+
@Override
277+
public String convert(Currency source) {
278+
279+
if (source == null) {
280+
return null;
281+
}
282+
283+
return source.getCurrencyCode();
284+
}
285+
}
286+
287+
/**
288+
* {@link Converter} implementation converting ISO 4217 {@link String} into {@link Currency}.
289+
*
290+
* @author Christoph Strobl
291+
* @since 1.9
292+
*/
293+
@ReadingConverter
294+
public static enum StringToCurrencyConverter implements Converter<String, Currency> {
295+
INSTANCE;
296+
297+
/*
298+
* (non-Javadoc)
299+
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
300+
*/
301+
@Override
302+
public Currency convert(String source) {
303+
304+
if (!StringUtils.hasText(source)) {
305+
return null;
306+
}
307+
308+
return Currency.getInstance(source);
309+
}
310+
}
231311
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/CustomConversionsUnitTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
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.
@@ -24,6 +24,7 @@
2424
import java.text.SimpleDateFormat;
2525
import java.util.Arrays;
2626
import java.util.Collections;
27+
import java.util.Currency;
2728
import java.util.Date;
2829
import java.util.Locale;
2930
import java.util.UUID;
@@ -271,6 +272,18 @@ public void registersConverterFactoryCorrectly() {
271272
assertThat(customConversions.getCustomWriteTarget(String.class, SimpleDateFormat.class), notNullValue());
272273
}
273274

275+
/**
276+
* @see DATAMONGO-1372
277+
*/
278+
@Test
279+
public void registersConvertersForCurrency() {
280+
281+
CustomConversions customConversions = new CustomConversions();
282+
283+
assertThat(customConversions.hasCustomWriteTarget(Currency.class), is(true));
284+
assertThat(customConversions.hasCustomReadTarget(String.class, Currency.class), is(true));
285+
}
286+
274287
private static Class<?> createProxyTypeFor(Class<?> type) {
275288

276289
ProxyFactory factory = new ProxyFactory();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersUnitTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2014 by the original author(s).
2+
* Copyright (c) 2011-2016 by the original author(s).
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.
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.*;
2020

2121
import java.math.BigDecimal;
22+
import java.util.Currency;
2223

2324
import org.junit.Test;
2425
import org.springframework.data.geo.Box;
@@ -27,7 +28,9 @@
2728
import org.springframework.data.geo.Polygon;
2829
import org.springframework.data.geo.Shape;
2930
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
31+
import org.springframework.data.mongodb.core.convert.MongoConverters.CurrencyToStringConverter;
3032
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
33+
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToCurrencyConverter;
3134
import org.springframework.data.mongodb.core.geo.Sphere;
3235

3336
import com.mongodb.DBObject;
@@ -37,6 +40,7 @@
3740
*
3841
* @author Oliver Gierke
3942
* @author Thomas Darimont
43+
* @author Christoph Strobl
4044
*/
4145
public class MongoConvertersUnitTests {
4246

@@ -120,4 +124,20 @@ public void convertsPointToListAndBackCorrectly() {
120124

121125
assertThat(converted, is((org.springframework.data.geo.Point) point));
122126
}
127+
128+
/**
129+
* @see DATAMONGO-1372
130+
*/
131+
@Test
132+
public void convertsCurrencyToStringCorrectly() {
133+
assertThat(CurrencyToStringConverter.INSTANCE.convert(Currency.getInstance("USD")), is("USD"));
134+
}
135+
136+
/**
137+
* @see DATAMONGO-1372
138+
*/
139+
@Test
140+
public void convertsStringToCurrencyCorrectly() {
141+
assertThat(StringToCurrencyConverter.INSTANCE.convert("USD"), is(Currency.getInstance("USD")));
142+
}
123143
}

0 commit comments

Comments
 (0)