DATACMNS-773 - Added support for persisting ZoneId instances as Strings.

We now ship converters for JSR-310 and ThreeTenBP's ZoneId conversion to String and back.
This commit is contained in:
Oliver Gierke
2015-10-05 18:03:42 +02:00
parent be47572e56
commit 828c19a4e5
4 changed files with 86 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 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.
@@ -23,6 +23,7 @@ import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -63,6 +64,8 @@ public abstract class Jsr310Converters {
converters.add(LocalTimeToDateConverter.INSTANCE);
converters.add(DateToInstantConverter.INSTANCE);
converters.add(InstantToDateConverter.INSTANCE);
converters.add(ZoneIdToStringConverter.INSTANCE);
converters.add(StringToZoneIdConverter.INSTANCE);
return converters;
}
@@ -156,4 +159,26 @@ public abstract class Jsr310Converters {
return source == null ? null : Date.from(source.atZone(systemDefault()).toInstant());
}
}
@WritingConverter
public static enum ZoneIdToStringConverter implements Converter<ZoneId, String> {
INSTANCE;
@Override
public String convert(ZoneId source) {
return source.toString();
}
}
@ReadingConverter
public static enum StringToZoneIdConverter implements Converter<String, ZoneId> {
INSTANCE;
@Override
public ZoneId convert(String source) {
return ZoneId.of(source);
}
}
}

View File

@@ -33,6 +33,7 @@ import org.threeten.bp.Instant;
import org.threeten.bp.LocalDate;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.LocalTime;
import org.threeten.bp.ZoneId;
/**
* Helper class to register {@link Converter} implementations for the ThreeTen Backport project in case it's present on
@@ -67,6 +68,8 @@ public abstract class ThreeTenBackPortConverters {
converters.add(LocalTimeToDateConverter.INSTANCE);
converters.add(DateToInstantConverter.INSTANCE);
converters.add(InstantToDateConverter.INSTANCE);
converters.add(ZoneIdToStringConverter.INSTANCE);
converters.add(StringToZoneIdConverter.INSTANCE);
return converters;
}
@@ -161,4 +164,26 @@ public abstract class ThreeTenBackPortConverters {
return source == null ? null : toDate(source.atZone(systemDefault()).toInstant());
}
}
@WritingConverter
public static enum ZoneIdToStringConverter implements Converter<ZoneId, String> {
INSTANCE;
@Override
public String convert(ZoneId source) {
return source.toString();
}
}
@ReadingConverter
public static enum StringToZoneIdConverter implements Converter<String, ZoneId> {
INSTANCE;
@Override
public ZoneId convert(String source) {
return ZoneId.of(source);
}
}
}

View File

@@ -23,7 +23,11 @@ import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
import org.springframework.core.convert.ConversionService;
@@ -126,6 +130,19 @@ public class Jsr310ConvertersUnitTests {
assertThat(CONVERSION_SERVICE.convert(now.toInstant(), Date.class), is(now));
}
@Test
public void convertsZoneIdToStringAndBack() {
Map<String, ZoneId> ids = new HashMap<String, ZoneId>();
ids.put("Europe/Berlin", ZoneId.of("Europe/Berlin"));
ids.put("+06:00", ZoneId.of("+06:00"));
for (Entry<String, ZoneId> entry : ids.entrySet()) {
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), ZoneId.class), is(entry.getValue()));
}
}
private static String format(Date date, String format) {
return new SimpleDateFormat(format).format(date);
}

View File

@@ -21,6 +21,9 @@ import static org.threeten.bp.DateTimeUtils.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
import org.springframework.core.convert.ConversionService;
@@ -30,12 +33,13 @@ import org.threeten.bp.Instant;
import org.threeten.bp.LocalDate;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.LocalTime;
import org.threeten.bp.ZoneId;
/**
* Unit tests for {@link ThreeTenBackPortConverters}.
*
* @author Oliver Gierke
* @ssince 1.10
* @since 1.10
*/
public class ThreeTenBackPortConvertersUnitTests {
@@ -128,6 +132,19 @@ public class ThreeTenBackPortConvertersUnitTests {
assertThat(CONVERSION_SERVICE.convert(toInstant(now), Date.class), is(now));
}
@Test
public void convertsZoneIdToStringAndBack() {
Map<String, ZoneId> ids = new HashMap<String, ZoneId>();
ids.put("Europe/Berlin", ZoneId.of("Europe/Berlin"));
ids.put("+06:00", ZoneId.of("+06:00"));
for (Entry<String, ZoneId> entry : ids.entrySet()) {
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), ZoneId.class), is(entry.getValue()));
}
}
private static String format(Date date, String format) {
return new SimpleDateFormat(format).format(date);
}