Skip to content

Commit 0612bc7

Browse files
committed
Harmonize default converters
Provide Converter implementations for Charset, Currency and TimeZone as related PropertyEditors are available for those. Issue: SPR-13020
1 parent c026977 commit 0612bc7

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.core.convert.support;
1818

19+
import java.nio.charset.Charset;
20+
import java.util.Currency;
1921
import java.util.Locale;
2022
import java.util.UUID;
2123

@@ -75,7 +77,7 @@ public static void addDefaultConverters(ConverterRegistry converterRegistry) {
7577

7678
converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry));
7779
if (jsr310Available) {
78-
Jsr310ConverterRegistrar.registerZoneIdConverters(converterRegistry);
80+
Jsr310ConverterRegistrar.registerJsr310Converters(converterRegistry);
7981
}
8082

8183
converterRegistry.addConverter(new ObjectToObjectConverter());
@@ -110,6 +112,12 @@ private static void addScalarConverters(ConverterRegistry converterRegistry) {
110112
converterRegistry.addConverter(new StringToLocaleConverter());
111113
converterRegistry.addConverter(Locale.class, String.class, new ObjectToStringConverter());
112114

115+
converterRegistry.addConverter(new StringToCharsetConverter());
116+
converterRegistry.addConverter(Charset.class, String.class, new ObjectToStringConverter());
117+
118+
converterRegistry.addConverter(new StringToCurrencyConverter());
119+
converterRegistry.addConverter(Currency.class, String.class, new ObjectToStringConverter());
120+
113121
converterRegistry.addConverter(new StringToPropertiesConverter());
114122
converterRegistry.addConverter(new PropertiesToStringConverter());
115123

@@ -150,7 +158,8 @@ private static void addCollectionConverters(ConverterRegistry converterRegistry)
150158
*/
151159
private static final class Jsr310ConverterRegistrar {
152160

153-
public static void registerZoneIdConverters(ConverterRegistry converterRegistry) {
161+
public static void registerJsr310Converters(ConverterRegistry converterRegistry) {
162+
converterRegistry.addConverter(new StringToTimeZoneConverter());
154163
converterRegistry.addConverter(new ZoneIdToTimeZoneConverter());
155164
converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter());
156165
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.convert.support;
18+
19+
import java.nio.charset.Charset;
20+
21+
import org.springframework.core.convert.converter.Converter;
22+
23+
/**
24+
* Convert a String to a {@link Charset}.
25+
*
26+
* @author Stephane Nicoll
27+
* @since 4.2
28+
*/
29+
public class StringToCharsetConverter implements Converter<String, Charset> {
30+
31+
@Override
32+
public Charset convert(String source) {
33+
return Charset.forName(source);
34+
}
35+
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.convert.support;
18+
19+
import java.util.Currency;
20+
21+
import org.springframework.core.convert.converter.Converter;
22+
23+
/**
24+
* Convert a String to a {@link Currency}.
25+
*
26+
* @author Stephane Nicoll
27+
* @since 4.2
28+
*/
29+
class StringToCurrencyConverter implements Converter<String, Currency> {
30+
31+
@Override
32+
public Currency convert(String source) {
33+
return Currency.getInstance(source);
34+
}
35+
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.convert.support;
18+
19+
import java.util.TimeZone;
20+
21+
import org.springframework.core.convert.converter.Converter;
22+
import org.springframework.lang.UsesJava8;
23+
import org.springframework.util.StringUtils;
24+
25+
/**
26+
* Convert a String to a {@link TimeZone}.
27+
*
28+
* @author Stephane Nicoll
29+
* @since 4.2
30+
*/
31+
@UsesJava8
32+
class StringToTimeZoneConverter implements Converter<String, TimeZone> {
33+
34+
@Override
35+
public TimeZone convert(String source) {
36+
return StringUtils.parseTimeZoneString(source);
37+
}
38+
39+
}

spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionServiceTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import java.lang.reflect.Method;
2121
import java.math.BigDecimal;
2222
import java.math.BigInteger;
23+
import java.nio.charset.Charset;
2324
import java.time.ZoneId;
2425
import java.util.AbstractList;
2526
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.Collection;
2829
import java.util.Collections;
30+
import java.util.Currency;
2931
import java.util.EnumSet;
3032
import java.util.HashMap;
3133
import java.util.LinkedHashMap;
@@ -37,6 +39,7 @@
3739
import java.util.Optional;
3840
import java.util.Properties;
3941
import java.util.Set;
42+
import java.util.TimeZone;
4043
import java.util.UUID;
4144
import java.util.stream.Stream;
4245

@@ -277,6 +280,26 @@ public void testStringToLocale() {
277280
assertEquals(Locale.ENGLISH, conversionService.convert("en", Locale.class));
278281
}
279282

283+
@Test
284+
public void testStringToCharset() {
285+
assertEquals(Charset.forName("UTF-8"), conversionService.convert("UTF-8", Charset.class));
286+
}
287+
288+
@Test
289+
public void testCharsetToString() {
290+
assertEquals("UTF-8", conversionService.convert(Charset.forName("UTF-8"), String.class));
291+
}
292+
293+
@Test
294+
public void testStringToCurrency() {
295+
assertEquals(Currency.getInstance("EUR"), conversionService.convert("EUR", Currency.class));
296+
}
297+
298+
@Test
299+
public void testCurrencyToString() {
300+
assertEquals("USD", conversionService.convert(Currency.getInstance("USD"), String.class));
301+
}
302+
280303
@Test
281304
public void testStringToString() {
282305
String str = "test";
@@ -801,6 +824,11 @@ public void convertObjectToObjectUsingObjectConstructor() {
801824
assertEquals("toString() invocations", 0, SSN.toStringCount);
802825
}
803826

827+
@Test
828+
public void convertStringToTimezone() {
829+
assertEquals("GMT+02:00", conversionService.convert("GMT+2", TimeZone.class).getID());
830+
}
831+
804832
@Test
805833
public void convertObjectToStringWithJavaTimeOfMethodPresent() {
806834
assertTrue(conversionService.convert(ZoneId.of("GMT+1"), String.class).startsWith("GMT+"));

0 commit comments

Comments
 (0)