Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 98707a8

Browse files
committed
Polishing.
Replace qualified class name access of inner classes with simple names and imports. Remove Java 8 guards. Extend supported temporal types in Jsr310Converters. Remove superfluous converter annotations. Simplify tests. See #2677 Original pull request: #2681
1 parent d89641e commit 98707a8

File tree

5 files changed

+105
-101
lines changed

5 files changed

+105
-101
lines changed

‎src/main/java/org/springframework/data/redis/core/convert/BinaryConverters.java‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
import java.nio.charset.StandardCharsets;
2020
import java.text.DateFormat;
2121
import java.text.ParseException;
22+
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.Collection;
2425
import java.util.Date;
25-
import java.util.Set;
26+
import java.util.List;
2627
import java.util.UUID;
2728

2829
import org.springframework.core.convert.converter.Converter;
@@ -50,20 +51,22 @@ private BinaryConverters() {}
5051

5152
static Collection<?> getConvertersToRegister() {
5253

53-
return Set.of(
54-
new BinaryConverters.StringToBytesConverter(),
55-
new BinaryConverters.BytesToStringConverter(),
56-
new BinaryConverters.NumberToBytesConverter(),
57-
new BinaryConverters.BytesToNumberConverterFactory(),
58-
new BinaryConverters.EnumToBytesConverter(),
59-
new BinaryConverters.BytesToEnumConverterFactory(),
60-
new BinaryConverters.BooleanToBytesConverter(),
61-
new BinaryConverters.BytesToBooleanConverter(),
62-
new BinaryConverters.DateToBytesConverter(),
63-
new BinaryConverters.BytesToDateConverter(),
64-
new BinaryConverters.UuidToBytesConverter(),
65-
new BinaryConverters.BytesToUuidConverter()
66-
);
54+
List<Object> converters = new ArrayList<>(12);
55+
56+
converters.add(new StringToBytesConverter());
57+
converters.add(new BytesToStringConverter());
58+
converters.add(new NumberToBytesConverter());
59+
converters.add(new BytesToNumberConverterFactory());
60+
converters.add(new EnumToBytesConverter());
61+
converters.add(new BytesToEnumConverterFactory());
62+
converters.add(new BooleanToBytesConverter());
63+
converters.add(new BytesToBooleanConverter());
64+
converters.add(new DateToBytesConverter());
65+
converters.add(new BytesToDateConverter());
66+
converters.add(new UuidToBytesConverter());
67+
converters.add(new BytesToUuidConverter());
68+
69+
return converters;
6770
}
6871

6972
/**

‎src/main/java/org/springframework/data/redis/core/convert/Jsr310Converters.java‎

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,27 @@
2828
import java.util.ArrayList;
2929
import java.util.Arrays;
3030
import java.util.Collection;
31-
import java.util.Collections;
3231
import java.util.List;
3332

3433
import org.springframework.core.convert.converter.Converter;
35-
import org.springframework.data.convert.ReadingConverter;
36-
import org.springframework.data.convert.WritingConverter;
3734
import org.springframework.data.redis.core.convert.BinaryConverters.StringBasedConverter;
38-
import org.springframework.util.ClassUtils;
3935

4036
/**
41-
* Helper class to register JSR-310 specific {@link Converter} implementations in case the we're running on Java 8.
37+
* Helper class to register JSR-310 specific {@link Converter} implementations.
4238
*
4339
* @author Mark Paluch
40+
* @author John Blum
4441
*/
4542
public abstract class Jsr310Converters {
4643

47-
private static final boolean JAVA_8_IS_PRESENT = ClassUtils.isPresent("java.time.LocalDateTime",
48-
Jsr310Converters.class.getClassLoader());
49-
5044
/**
5145
* Returns the {@link Converter Converters} to be registered.
52-
* <p>
53-
* Will only return {@link Converter Converters} in case we're running on Java 8.
5446
*
5547
* @return the {@link Converter Converters} to be registered.
5648
*/
5749
public static Collection<Converter<?, ?>> getConvertersToRegister() {
5850

59-
if (!JAVA_8_IS_PRESENT) {
60-
return Collections.emptySet();
61-
}
62-
63-
List<Converter<?, ?>> converters = new ArrayList<>();
51+
List<Converter<?, ?>> converters = new ArrayList<>(20);
6452

6553
converters.add(new LocalDateTimeToBytesConverter());
6654
converters.add(new BytesToLocalDateTimeConverter());
@@ -88,19 +76,15 @@ public abstract class Jsr310Converters {
8876

8977
public static boolean supports(Class<?> type) {
9078

91-
if (!JAVA_8_IS_PRESENT) {
92-
return false;
93-
}
94-
9579
return Arrays.<Class<?>> asList(LocalDateTime.class, LocalDate.class, LocalTime.class, Instant.class,
96-
ZonedDateTime.class, ZoneId.class, Period.class, Duration.class).contains(type);
80+
ZonedDateTime.class, ZoneId.class, Period.class, Duration.class, OffsetDateTime.class, OffsetTime.class)
81+
.contains(type);
9782
}
9883

9984
/**
10085
* @author Mark Paluch
10186
* @since 1.7
10287
*/
103-
@WritingConverter
10488
static class LocalDateTimeToBytesConverter extends StringBasedConverter implements Converter<LocalDateTime, byte[]> {
10589

10690
@Override
@@ -113,7 +97,6 @@ public byte[] convert(LocalDateTime source) {
11397
* @author Mark Paluch
11498
* @since 1.7
11599
*/
116-
@ReadingConverter
117100
static class BytesToLocalDateTimeConverter extends StringBasedConverter implements Converter<byte[], LocalDateTime> {
118101

119102
@Override
@@ -126,7 +109,6 @@ public LocalDateTime convert(byte[] source) {
126109
* @author Mark Paluch
127110
* @since 1.7
128111
*/
129-
@WritingConverter
130112
static class LocalDateToBytesConverter extends StringBasedConverter implements Converter<LocalDate, byte[]> {
131113

132114
@Override
@@ -139,7 +121,6 @@ public byte[] convert(LocalDate source) {
139121
* @author Mark Paluch
140122
* @since 1.7
141123
*/
142-
@ReadingConverter
143124
static class BytesToLocalDateConverter extends StringBasedConverter implements Converter<byte[], LocalDate> {
144125

145126
@Override
@@ -152,7 +133,6 @@ public LocalDate convert(byte[] source) {
152133
* @author Mark Paluch
153134
* @since 1.7
154135
*/
155-
@WritingConverter
156136
static class LocalTimeToBytesConverter extends StringBasedConverter implements Converter<LocalTime, byte[]> {
157137

158138
@Override
@@ -165,7 +145,6 @@ public byte[] convert(LocalTime source) {
165145
* @author Mark Paluch
166146
* @since 1.7
167147
*/
168-
@ReadingConverter
169148
static class BytesToLocalTimeConverter extends StringBasedConverter implements Converter<byte[], LocalTime> {
170149

171150
@Override
@@ -178,7 +157,6 @@ public LocalTime convert(byte[] source) {
178157
* @author Mark Paluch
179158
* @since 1.7
180159
*/
181-
@WritingConverter
182160
static class ZonedDateTimeToBytesConverter extends StringBasedConverter implements Converter<ZonedDateTime, byte[]> {
183161

184162
@Override
@@ -191,7 +169,6 @@ public byte[] convert(ZonedDateTime source) {
191169
* @author Mark Paluch
192170
* @since 1.7
193171
*/
194-
@ReadingConverter
195172
static class BytesToZonedDateTimeConverter extends StringBasedConverter implements Converter<byte[], ZonedDateTime> {
196173

197174
@Override
@@ -204,7 +181,6 @@ public ZonedDateTime convert(byte[] source) {
204181
* @author Mark Paluch
205182
* @since 1.7
206183
*/
207-
@WritingConverter
208184
static class InstantToBytesConverter extends StringBasedConverter implements Converter<Instant, byte[]> {
209185

210186
@Override
@@ -217,7 +193,6 @@ public byte[] convert(Instant source) {
217193
* @author Mark Paluch
218194
* @since 1.7
219195
*/
220-
@ReadingConverter
221196
static class BytesToInstantConverter extends StringBasedConverter implements Converter<byte[], Instant> {
222197

223198
@Override
@@ -230,7 +205,6 @@ public Instant convert(byte[] source) {
230205
* @author Mark Paluch
231206
* @since 1.7
232207
*/
233-
@WritingConverter
234208
static class ZoneIdToBytesConverter extends StringBasedConverter implements Converter<ZoneId, byte[]> {
235209

236210
@Override
@@ -243,7 +217,6 @@ public byte[] convert(ZoneId source) {
243217
* @author Mark Paluch
244218
* @since 1.7
245219
*/
246-
@ReadingConverter
247220
static class BytesToZoneIdConverter extends StringBasedConverter implements Converter<byte[], ZoneId> {
248221

249222
@Override
@@ -256,7 +229,6 @@ public ZoneId convert(byte[] source) {
256229
* @author Mark Paluch
257230
* @since 1.7
258231
*/
259-
@WritingConverter
260232
static class PeriodToBytesConverter extends StringBasedConverter implements Converter<Period, byte[]> {
261233

262234
@Override
@@ -269,7 +241,6 @@ public byte[] convert(Period source) {
269241
* @author Mark Paluch
270242
* @since 1.7
271243
*/
272-
@ReadingConverter
273244
static class BytesToPeriodConverter extends StringBasedConverter implements Converter<byte[], Period> {
274245

275246
@Override
@@ -282,7 +253,6 @@ public Period convert(byte[] source) {
282253
* @author Mark Paluch
283254
* @since 1.7
284255
*/
285-
@WritingConverter
286256
static class DurationToBytesConverter extends StringBasedConverter implements Converter<Duration, byte[]> {
287257

288258
@Override
@@ -295,7 +265,6 @@ public byte[] convert(Duration source) {
295265
* @author Mark Paluch
296266
* @since 1.7
297267
*/
298-
@ReadingConverter
299268
static class BytesToDurationConverter extends StringBasedConverter implements Converter<byte[], Duration> {
300269

301270
@Override
@@ -306,9 +275,10 @@ public Duration convert(byte[] source) {
306275

307276
/**
308277
* @author John Blum
309-
* @see java.time.OffsetDateTime
278+
* @since 3.1.3
310279
*/
311-
static class OffsetDateTimeToBytesConverter extends StringBasedConverter implements Converter<OffsetDateTime, byte[]> {
280+
static class OffsetDateTimeToBytesConverter extends StringBasedConverter
281+
implements Converter<OffsetDateTime, byte[]> {
312282

313283
@Override
314284
public byte[] convert(OffsetDateTime source) {
@@ -318,9 +288,10 @@ public byte[] convert(OffsetDateTime source) {
318288

319289
/**
320290
* @author John Blum
321-
* @see java.time.OffsetDateTime
291+
* @since 3.1.3
322292
*/
323-
static class BytesToOffsetDateTimeConverter extends StringBasedConverter implements Converter<byte[], OffsetDateTime> {
293+
static class BytesToOffsetDateTimeConverter extends StringBasedConverter
294+
implements Converter<byte[], OffsetDateTime> {
324295

325296
@Override
326297
public OffsetDateTime convert(byte[] source) {
@@ -330,7 +301,7 @@ public OffsetDateTime convert(byte[] source) {
330301

331302
/**
332303
* @author John Blum
333-
* @see java.time.OffsetTime
304+
* @since 3.1.3
334305
*/
335306
static class OffsetTimeToBytesConverter extends StringBasedConverter implements Converter<OffsetTime, byte[]> {
336307

@@ -342,7 +313,7 @@ public byte[] convert(OffsetTime source) {
342313

343314
/**
344315
* @author John Blum
345-
* @see java.time.OffsetTime
316+
* @since 3.1.3
346317
*/
347318
static class BytesToOffsetTimeConverter extends StringBasedConverter implements Converter<byte[], OffsetTime> {
348319

‎src/main/java/org/springframework/data/redis/core/convert/RedisCustomConversions.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class RedisCustomConversions extends org.springframework.data.convert.Cus
3737

3838
static {
3939

40-
List<Object> converters = new ArrayList<>();
40+
List<Object> converters = new ArrayList<>(35);
4141

4242
converters.addAll(BinaryConverters.getConvertersToRegister());
4343
converters.addAll(Jsr310Converters.getConvertersToRegister());
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2023 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.redis.core.convert;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.time.Duration;
21+
import java.time.Instant;
22+
import java.time.LocalDate;
23+
import java.time.LocalDateTime;
24+
import java.time.LocalTime;
25+
import java.time.OffsetDateTime;
26+
import java.time.OffsetTime;
27+
import java.time.Period;
28+
import java.time.ZoneId;
29+
import java.time.ZonedDateTime;
30+
import java.util.Date;
31+
32+
import org.junit.jupiter.api.Test;
33+
34+
/**
35+
* Unit test for {@link Jsr310Converters}.
36+
*
37+
* @author Mark Paluch
38+
*/
39+
class Jsr310ConvertersTest {
40+
41+
@Test // GH-2677
42+
void shouldReportSupportedTemporalTypes() {
43+
44+
assertThat(Jsr310Converters.supports(Object.class)).isFalse();
45+
assertThat(Jsr310Converters.supports(Date.class)).isFalse();
46+
47+
assertThat(Jsr310Converters.supports(Instant.class)).isTrue();
48+
assertThat(Jsr310Converters.supports(ZoneId.class)).isTrue();
49+
assertThat(Jsr310Converters.supports(ZonedDateTime.class)).isTrue();
50+
51+
assertThat(Jsr310Converters.supports(LocalDateTime.class)).isTrue();
52+
assertThat(Jsr310Converters.supports(LocalDate.class)).isTrue();
53+
assertThat(Jsr310Converters.supports(LocalTime.class)).isTrue();
54+
55+
assertThat(Jsr310Converters.supports(Duration.class)).isTrue();
56+
assertThat(Jsr310Converters.supports(Period.class)).isTrue();
57+
58+
assertThat(Jsr310Converters.supports(OffsetTime.class)).isTrue();
59+
assertThat(Jsr310Converters.supports(OffsetDateTime.class)).isTrue();
60+
}
61+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /