DATAJDBC-637 - Polishing.

Make Jsr310TimestampBasedConverters package-private. Introduce convenience constructors to improve external configuration of JdbcCustomConversions.

Original pull request: #254.
This commit is contained in:
Mark Paluch
2020-12-07 15:24:48 +01:00
committed by Jens Schauder
parent 69fe41dd55
commit 4e6a9b12ab
2 changed files with 27 additions and 15 deletions

View File

@@ -19,6 +19,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
@@ -27,6 +28,7 @@ import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
* {@link org.springframework.data.mapping.model.SimpleTypeHolder}
*
* @author Mark Paluch
* @author Jens Schauder
* @see CustomConversions
* @see org.springframework.data.mapping.model.SimpleTypeHolder
* @see JdbcSimpleTypes
@@ -54,12 +56,27 @@ public class JdbcCustomConversions extends CustomConversions {
super(new ConverterConfiguration(STORE_CONVERSIONS, converters, JdbcCustomConversions::isDateTimeApiConversion));
}
private static boolean isDateTimeApiConversion(
org.springframework.core.convert.converter.GenericConverter.ConvertiblePair cp) {
/**
* Create a new {@link JdbcCustomConversions} instance given
* {@link org.springframework.data.convert.CustomConversions.ConverterConfiguration}.
*
* @param converterConfiguration must not be {@literal null}.
* @since 2.2
*/
public JdbcCustomConversions(ConverterConfiguration converterConfiguration) {
super(converterConfiguration);
}
return (cp.getSourceType().getTypeName().equals("java.util.Date")
&& cp.getTargetType().getTypeName().startsWith("java.time.") //
) || (cp.getTargetType().getTypeName().equals("java.util.Date")
&& cp.getSourceType().getTypeName().startsWith("java.time."));
private static boolean isDateTimeApiConversion(ConvertiblePair cp) {
if (cp.getSourceType().equals(java.util.Date.class) && cp.getTargetType().getTypeName().startsWith("java.time.")) {
return true;
}
if (cp.getTargetType().equals(java.util.Date.class) && cp.getSourceType().getTypeName().startsWith("java.time.")) {
return true;
}
return false;
}
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.jdbc.core.convert;
import static java.time.Instant.*;
import static java.time.LocalDateTime.*;
import static java.time.ZoneId.*;
@@ -41,12 +40,12 @@ import org.springframework.lang.NonNull;
/**
* Helper class to register JSR-310 specific {@link Converter} implementations. These converters are based on
* {@link java.sql.Timestamp} instead of {@link Date} and therefore preserve nanosecond precision
*
*
* @see org.springframework.data.convert.Jsr310Converters
* @author Jens Schauder
* @since 2.2
*/
public abstract class Jsr310TimestampBasedConverters {
abstract class Jsr310TimestampBasedConverters {
private static final List<Class<?>> CLASSES = Arrays.asList(LocalDateTime.class, LocalDate.class, LocalTime.class,
Instant.class, ZoneId.class, Duration.class, Period.class);
@@ -58,7 +57,8 @@ public abstract class Jsr310TimestampBasedConverters {
*/
public static Collection<Converter<?, ?>> getConvertersToRegister() {
List<Converter<?, ?>> converters = new ArrayList<>();
List<Converter<?, ?>> converters = new ArrayList<>(8);
converters.add(TimestampToLocalDateTimeConverter.INSTANCE);
converters.add(LocalDateTimeToTimestampConverter.INSTANCE);
converters.add(TimestampToLocalDateConverter.INSTANCE);
@@ -71,11 +71,6 @@ public abstract class Jsr310TimestampBasedConverters {
return converters;
}
public static boolean supports(Class<?> type) {
return CLASSES.contains(type);
}
@ReadingConverter
public enum TimestampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {