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
This commit is contained in:
Mark Paluch
2022-04-05 11:21:46 +02:00
committed by John Blum
parent cb5201f9d2
commit cad739c337
6 changed files with 42 additions and 68 deletions

View File

@@ -33,6 +33,7 @@ import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.converter.Converter;
@@ -42,7 +43,6 @@ import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.Predicates;
import org.springframework.data.util.Streamable;
@@ -183,35 +183,9 @@ public class CustomConversions {
VavrCollectionConverters.getConvertersToRegister().forEach(it -> registerConverterIn(it, conversionService));
}
/**
* Delegate check if a {@link PropertyValueConverter} for the given {@literal property} is present via
* {@link PropertyValueConversions}.
*
* @param property must not be {@literal null}.
* @return {@literal true} if a specific {@link PropertyValueConverter} is available.
* @see PropertyValueConversions#hasValueConverter(PersistentProperty)
* @since 2.7
*/
public boolean hasPropertyValueConverter(PersistentProperty<?> property) {
return propertyValueConversions != null && propertyValueConversions.hasValueConverter(property);
}
/**
* Delegate to obtain the {@link PropertyValueConverter} for the given {@literal property} from
* {@link PropertyValueConversions}.
*
* @param property must not be {@literal null}.
* @param <DV> domain-specific type
* @param <SV> store-native type
* @param <C> conversion context type
* @return the suitable {@link PropertyValueConverter} or {@literal null} if none available.
* @see PropertyValueConversions#getValueConverter(PersistentProperty)
* @since 2.7
*/
@Nullable
public <DV, SV, C extends PersistentProperty<C>, VCC extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, VCC> getPropertyValueConverter(
C property) {
return propertyValueConversions != null ? propertyValueConversions.getValueConverter(property) : null;
public PropertyValueConversions getPropertyValueConversions() {
return propertyValueConversions;
}
/**

View File

@@ -28,13 +28,14 @@ import org.springframework.util.Assert;
*/
public class PropertyValueConversionService {
private final CustomConversions conversions;
private final PropertyValueConversions conversions;
public PropertyValueConversionService(CustomConversions conversions) {
Assert.notNull(conversions, "CustomConversions must not be null");
this.conversions = conversions;
PropertyValueConversions pvc = conversions.getPropertyValueConversions();
this.conversions = pvc == null ? NoOpPropertyValueConversions.INSTANCE : pvc;
}
/**
@@ -47,7 +48,7 @@ public class PropertyValueConversionService {
* @return {@literal true} there is a converter registered for {@link PersistentProperty}.
*/
public boolean hasConverter(PersistentProperty<?> property) {
return conversions.hasPropertyValueConverter(property);
return conversions.hasValueConverter(property);
}
/**
@@ -64,7 +65,8 @@ public class PropertyValueConversionService {
public <P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> Object read(@Nullable Object value,
P property, VCC context) {
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = getRequiredConverter(property);
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = conversions
.getValueConverter(property);
if (value == null) {
return converter.readNull(context);
@@ -87,7 +89,8 @@ public class PropertyValueConversionService {
public <P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> Object write(@Nullable Object value,
P property, VCC context) {
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = getRequiredConverter(property);
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = conversions
.getValueConverter(property);
if (value == null) {
return converter.writeNull(context);
@@ -96,16 +99,19 @@ public class PropertyValueConversionService {
return converter.write(value, context);
}
private <P extends PersistentProperty<P>> PropertyValueConverter<Object, Object, ValueConversionContext<P>> getRequiredConverter(
P property) {
enum NoOpPropertyValueConversions implements PropertyValueConversions {
PropertyValueConverter<Object, Object, ValueConversionContext<P>> converter = conversions
.getPropertyValueConverter(property);
INSTANCE;
if (converter == null) {
throw new IllegalArgumentException(String.format("No converter registered for property %s", property));
@Override
public boolean hasValueConverter(PersistentProperty<?> property) {
return false;
}
return converter;
@Override
public <DV, SV, P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> PropertyValueConverter<DV, SV, VCC> getValueConverter(
P property) {
throw new UnsupportedOperationException();
}
}
}

View File

@@ -45,11 +45,13 @@ public interface PropertyValueConversions {
* @param property must not be {@literal null}.
* @param <DV> domain-specific type
* @param <SV> store-native type
* @param <C> conversion context type
* @param <P> conversion context type
* @return the suitable {@link PropertyValueConverter}.
* @throws IllegalArgumentException if there is no converter available for {@code property}.
* @see #hasValueConverter(PersistentProperty)
*/
<DV, SV, C extends PersistentProperty<C>, VCC extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, VCC> getValueConverter(
C property);
<DV, SV, P extends PersistentProperty<P>, VCC extends ValueConversionContext<P>> PropertyValueConverter<DV, SV, VCC> getValueConverter(
P property);
/**
* Helper that allows to create {@link PropertyValueConversions} instance with the configured

View File

@@ -46,12 +46,12 @@ public interface PropertyValueConverterFactory {
* @param property must not be {@literal null}.
* @param <DV> domain-specific type.
* @param <SV> store-native type.
* @param <C> value conversion context to use.
* @param <P> value conversion context to use.
* @return can be {@literal null}.
*/
@SuppressWarnings("unchecked")
@Nullable
default <DV, SV, C extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, C> getConverter(
default <DV, SV, P extends ValueConversionContext<?>> PropertyValueConverter<DV, SV, P> getConverter(
PersistentProperty<?> property) {
AnnotatedPropertyValueConverterAccessor accessor = new AnnotatedPropertyValueConverterAccessor(property);
@@ -60,7 +60,7 @@ public interface PropertyValueConverterFactory {
return null;
}
return getConverter((Class<PropertyValueConverter<DV, SV, C>>) accessor.getValueConverterType());
return getConverter((Class<PropertyValueConverter<DV, SV, P>>) accessor.getValueConverterType());
}
/**

View File

@@ -105,11 +105,17 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
return obtainConverterFactory().getConverter(property) != null;
}
@Nullable
@Override
public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, D> getValueConverter(
C property) {
return obtainConverterFactory().getConverter(property);
public <DV, SV, P extends PersistentProperty<P>, D extends ValueConversionContext<P>> PropertyValueConverter<DV, SV, D> getValueConverter(
P property) {
PropertyValueConverter<DV, SV, D> converter = obtainConverterFactory().getConverter(property);
if (converter == null) {
throw new IllegalArgumentException(String.format("No PropertyValueConverter registered for %s", property));
}
return converter;
}
/**