Skip to content

Commit d02e37a

Browse files
author
Keith Donald
committed
added new ConverterRegistry operation; polishing
1 parent e25fbf2 commit d02e37a

File tree

5 files changed

+94
-69
lines changed

5 files changed

+94
-69
lines changed

org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,28 @@ public interface ConverterRegistry {
2727

2828
/**
2929
* Add a plain converter to this registry.
30+
* The convertible sourceType/targetType pair is derived from the Converter's parameterized types.
31+
* @throws IllegalArgumentException if the parameterized types could not be resolved
3032
*/
3133
void addConverter(Converter<?, ?> converter);
3234

35+
/**
36+
* Add a plain converter to this registry.
37+
* The convertible sourceType/targetType pair is specified explicitly.
38+
* Allows for a Converter to be reused for multiple distinct pairs without having to create a Converter class for each pair.
39+
* @since 3.1
40+
*/
41+
void addConverter(Class<?> sourceType, Class<?> targetType, Converter<?, ?> converter);
42+
3343
/**
3444
* Add a generic converter to this registry.
3545
*/
3646
void addConverter(GenericConverter converter);
37-
47+
3848
/**
3949
* Add a ranged converter factory to this registry.
50+
* The convertible sourceType/rangeType pair is derived from the ConverterFactory's parameterized types.
51+
* @throws IllegalArgumentException if the parameterized types could not be resolved.
4052
*/
4153
void addConverterFactory(ConverterFactory<?, ?> converterFactory);
4254

org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,16 @@ else if (converter instanceof ConverterFactory<?, ?>) {
6161
}
6262

6363
/**
64-
* Create a new default ConversionService instance that can be safely modified.
65-
*
64+
* Create a new default GenericConversionService instance that can be safely modified.
6665
* @deprecated in Spring 3.1 in favor of {@link DefaultConversionService#DefaultConversionService()}
6766
*/
6867
public static GenericConversionService createDefaultConversionService() {
6968
return new DefaultConversionService();
7069
}
7170

7271
/**
73-
* Populate the given ConversionService instance with all applicable default converters.
74-
*
75-
* @deprecated in Spring 3.1 in favor of {@link DefaultConversionService#addDefaultConverters}
72+
* Populate the given GenericConversionService instance with the set of default converters.
73+
* @deprecated in Spring 3.1 in favor of {@link DefaultConversionService#addDefaultConverters(ConverterRegistry)}
7674
*/
7775
public static void addDefaultConverters(GenericConversionService conversionService) {
7876
DefaultConversionService.addDefaultConverters(conversionService);

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

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818

1919
import java.util.Locale;
2020

21+
import org.springframework.core.convert.ConversionService;
22+
import org.springframework.core.convert.converter.ConverterRegistry;
23+
2124
/**
2225
* A specialization of {@link GenericConversionService} configured by default with
23-
* converters appropriate for most applications.
26+
* converters appropriate for most environments.
2427
*
2528
* <p>Designed for direct instantiation but also exposes the static
26-
* {@link #addDefaultConverters} utility method for ad hoc use against any
27-
* {@code GenericConversionService} instance.
29+
* {@link #addDefaultConverters(ConverterRegistry)} utility method for ad hoc use against any
30+
* {@code ConverterRegistry} instance.
2831
*
2932
* @author Chris Beams
3033
* @since 3.1
@@ -33,62 +36,79 @@ public class DefaultConversionService extends GenericConversionService {
3336

3437
/**
3538
* Create a new {@code DefaultConversionService} with the set of
36-
* {@linkplain DefaultConversionService#addDefaultConverters default converters}.
39+
* {@linkplain DefaultConversionService#addDefaultConverters(ConverterRegistry) default converters}.
3740
*/
3841
public DefaultConversionService() {
3942
addDefaultConverters(this);
4043
}
4144

45+
// static utility methods
46+
4247
/**
4348
* Add converters appropriate for most environments.
44-
* @param conversionService the service to register default formatters against
49+
* @param converterRegistry the registry of converters to add to (must also be castable to ConversionService)
50+
* @throws ClassCastException if the converterRegistry could not be cast to a ConversionService
4551
*/
46-
public static void addDefaultConverters(GenericConversionService conversionService) {
47-
conversionService.addConverter(new ArrayToCollectionConverter(conversionService));
48-
conversionService.addConverter(new CollectionToArrayConverter(conversionService));
49-
50-
conversionService.addConverter(new ArrayToStringConverter(conversionService));
51-
conversionService.addConverter(new StringToArrayConverter(conversionService));
52+
public static void addDefaultConverters(ConverterRegistry converterRegistry) {
53+
addScalarConverters(converterRegistry);
54+
addCollectionConverters(converterRegistry);
55+
addFallbackConverters(converterRegistry);
56+
}
57+
58+
// internal helpers
59+
60+
private static void addScalarConverters(ConverterRegistry converterRegistry) {
61+
converterRegistry.addConverter(new StringToBooleanConverter());
62+
converterRegistry.addConverter(Boolean.class, String.class, new ObjectToStringConverter());
5263

53-
conversionService.addConverter(new ArrayToObjectConverter(conversionService));
54-
conversionService.addConverter(new ObjectToArrayConverter(conversionService));
64+
converterRegistry.addConverterFactory(new StringToNumberConverterFactory());
65+
converterRegistry.addConverter(Number.class, String.class, new ObjectToStringConverter());
5566

56-
conversionService.addConverter(new CollectionToStringConverter(conversionService));
57-
conversionService.addConverter(new StringToCollectionConverter(conversionService));
67+
converterRegistry.addConverterFactory(new NumberToNumberConverterFactory());
68+
69+
converterRegistry.addConverter(new StringToCharacterConverter());
70+
converterRegistry.addConverter(Character.class, String.class, new ObjectToStringConverter());
5871

59-
conversionService.addConverter(new CollectionToObjectConverter(conversionService));
60-
conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
72+
converterRegistry.addConverter(new NumberToCharacterConverter());
73+
converterRegistry.addConverterFactory(new CharacterToNumberFactory());
74+
75+
converterRegistry.addConverterFactory(new StringToEnumConverterFactory());
76+
converterRegistry.addConverter(Enum.class, String.class, new EnumToStringConverter());
77+
78+
converterRegistry.addConverter(new StringToLocaleConverter());
79+
converterRegistry.addConverter(Locale.class, String.class, new ObjectToStringConverter());
6180

62-
conversionService.addConverter(new ArrayToArrayConverter(conversionService));
63-
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
64-
conversionService.addConverter(new MapToMapConverter(conversionService));
81+
converterRegistry.addConverter(new PropertiesToStringConverter());
82+
converterRegistry.addConverter(new StringToPropertiesConverter());
83+
}
6584

66-
conversionService.addConverter(new PropertiesToStringConverter());
67-
conversionService.addConverter(new StringToPropertiesConverter());
85+
private static void addCollectionConverters(ConverterRegistry converterRegistry) {
86+
ConversionService conversionService = (ConversionService) converterRegistry;
87+
converterRegistry.addConverter(new ArrayToCollectionConverter(conversionService));
88+
converterRegistry.addConverter(new CollectionToArrayConverter(conversionService));
6889

69-
conversionService.addConverter(new StringToBooleanConverter());
70-
conversionService.addConverter(Boolean.class, String.class, new ObjectToStringConverter());
90+
converterRegistry.addConverter(new ArrayToArrayConverter(conversionService));
91+
converterRegistry.addConverter(new CollectionToCollectionConverter(conversionService));
92+
converterRegistry.addConverter(new MapToMapConverter(conversionService));
7193

72-
conversionService.addConverter(new StringToCharacterConverter());
73-
conversionService.addConverter(Character.class, String.class, new ObjectToStringConverter());
94+
converterRegistry.addConverter(new ArrayToStringConverter(conversionService));
95+
converterRegistry.addConverter(new StringToArrayConverter(conversionService));
7496

75-
conversionService.addConverter(new StringToLocaleConverter());
76-
conversionService.addConverter(Locale.class, String.class, new ObjectToStringConverter());
77-
78-
conversionService.addConverterFactory(new StringToNumberConverterFactory());
79-
conversionService.addConverter(Number.class, String.class, new ObjectToStringConverter());
80-
81-
conversionService.addConverterFactory(new StringToEnumConverterFactory());
82-
conversionService.addConverter(Enum.class, String.class, new EnumToStringConverter());
83-
84-
conversionService.addConverter(new NumberToCharacterConverter());
85-
conversionService.addConverterFactory(new CharacterToNumberFactory());
97+
converterRegistry.addConverter(new ArrayToObjectConverter(conversionService));
98+
converterRegistry.addConverter(new ObjectToArrayConverter(conversionService));
8699

87-
conversionService.addConverterFactory(new NumberToNumberConverterFactory());
100+
converterRegistry.addConverter(new CollectionToStringConverter(conversionService));
101+
converterRegistry.addConverter(new StringToCollectionConverter(conversionService));
88102

89-
conversionService.addConverter(new ObjectToObjectConverter());
90-
conversionService.addConverter(new IdToEntityConverter(conversionService));
91-
conversionService.addConverter(new FallbackObjectToStringConverter());
103+
converterRegistry.addConverter(new CollectionToObjectConverter(conversionService));
104+
converterRegistry.addConverter(new ObjectToCollectionConverter(conversionService));
105+
}
106+
107+
private static void addFallbackConverters(ConverterRegistry converterRegistry) {
108+
ConversionService conversionService = (ConversionService) converterRegistry;
109+
converterRegistry.addConverter(new ObjectToObjectConverter());
110+
converterRegistry.addConverter(new IdToEntityConverter(conversionService));
111+
converterRegistry.addConverter(new FallbackObjectToStringConverter());
92112
}
93113

94-
}
114+
}

org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,6 @@ public String toString() {
8888
new ConcurrentHashMap<ConverterCacheKey, GenericConverter>();
8989

9090

91-
/**
92-
* Add a converter to the register indexed under the explicit source and target types.
93-
* Allows for a general converter to be reused for multiple distinct source-to-target convertible pairs without having to create a Converter class for each pair.
94-
* Not yet part of the ConverterRegistry interface.
95-
* @since 3.1
96-
*/
97-
public void addConverter(Class<?> sourceType, Class<?> targetType, Converter<?, ?> converter) {
98-
GenericConverter.ConvertiblePair typeInfo = new GenericConverter.ConvertiblePair(sourceType, targetType);
99-
addConverter(new ConverterAdapter(typeInfo, converter));
100-
}
101-
10291
// implementing ConverterRegistry
10392

10493
public void addConverter(Converter<?, ?> converter) {
@@ -110,13 +99,9 @@ public void addConverter(Converter<?, ?> converter) {
11099
addConverter(new ConverterAdapter(typeInfo, converter));
111100
}
112101

113-
public void addConverterFactory(ConverterFactory<?, ?> converterFactory) {
114-
GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converterFactory, ConverterFactory.class);
115-
if (typeInfo == null) {
116-
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetRangeType R which " +
117-
"your ConverterFactory<S, R> converts between; declare these generic types.");
118-
}
119-
addConverter(new ConverterFactoryAdapter(typeInfo, converterFactory));
102+
public void addConverter(Class<?> sourceType, Class<?> targetType, Converter<?, ?> converter) {
103+
GenericConverter.ConvertiblePair typeInfo = new GenericConverter.ConvertiblePair(sourceType, targetType);
104+
addConverter(new ConverterAdapter(typeInfo, converter));
120105
}
121106

122107
public void addConverter(GenericConverter converter) {
@@ -127,6 +112,15 @@ public void addConverter(GenericConverter converter) {
127112
invalidateCache();
128113
}
129114

115+
public void addConverterFactory(ConverterFactory<?, ?> converterFactory) {
116+
GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converterFactory, ConverterFactory.class);
117+
if (typeInfo == null) {
118+
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetRangeType R which " +
119+
"your ConverterFactory<S, R> converts between; declare these generic types.");
120+
}
121+
addConverter(new ConverterFactoryAdapter(typeInfo, converterFactory));
122+
}
123+
130124
public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
131125
getSourceConverterMap(sourceType).remove(targetType);
132126
invalidateCache();

org.springframework.core/src/main/java/org/springframework/core/convert/support/IdToEntityConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.Set;
2323

24+
import org.springframework.core.convert.ConversionService;
2425
import org.springframework.core.convert.TypeDescriptor;
2526
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2627
import org.springframework.util.ClassUtils;
@@ -38,9 +39,9 @@
3839
*/
3940
final class IdToEntityConverter implements ConditionalGenericConverter {
4041

41-
private final GenericConversionService conversionService;
42+
private final ConversionService conversionService;
4243

43-
public IdToEntityConverter(GenericConversionService conversionService) {
44+
public IdToEntityConverter(ConversionService conversionService) {
4445
this.conversionService = conversionService;
4546
}
4647

@@ -56,7 +57,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
5657

5758
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
5859
if (source == null) {
59-
return this.conversionService.convertNullSource(sourceType, targetType);
60+
return null;
6061
}
6162
Method finder = getFinder(targetType.getType());
6263
Object id = this.conversionService.convert(

0 commit comments

Comments
 (0)