Skip to content

Commit 4e6a9b1

Browse files
mp911deschauder
authored andcommitted
DATAJDBC-637 - Polishing.
Make Jsr310TimestampBasedConverters package-private. Introduce convenience constructors to improve external configuration of JdbcCustomConversions. Original pull request: #254.
1 parent 69fe41d commit 4e6a9b1

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/JdbcCustomConversions.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22+
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
2223
import org.springframework.data.convert.CustomConversions;
2324
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
2425

@@ -27,6 +28,7 @@
2728
* {@link org.springframework.data.mapping.model.SimpleTypeHolder}
2829
*
2930
* @author Mark Paluch
31+
* @author Jens Schauder
3032
* @see CustomConversions
3133
* @see org.springframework.data.mapping.model.SimpleTypeHolder
3234
* @see JdbcSimpleTypes
@@ -54,12 +56,27 @@ public JdbcCustomConversions(List<?> converters) {
5456
super(new ConverterConfiguration(STORE_CONVERSIONS, converters, JdbcCustomConversions::isDateTimeApiConversion));
5557
}
5658

57-
private static boolean isDateTimeApiConversion(
58-
org.springframework.core.convert.converter.GenericConverter.ConvertiblePair cp) {
59+
/**
60+
* Create a new {@link JdbcCustomConversions} instance given
61+
* {@link org.springframework.data.convert.CustomConversions.ConverterConfiguration}.
62+
*
63+
* @param converterConfiguration must not be {@literal null}.
64+
* @since 2.2
65+
*/
66+
public JdbcCustomConversions(ConverterConfiguration converterConfiguration) {
67+
super(converterConfiguration);
68+
}
69+
70+
private static boolean isDateTimeApiConversion(ConvertiblePair cp) {
71+
72+
if (cp.getSourceType().equals(java.util.Date.class) && cp.getTargetType().getTypeName().startsWith("java.time.")) {
73+
return true;
74+
}
75+
76+
if (cp.getTargetType().equals(java.util.Date.class) && cp.getSourceType().getTypeName().startsWith("java.time.")) {
77+
return true;
78+
}
5979

60-
return (cp.getSourceType().getTypeName().equals("java.util.Date")
61-
&& cp.getTargetType().getTypeName().startsWith("java.time.") //
62-
) || (cp.getTargetType().getTypeName().equals("java.util.Date")
63-
&& cp.getSourceType().getTypeName().startsWith("java.time."));
80+
return false;
6481
}
6582
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/Jsr310TimestampBasedConverters.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.jdbc.core.convert;
1717

18-
import static java.time.Instant.*;
1918
import static java.time.LocalDateTime.*;
2019
import static java.time.ZoneId.*;
2120

@@ -41,12 +40,12 @@
4140
/**
4241
* Helper class to register JSR-310 specific {@link Converter} implementations. These converters are based on
4342
* {@link java.sql.Timestamp} instead of {@link Date} and therefore preserve nanosecond precision
44-
*
43+
*
4544
* @see org.springframework.data.convert.Jsr310Converters
4645
* @author Jens Schauder
4746
* @since 2.2
4847
*/
49-
public abstract class Jsr310TimestampBasedConverters {
48+
abstract class Jsr310TimestampBasedConverters {
5049

5150
private static final List<Class<?>> CLASSES = Arrays.asList(LocalDateTime.class, LocalDate.class, LocalTime.class,
5251
Instant.class, ZoneId.class, Duration.class, Period.class);
@@ -58,7 +57,8 @@ public abstract class Jsr310TimestampBasedConverters {
5857
*/
5958
public static Collection<Converter<?, ?>> getConvertersToRegister() {
6059

61-
List<Converter<?, ?>> converters = new ArrayList<>();
60+
List<Converter<?, ?>> converters = new ArrayList<>(8);
61+
6262
converters.add(TimestampToLocalDateTimeConverter.INSTANCE);
6363
converters.add(LocalDateTimeToTimestampConverter.INSTANCE);
6464
converters.add(TimestampToLocalDateConverter.INSTANCE);
@@ -71,11 +71,6 @@ public abstract class Jsr310TimestampBasedConverters {
7171
return converters;
7272
}
7373

74-
public static boolean supports(Class<?> type) {
75-
76-
return CLASSES.contains(type);
77-
}
78-
7974
@ReadingConverter
8075
public enum TimestampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {
8176

0 commit comments

Comments
 (0)