DATACMNS-951 - Added Converter implementations for JSR-310 Duration and Period.
Original pull request: #186.
This commit is contained in:
committed by
Oliver Gierke
parent
e768e8eb5e
commit
5020c7b8bb
@@ -19,11 +19,7 @@ import static java.time.Instant.*;
|
||||
import static java.time.LocalDateTime.*;
|
||||
import static java.time.ZoneId.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -66,6 +62,10 @@ public abstract class Jsr310Converters {
|
||||
converters.add(InstantToDateConverter.INSTANCE);
|
||||
converters.add(ZoneIdToStringConverter.INSTANCE);
|
||||
converters.add(StringToZoneIdConverter.INSTANCE);
|
||||
converters.add(DurationToStringConverter.INSTANCE);
|
||||
converters.add(StringToDurationConverter.INSTANCE);
|
||||
converters.add(PeriodToStringConverter.INSTANCE);
|
||||
converters.add(StringToPeriodConverter.INSTANCE);
|
||||
|
||||
return converters;
|
||||
}
|
||||
@@ -181,4 +181,58 @@ public abstract class Jsr310Converters {
|
||||
return ZoneId.of(source);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* enable jsr-310 {@link java.time.Duration} write
|
||||
*/
|
||||
@WritingConverter
|
||||
public static enum DurationToStringConverter implements Converter<Duration, String> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public String convert(Duration duration) {
|
||||
return duration.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* enable jsr-310 {@link java.time.Duration} read
|
||||
*/
|
||||
@ReadingConverter
|
||||
public static enum StringToDurationConverter implements Converter<String, Duration> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Duration convert(String s) {
|
||||
return Duration.parse(s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* enable jsr-310 {@link java.time.Period} write
|
||||
*/
|
||||
@WritingConverter
|
||||
public static enum PeriodToStringConverter implements Converter<Period, String> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public String convert(Period period) {
|
||||
return period.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* enable jsr-310 {@link java.time.Period} read
|
||||
*/
|
||||
@ReadingConverter
|
||||
public static enum StringToPeriodConverter implements Converter<String, Period> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Period convert(String s) {
|
||||
return Period.parse(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,11 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.*;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -37,7 +33,7 @@ import org.springframework.core.convert.support.GenericConversionService;
|
||||
/**
|
||||
* Unit tests for {@link Jsr310Converters}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Oliver Gierke & Barak Schoster
|
||||
*/
|
||||
public class Jsr310ConvertersUnitTests {
|
||||
|
||||
@@ -143,6 +139,38 @@ public class Jsr310ConvertersUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertsDurationToStringAndBack() {
|
||||
|
||||
Map<String, Duration> ids = new HashMap<String, Duration>();
|
||||
ids.put("PT240H", Duration.ofDays(10));
|
||||
ids.put("PT2H", Duration.ofHours(2));
|
||||
ids.put("PT3M", Duration.ofMinutes(3));
|
||||
ids.put("PT4S", Duration.ofSeconds(4));
|
||||
ids.put("PT0.005S", Duration.ofMillis(5));
|
||||
ids.put("PT0.000000006S", Duration.ofNanos(6));
|
||||
|
||||
|
||||
for (Entry<String, Duration> entry : ids.entrySet()) {
|
||||
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
|
||||
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), Duration.class), is(entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertsPeriodToStringAndBack() {
|
||||
|
||||
Map<String, Period> ids = new HashMap<String, Period>();
|
||||
ids.put("P2D", Period.ofDays(2));
|
||||
ids.put("P21D", Period.ofWeeks(3));
|
||||
ids.put("P4M", Period.ofMonths(4));
|
||||
ids.put("P5Y", Period.ofYears(5));
|
||||
|
||||
for (Entry<String, Period> entry : ids.entrySet()) {
|
||||
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
|
||||
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), Period.class), is(entry.getValue()));
|
||||
}
|
||||
}
|
||||
private static String format(Date date, String format) {
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user