|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jdbc.core.convert; |
| 17 | + |
| 18 | +import static java.time.Instant.*; |
| 19 | +import static java.time.LocalDateTime.*; |
| 20 | +import static java.time.ZoneId.*; |
| 21 | + |
| 22 | +import java.sql.Timestamp; |
| 23 | +import java.time.Duration; |
| 24 | +import java.time.Instant; |
| 25 | +import java.time.LocalDate; |
| 26 | +import java.time.LocalDateTime; |
| 27 | +import java.time.LocalTime; |
| 28 | +import java.time.Period; |
| 29 | +import java.time.ZoneId; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Collection; |
| 33 | +import java.util.Date; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +import org.springframework.core.convert.converter.Converter; |
| 37 | +import org.springframework.data.convert.ReadingConverter; |
| 38 | +import org.springframework.data.convert.WritingConverter; |
| 39 | +import org.springframework.lang.NonNull; |
| 40 | + |
| 41 | +/** |
| 42 | + * Helper class to register JSR-310 specific {@link Converter} implementations. These converters are based on |
| 43 | + * {@link java.sql.Timestamp} instead of {@link Date} and therefore preserve nanosecond precision |
| 44 | + * |
| 45 | + * @see org.springframework.data.convert.Jsr310Converters |
| 46 | + * @author Jens Schauder |
| 47 | + * @since 2.2 |
| 48 | + */ |
| 49 | +public abstract class Jsr310TimestampBasedConverters { |
| 50 | + |
| 51 | + private static final List<Class<?>> CLASSES = Arrays.asList(LocalDateTime.class, LocalDate.class, LocalTime.class, |
| 52 | + Instant.class, ZoneId.class, Duration.class, Period.class); |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the converters to be registered. Will only return converters in case we're running on Java 8. |
| 56 | + * |
| 57 | + * @return |
| 58 | + */ |
| 59 | + public static Collection<Converter<?, ?>> getConvertersToRegister() { |
| 60 | + |
| 61 | + List<Converter<?, ?>> converters = new ArrayList<>(); |
| 62 | + converters.add(TimestampToLocalDateTimeConverter.INSTANCE); |
| 63 | + converters.add(LocalDateTimeToTimestampConverter.INSTANCE); |
| 64 | + converters.add(TimestampToLocalDateConverter.INSTANCE); |
| 65 | + converters.add(LocalDateToTimestampConverter.INSTANCE); |
| 66 | + converters.add(TimestampToLocalTimeConverter.INSTANCE); |
| 67 | + converters.add(LocalTimeToTimestampConverter.INSTANCE); |
| 68 | + converters.add(TimestampToInstantConverter.INSTANCE); |
| 69 | + converters.add(InstantToTimestampConverter.INSTANCE); |
| 70 | + |
| 71 | + return converters; |
| 72 | + } |
| 73 | + |
| 74 | + public static boolean supports(Class<?> type) { |
| 75 | + |
| 76 | + return CLASSES.contains(type); |
| 77 | + } |
| 78 | + |
| 79 | + @ReadingConverter |
| 80 | + public enum TimestampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> { |
| 81 | + |
| 82 | + INSTANCE; |
| 83 | + |
| 84 | + @NonNull |
| 85 | + @Override |
| 86 | + public LocalDateTime convert(Timestamp source) { |
| 87 | + return ofInstant(source.toInstant(), systemDefault()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @WritingConverter |
| 92 | + public enum LocalDateTimeToTimestampConverter implements Converter<LocalDateTime, Timestamp> { |
| 93 | + |
| 94 | + INSTANCE; |
| 95 | + |
| 96 | + @NonNull |
| 97 | + @Override |
| 98 | + public Timestamp convert(LocalDateTime source) { |
| 99 | + return Timestamp.from(source.atZone(systemDefault()).toInstant()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @ReadingConverter |
| 104 | + public enum TimestampToLocalDateConverter implements Converter<Timestamp, LocalDate> { |
| 105 | + |
| 106 | + INSTANCE; |
| 107 | + |
| 108 | + @NonNull |
| 109 | + @Override |
| 110 | + public LocalDate convert(Timestamp source) { |
| 111 | + return source.toLocalDateTime().toLocalDate(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @WritingConverter |
| 116 | + public enum LocalDateToTimestampConverter implements Converter<LocalDate, Timestamp> { |
| 117 | + |
| 118 | + INSTANCE; |
| 119 | + |
| 120 | + @NonNull |
| 121 | + @Override |
| 122 | + public Timestamp convert(LocalDate source) { |
| 123 | + return Timestamp.from(source.atStartOfDay(systemDefault()).toInstant()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @ReadingConverter |
| 128 | + public enum TimestampToLocalTimeConverter implements Converter<Timestamp, LocalTime> { |
| 129 | + |
| 130 | + INSTANCE; |
| 131 | + |
| 132 | + @NonNull |
| 133 | + @Override |
| 134 | + public LocalTime convert(Timestamp source) { |
| 135 | + return source.toLocalDateTime().toLocalTime(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @WritingConverter |
| 140 | + public enum LocalTimeToTimestampConverter implements Converter<LocalTime, Timestamp> { |
| 141 | + |
| 142 | + INSTANCE; |
| 143 | + |
| 144 | + @NonNull |
| 145 | + @Override |
| 146 | + public Timestamp convert(LocalTime source) { |
| 147 | + return Timestamp.from(source.atDate(LocalDate.now()).atZone(systemDefault()).toInstant()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @ReadingConverter |
| 152 | + public enum TimestampToInstantConverter implements Converter<Timestamp, Instant> { |
| 153 | + |
| 154 | + INSTANCE; |
| 155 | + |
| 156 | + @NonNull |
| 157 | + @Override |
| 158 | + public Instant convert(Timestamp source) { |
| 159 | + return source.toInstant(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @WritingConverter |
| 164 | + public enum InstantToTimestampConverter implements Converter<Instant, Timestamp> { |
| 165 | + |
| 166 | + INSTANCE; |
| 167 | + |
| 168 | + @NonNull |
| 169 | + @Override |
| 170 | + public Timestamp convert(Instant source) { |
| 171 | + return Timestamp.from(source); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments