Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mp911de committed Aug 17, 2023
1 parent dc09635 commit 76c1830
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.List;
import java.util.UUID;

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

static Collection<?> getConvertersToRegister() {

return Set.of(
new BinaryConverters.StringToBytesConverter(),
new BinaryConverters.BytesToStringConverter(),
new BinaryConverters.NumberToBytesConverter(),
new BinaryConverters.BytesToNumberConverterFactory(),
new BinaryConverters.EnumToBytesConverter(),
new BinaryConverters.BytesToEnumConverterFactory(),
new BinaryConverters.BooleanToBytesConverter(),
new BinaryConverters.BytesToBooleanConverter(),
new BinaryConverters.DateToBytesConverter(),
new BinaryConverters.BytesToDateConverter(),
new BinaryConverters.UuidToBytesConverter(),
new BinaryConverters.BytesToUuidConverter()
);
List<Object> converters = new ArrayList<>(12);

converters.add(new StringToBytesConverter());
converters.add(new BytesToStringConverter());
converters.add(new NumberToBytesConverter());
converters.add(new BytesToNumberConverterFactory());
converters.add(new EnumToBytesConverter());
converters.add(new BytesToEnumConverterFactory());
converters.add(new BooleanToBytesConverter());
converters.add(new BytesToBooleanConverter());
converters.add(new DateToBytesConverter());
converters.add(new BytesToDateConverter());
converters.add(new UuidToBytesConverter());
converters.add(new BytesToUuidConverter());

return converters;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,27 @@
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.
* Helper class to register JSR-310 specific {@link Converter} implementations.
*
* @author Mark Paluch
* @author John Blum
*/
public abstract class Jsr310Converters {

private static final boolean JAVA_8_IS_PRESENT = ClassUtils.isPresent("java.time.LocalDateTime",
Jsr310Converters.class.getClassLoader());

/**
* Returns the {@link Converter Converters} to be registered.
* <p>
* Will only return {@link Converter Converters} in case we're running on Java 8.
*
* @return the {@link Converter Converters} to be registered.
*/
public static Collection<Converter<?, ?>> getConvertersToRegister() {

if (!JAVA_8_IS_PRESENT) {
return Collections.emptySet();
}

List<Converter<?, ?>> converters = new ArrayList<>();
List<Converter<?, ?>> converters = new ArrayList<>(20);

converters.add(new LocalDateTimeToBytesConverter());
converters.add(new BytesToLocalDateTimeConverter());
Expand Down Expand Up @@ -88,19 +76,15 @@ public abstract class Jsr310Converters {

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);
ZonedDateTime.class, ZoneId.class, Period.class, Duration.class, OffsetDateTime.class, OffsetTime.class)
.contains(type);
}

/**
* @author Mark Paluch
* @since 1.7
*/
@WritingConverter
static class LocalDateTimeToBytesConverter extends StringBasedConverter implements Converter<LocalDateTime, byte[]> {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
* @author John Blum
* @see java.time.OffsetDateTime
* @since 3.1.3
*/
static class OffsetDateTimeToBytesConverter extends StringBasedConverter implements Converter<OffsetDateTime, byte[]> {
static class OffsetDateTimeToBytesConverter extends StringBasedConverter
implements Converter<OffsetDateTime, byte[]> {

@Override
public byte[] convert(OffsetDateTime source) {
Expand All @@ -318,9 +288,10 @@ public byte[] convert(OffsetDateTime source) {

/**
* @author John Blum
* @see java.time.OffsetDateTime
* @since 3.1.3
*/
static class BytesToOffsetDateTimeConverter extends StringBasedConverter implements Converter<byte[], OffsetDateTime> {
static class BytesToOffsetDateTimeConverter extends StringBasedConverter
implements Converter<byte[], OffsetDateTime> {

@Override
public OffsetDateTime convert(byte[] source) {
Expand All @@ -330,7 +301,7 @@ public OffsetDateTime convert(byte[] source) {

/**
* @author John Blum
* @see java.time.OffsetTime
* @since 3.1.3
*/
static class OffsetTimeToBytesConverter extends StringBasedConverter implements Converter<OffsetTime, byte[]> {

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

/**
* @author John Blum
* @see java.time.OffsetTime
* @since 3.1.3
*/
static class BytesToOffsetTimeConverter extends StringBasedConverter implements Converter<byte[], OffsetTime> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class RedisCustomConversions extends org.springframework.data.convert.Cus

static {

List<Object> converters = new ArrayList<>();
List<Object> converters = new ArrayList<>(35);

converters.addAll(BinaryConverters.getConvertersToRegister());
converters.addAll(Jsr310Converters.getConvertersToRegister());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 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 static org.assertj.core.api.Assertions.*;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

import org.junit.jupiter.api.Test;

/**
* Unit test for {@link Jsr310Converters}.
*
* @author Mark Paluch
*/
class Jsr310ConvertersTest {

@Test // GH-2677
void shouldReportSupportedTemporalTypes() {

assertThat(Jsr310Converters.supports(Object.class)).isFalse();
assertThat(Jsr310Converters.supports(Date.class)).isFalse();

assertThat(Jsr310Converters.supports(Instant.class)).isTrue();
assertThat(Jsr310Converters.supports(ZoneId.class)).isTrue();
assertThat(Jsr310Converters.supports(ZonedDateTime.class)).isTrue();

assertThat(Jsr310Converters.supports(LocalDateTime.class)).isTrue();
assertThat(Jsr310Converters.supports(LocalDate.class)).isTrue();
assertThat(Jsr310Converters.supports(LocalTime.class)).isTrue();

assertThat(Jsr310Converters.supports(Duration.class)).isTrue();
assertThat(Jsr310Converters.supports(Period.class)).isTrue();

assertThat(Jsr310Converters.supports(OffsetTime.class)).isTrue();
assertThat(Jsr310Converters.supports(OffsetDateTime.class)).isTrue();
}
}
Loading

0 comments on commit 76c1830

Please sign in to comment.