-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Jsr310Converters.java
299 lines (254 loc) · 7.55 KB
/
Jsr310Converters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright 2016-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.core.convert;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.redis.core.convert.BinaryConverters.StringBasedConverter;
import org.springframework.util.ClassUtils;
/**
* Helper class to register JSR-310 specific {@link Converter} implementations in case the we're running on Java 8.
*
* @author Mark Paluch
*/
public abstract class Jsr310Converters {
private static final boolean JAVA_8_IS_PRESENT = ClassUtils.isPresent("java.time.LocalDateTime",
Jsr310Converters.class.getClassLoader());
/**
* Returns the converters to be registered. Will only return converters in case we're running on Java 8.
*
* @return
*/
public static Collection<Converter<?, ?>> getConvertersToRegister() {
if (!JAVA_8_IS_PRESENT) {
return Collections.emptySet();
}
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(new LocalDateTimeToBytesConverter());
converters.add(new BytesToLocalDateTimeConverter());
converters.add(new LocalDateToBytesConverter());
converters.add(new BytesToLocalDateConverter());
converters.add(new LocalTimeToBytesConverter());
converters.add(new BytesToLocalTimeConverter());
converters.add(new ZonedDateTimeToBytesConverter());
converters.add(new BytesToZonedDateTimeConverter());
converters.add(new InstantToBytesConverter());
converters.add(new BytesToInstantConverter());
converters.add(new ZoneIdToBytesConverter());
converters.add(new BytesToZoneIdConverter());
converters.add(new PeriodToBytesConverter());
converters.add(new BytesToPeriodConverter());
converters.add(new DurationToBytesConverter());
converters.add(new BytesToDurationConverter());
return converters;
}
public static boolean supports(Class<?> type) {
if (!JAVA_8_IS_PRESENT) {
return false;
}
return Arrays.<Class<?>> asList(LocalDateTime.class, LocalDate.class, LocalTime.class, Instant.class,
ZonedDateTime.class, ZoneId.class, Period.class, Duration.class).contains(type);
}
/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class LocalDateTimeToBytesConverter extends StringBasedConverter implements Converter<LocalDateTime, byte[]> {
@Override
public byte[] convert(LocalDateTime source) {
return fromString(source.toString());
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@ReadingConverter
static class BytesToLocalDateTimeConverter extends StringBasedConverter implements Converter<byte[], LocalDateTime> {
@Override
public LocalDateTime convert(byte[] source) {
return LocalDateTime.parse(toString(source));
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class LocalDateToBytesConverter extends StringBasedConverter implements Converter<LocalDate, byte[]> {
@Override
public byte[] convert(LocalDate source) {
return fromString(source.toString());
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@ReadingConverter
static class BytesToLocalDateConverter extends StringBasedConverter implements Converter<byte[], LocalDate> {
@Override
public LocalDate convert(byte[] source) {
return LocalDate.parse(toString(source));
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class LocalTimeToBytesConverter extends StringBasedConverter implements Converter<LocalTime, byte[]> {
@Override
public byte[] convert(LocalTime source) {
return fromString(source.toString());
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@ReadingConverter
static class BytesToLocalTimeConverter extends StringBasedConverter implements Converter<byte[], LocalTime> {
@Override
public LocalTime convert(byte[] source) {
return LocalTime.parse(toString(source));
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class ZonedDateTimeToBytesConverter extends StringBasedConverter implements Converter<ZonedDateTime, byte[]> {
@Override
public byte[] convert(ZonedDateTime source) {
return fromString(source.toString());
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@ReadingConverter
static class BytesToZonedDateTimeConverter extends StringBasedConverter implements Converter<byte[], ZonedDateTime> {
@Override
public ZonedDateTime convert(byte[] source) {
return ZonedDateTime.parse(toString(source));
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class InstantToBytesConverter extends StringBasedConverter implements Converter<Instant, byte[]> {
@Override
public byte[] convert(Instant source) {
return fromString(source.toString());
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@ReadingConverter
static class BytesToInstantConverter extends StringBasedConverter implements Converter<byte[], Instant> {
@Override
public Instant convert(byte[] source) {
return Instant.parse(toString(source));
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class ZoneIdToBytesConverter extends StringBasedConverter implements Converter<ZoneId, byte[]> {
@Override
public byte[] convert(ZoneId source) {
return fromString(source.toString());
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@ReadingConverter
static class BytesToZoneIdConverter extends StringBasedConverter implements Converter<byte[], ZoneId> {
@Override
public ZoneId convert(byte[] source) {
return ZoneId.of(toString(source));
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class PeriodToBytesConverter extends StringBasedConverter implements Converter<Period, byte[]> {
@Override
public byte[] convert(Period source) {
return fromString(source.toString());
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@ReadingConverter
static class BytesToPeriodConverter extends StringBasedConverter implements Converter<byte[], Period> {
@Override
public Period convert(byte[] source) {
return Period.parse(toString(source));
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class DurationToBytesConverter extends StringBasedConverter implements Converter<Duration, byte[]> {
@Override
public byte[] convert(Duration source) {
return fromString(source.toString());
}
}
/**
* @author Mark Paluch
* @since 1.7
*/
@ReadingConverter
static class BytesToDurationConverter extends StringBasedConverter implements Converter<byte[], Duration> {
@Override
public Duration convert(byte[] source) {
return Duration.parse(toString(source));
}
}
}