Skip to content

Commit 3125bbf

Browse files
Barak Schusterodrotbohm
Barak Schuster
authored andcommitted
DATACMNS-951 - Added Converter implementations for JSR-310 Duration and Period.
Original pull request: #186.
1 parent 8062698 commit 3125bbf

File tree

2 files changed

+93
-11
lines changed

2 files changed

+93
-11
lines changed

src/main/java/org/springframework/data/convert/Jsr310Converters.java

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import static java.time.LocalDateTime.*;
2020
import static java.time.ZoneId.*;
2121

22-
import java.time.Instant;
23-
import java.time.LocalDate;
24-
import java.time.LocalDateTime;
25-
import java.time.LocalTime;
26-
import java.time.ZoneId;
22+
import java.time.*;
2723
import java.util.ArrayList;
2824
import java.util.Arrays;
2925
import java.util.Collection;
@@ -66,6 +62,10 @@ public abstract class Jsr310Converters {
6662
converters.add(InstantToDateConverter.INSTANCE);
6763
converters.add(ZoneIdToStringConverter.INSTANCE);
6864
converters.add(StringToZoneIdConverter.INSTANCE);
65+
converters.add(DurationToStringConverter.INSTANCE);
66+
converters.add(StringToDurationConverter.INSTANCE);
67+
converters.add(PeriodToStringConverter.INSTANCE);
68+
converters.add(StringToPeriodConverter.INSTANCE);
6969

7070
return converters;
7171
}
@@ -181,4 +181,58 @@ public ZoneId convert(String source) {
181181
return ZoneId.of(source);
182182
}
183183
}
184+
185+
/**
186+
* enable jsr-310 {@link java.time.Duration} write
187+
*/
188+
@WritingConverter
189+
public static enum DurationToStringConverter implements Converter<Duration, String> {
190+
INSTANCE;
191+
192+
@Override
193+
public String convert(Duration duration) {
194+
return duration.toString();
195+
}
196+
}
197+
198+
/**
199+
* enable jsr-310 {@link java.time.Duration} read
200+
*/
201+
@ReadingConverter
202+
public static enum StringToDurationConverter implements Converter<String, Duration> {
203+
204+
INSTANCE;
205+
206+
@Override
207+
public Duration convert(String s) {
208+
return Duration.parse(s);
209+
}
210+
}
211+
212+
/**
213+
* enable jsr-310 {@link java.time.Period} write
214+
*/
215+
@WritingConverter
216+
public static enum PeriodToStringConverter implements Converter<Period, String> {
217+
INSTANCE;
218+
219+
@Override
220+
public String convert(Period period) {
221+
return period.toString();
222+
}
223+
}
224+
225+
/**
226+
* enable jsr-310 {@link java.time.Period} read
227+
*/
228+
@ReadingConverter
229+
public static enum StringToPeriodConverter implements Converter<String, Period> {
230+
231+
INSTANCE;
232+
233+
@Override
234+
public Period convert(String s) {
235+
return Period.parse(s);
236+
}
237+
}
184238
}

src/test/java/org/springframework/data/convert/Jsr310ConvertersUnitTests.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import static org.junit.Assert.*;
2020

2121
import java.text.SimpleDateFormat;
22-
import java.time.Instant;
23-
import java.time.LocalDate;
24-
import java.time.LocalDateTime;
25-
import java.time.LocalTime;
26-
import java.time.ZoneId;
22+
import java.time.*;
2723
import java.util.Date;
2824
import java.util.HashMap;
2925
import java.util.Map;
@@ -37,7 +33,7 @@
3733
/**
3834
* Unit tests for {@link Jsr310Converters}.
3935
*
40-
* @author Oliver Gierke
36+
* @author Oliver Gierke & Barak Schoster
4137
*/
4238
public class Jsr310ConvertersUnitTests {
4339

@@ -143,6 +139,38 @@ public void convertsZoneIdToStringAndBack() {
143139
}
144140
}
145141

142+
@Test
143+
public void convertsDurationToStringAndBack() {
144+
145+
Map<String, Duration> ids = new HashMap<String, Duration>();
146+
ids.put("PT240H", Duration.ofDays(10));
147+
ids.put("PT2H", Duration.ofHours(2));
148+
ids.put("PT3M", Duration.ofMinutes(3));
149+
ids.put("PT4S", Duration.ofSeconds(4));
150+
ids.put("PT0.005S", Duration.ofMillis(5));
151+
ids.put("PT0.000000006S", Duration.ofNanos(6));
152+
153+
154+
for (Entry<String, Duration> entry : ids.entrySet()) {
155+
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
156+
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), Duration.class), is(entry.getValue()));
157+
}
158+
}
159+
160+
@Test
161+
public void convertsPeriodToStringAndBack() {
162+
163+
Map<String, Period> ids = new HashMap<String, Period>();
164+
ids.put("P2D", Period.ofDays(2));
165+
ids.put("P21D", Period.ofWeeks(3));
166+
ids.put("P4M", Period.ofMonths(4));
167+
ids.put("P5Y", Period.ofYears(5));
168+
169+
for (Entry<String, Period> entry : ids.entrySet()) {
170+
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
171+
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), Period.class), is(entry.getValue()));
172+
}
173+
}
146174
private static String format(Date date, String format) {
147175
return new SimpleDateFormat(format).format(date);
148176
}

0 commit comments

Comments
 (0)