Skip to content

Commit a106ec9

Browse files
mp911dejxblum
authored andcommitted
Polishing.
Rename generics from C extends PersistentProperty to P extends PersistentProperty. Refine conversion setup. Make PropertyValueConversions.getValueConverter(…) to return non-null. Return PropertyValueConversions from CustomConversions. Resolves #2577 Closes #2592
1 parent 053875c commit a106ec9

File tree

6 files changed

+44
-68
lines changed

6 files changed

+44
-68
lines changed

src/main/java/org/springframework/data/convert/CustomConversions.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import org.apache.commons.logging.Log;
3535
import org.apache.commons.logging.LogFactory;
36+
3637
import org.springframework.core.GenericTypeResolver;
3738
import org.springframework.core.annotation.AnnotationUtils;
3839
import org.springframework.core.convert.converter.Converter;
@@ -42,7 +43,6 @@
4243
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
4344
import org.springframework.core.convert.support.GenericConversionService;
4445
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
45-
import org.springframework.data.mapping.PersistentProperty;
4646
import org.springframework.data.mapping.model.SimpleTypeHolder;
4747
import org.springframework.data.util.Predicates;
4848
import org.springframework.data.util.Streamable;
@@ -185,35 +185,9 @@ public void registerConvertersIn(ConverterRegistry conversionService) {
185185
VavrCollectionConverters.getConvertersToRegister().forEach(it -> registerConverterIn(it, conversionService));
186186
}
187187

188-
/**
189-
* Delegate check if a {@link PropertyValueConverter} for the given {@literal property} is present via
190-
* {@link PropertyValueConversions}.
191-
*
192-
* @param property must not be {@literal null}.
193-
* @return {@literal true} if a specific {@link PropertyValueConverter} is available.
194-
* @see PropertyValueConversions#hasValueConverter(PersistentProperty)
195-
* @since 2.7
196-
*/
197-
public boolean hasPropertyValueConverter(PersistentProperty<?> property) {
198-
return propertyValueConversions != null && propertyValueConversions.hasValueConverter(property);
199-
}
200-
201-
/**
202-
* Delegate to obtain the {@link PropertyValueConverter} for the given {@literal property} from
203-
* {@link PropertyValueConversions}.
204-
*
205-
* @param property must not be {@literal null}.
206-
* @param <DV> domain-specific type
207-
* @param <SV> store-native type
208-
* @param <C> conversion context type
209-
* @return the suitable {@link PropertyValueConverter} or {@literal null} if none available.
210-
* @see PropertyValueConversions#getValueConverter(PersistentProperty)
211-
* @since 2.7
212-
*/
213188
@Nullable
214-
public <DV, SV, C extends PersistentProperty<C>, VCC extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, VCC> getPropertyValueConverter(
215-
C property) {
216-
return propertyValueConversions != null ? propertyValueConversions.getValueConverter(property) : null;
189+
public PropertyValueConversions getPropertyValueConversions() {
190+
return propertyValueConversions;
217191
}
218192

219193
/**

src/main/java/org/springframework/data/convert/PropertyValueConversionService.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
*/
2929
public class PropertyValueConversionService {
3030

31-
private final CustomConversions conversions;
31+
private final PropertyValueConversions conversions;
3232

3333
public PropertyValueConversionService(CustomConversions conversions) {
3434

3535
Assert.notNull(conversions, "CustomConversions must not be null");
3636

37-
this.conversions = conversions;
37+
PropertyValueConversions pvc = conversions.getPropertyValueConversions();
38+
this.conversions = pvc == null ? NoOpPropertyValueConversions.INSTANCE : pvc;
3839
}
3940

4041
/**
@@ -47,7 +48,7 @@ public PropertyValueConversionService(CustomConversions conversions) {
4748
* @return {@literal true} there is a converter registered for {@link PersistentProperty}.
4849
*/
4950
public boolean hasConverter(PersistentProperty<?> property) {
50-
return conversions.hasPropertyValueConverter(property);
51+
return conversions.hasValueConverter(property);
5152
}
5253

5354
/**
@@ -64,7 +65,8 @@ public boolean hasConverter(PersistentProperty<?> property) {
6465
public <P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> Object read(@Nullable Object value,
6566
P property, VCC context) {
6667

67-
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = getRequiredConverter(property);
68+
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = conversions
69+
.getValueConverter(property);
6870

6971
if (value == null) {
7072
return converter.readNull(context);
@@ -87,7 +89,8 @@ public <P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>>
8789
public <P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> Object write(@Nullable Object value,
8890
P property, VCC context) {
8991

90-
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = getRequiredConverter(property);
92+
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = conversions
93+
.getValueConverter(property);
9194

9295
if (value == null) {
9396
return converter.writeNull(context);
@@ -96,16 +99,19 @@ public <P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>>
9699
return converter.write(value, context);
97100
}
98101

99-
private <P extends PersistentProperty<P>> PropertyValueConverter<Object, Object, ValueConversionContext<P>> getRequiredConverter(
100-
P property) {
102+
enum NoOpPropertyValueConversions implements PropertyValueConversions {
101103

102-
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = conversions
103-
.getPropertyValueConverter(property);
104+
INSTANCE;
104105

105-
if (converter == null) {
106-
throw new IllegalArgumentException(String.format("No converter registered for property %s", property));
106+
@Override
107+
public boolean hasValueConverter(PersistentProperty<?> property) {
108+
return false;
107109
}
108110

109-
return converter;
111+
@Override
112+
public <DV, SV, P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> PropertyValueConverter<DV, SV, VCC> getValueConverter(
113+
P property) {
114+
throw new UnsupportedOperationException();
115+
}
110116
}
111117
}

src/main/java/org/springframework/data/convert/PropertyValueConversions.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ public interface PropertyValueConversions {
4545
* @param property must not be {@literal null}.
4646
* @param <DV> domain-specific type
4747
* @param <SV> store-native type
48-
* @param <C> conversion context type
48+
* @param <P> conversion context type
4949
* @return the suitable {@link PropertyValueConverter}.
50+
* @throws IllegalArgumentException if there is no converter available for {@code property}.
51+
* @see #hasValueConverter(PersistentProperty)
5052
*/
51-
<DV, SV, C extends PersistentProperty<C>, VCC extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, VCC> getValueConverter(
52-
C property);
53+
<DV, SV, P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> PropertyValueConverter<DV, SV, VCC> getValueConverter(
54+
P property);
5355

5456
/**
5557
* Helper that allows to create {@link PropertyValueConversions} instance with the configured

src/main/java/org/springframework/data/convert/PropertyValueConverterFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public interface PropertyValueConverterFactory {
4646
* @param property must not be {@literal null}.
4747
* @param <DV> domain-specific type.
4848
* @param <SV> store-native type.
49-
* @param <C> value conversion context to use.
49+
* @param <P> value conversion context to use.
5050
* @return can be {@literal null}.
5151
*/
5252
@SuppressWarnings("unchecked")
5353
@Nullable
54-
default <DV, SV, C extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, C> getConverter(
54+
default <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> getConverter(
5555
PersistentProperty<?> property) {
5656

5757
AnnotatedPropertyValueConverterAccessor accessor = new AnnotatedPropertyValueConverterAccessor(property);
@@ -60,7 +60,7 @@ default <DV, SV, C extends ValueConversionContext<?>> PropertyValueConverter<DV,
6060
return null;
6161
}
6262

63-
return getConverter((Class<PropertyValueConverter<DV, SV, C>>) accessor.getValueConverterType());
63+
return getConverter((Class<PropertyValueConverter<DV, SV, P>>) accessor.getValueConverterType());
6464
}
6565

6666
/**

src/main/java/org/springframework/data/convert/SimplePropertyValueConversions.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,17 @@ public boolean hasValueConverter(PersistentProperty<?> property) {
105105
return obtainConverterFactory().getConverter(property) != null;
106106
}
107107

108-
@Nullable
109108
@Override
110-
public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, D> getValueConverter(
111-
C property) {
112-
return obtainConverterFactory().getConverter(property);
109+
public <DV, SV, P extends PersistentProperty<P>, D extends ValueConversionContext<P>> PropertyValueConverter<DV, SV, D> getValueConverter(
110+
P property) {
111+
112+
PropertyValueConverter<DV, SV, D> converter = obtainConverterFactory().getConverter(property);
113+
114+
if (converter == null) {
115+
throw new IllegalArgumentException(String.format("No PropertyValueConverter registered for %s", property));
116+
}
117+
118+
return converter;
113119
}
114120

115121
/**

src/test/java/org/springframework/data/convert/CustomConversionsUnitTests.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.jmolecules.ddd.types.Identifier;
3232
import org.joda.time.DateTime;
3333
import org.junit.jupiter.api.Test;
34+
3435
import org.springframework.aop.framework.ProxyFactory;
3536
import org.springframework.core.convert.converter.Converter;
3637
import org.springframework.core.convert.converter.ConverterFactory;
@@ -45,8 +46,8 @@
4546
import org.springframework.data.convert.Jsr310Converters.LocalDateTimeToDateConverter;
4647
import org.springframework.data.convert.ThreeTenBackPortConverters.LocalDateTimeToJavaTimeInstantConverter;
4748
import org.springframework.data.geo.Point;
48-
import org.springframework.data.mapping.PersistentProperty;
4949
import org.springframework.data.mapping.model.SimpleTypeHolder;
50+
5051
import org.threeten.bp.LocalDateTime;
5152

5253
/**
@@ -297,30 +298,17 @@ void registersVavrConverters() {
297298

298299
ConfigurableConversionService conversionService = new DefaultConversionService();
299300

300-
new CustomConversions(StoreConversions.NONE, Collections.emptyList())
301-
.registerConvertersIn(conversionService);
301+
new CustomConversions(StoreConversions.NONE, Collections.emptyList()).registerConvertersIn(conversionService);
302302

303303
assertThat(conversionService.canConvert(io.vavr.collection.List.class, List.class)).isTrue();
304304
assertThat(conversionService.canConvert(List.class, io.vavr.collection.List.class)).isTrue();
305305
}
306306

307-
@Test // GH-1484
308-
void allowsToRegisterPropertyConversions() {
309-
310-
PropertyValueConversions propertyValueConversions = mock(PropertyValueConversions.class);
311-
when(propertyValueConversions.getValueConverter(any())).thenReturn(mock(PropertyValueConverter.class));
312-
313-
CustomConversions conversions = new CustomConversions(new ConverterConfiguration(StoreConversions.NONE,
314-
Collections.emptyList(), (it) -> true, propertyValueConversions));
315-
assertThat(conversions.getPropertyValueConverter(mock(PersistentProperty.class))).isNotNull();
316-
}
317-
318307
@Test // GH-1484
319308
void doesNotFailIfPropertiesConversionIsNull() {
320309

321-
CustomConversions conversions = new CustomConversions(new ConverterConfiguration(StoreConversions.NONE,
322-
Collections.emptyList(), (it) -> true, null));
323-
assertThat(conversions.getPropertyValueConverter(mock(PersistentProperty.class))).isNull();
310+
new CustomConversions(
311+
new ConverterConfiguration(StoreConversions.NONE, Collections.emptyList(), (it) -> true, null));
324312
}
325313

326314
private static Class<?> createProxyTypeFor(Class<?> type) {

0 commit comments

Comments
 (0)