Prepared for the development of the next release 1.0.1
[jpa-converters] / src / main / java / de / juplo / jpa / converters / DurationConverter.java
1 package de.juplo.jpa.converters;
2
3 import java.time.Duration;
4 import javax.persistence.AttributeConverter;
5 import javax.persistence.Converter;
6
7
8 /**
9  * Converts a {@link Duration} into its {@link String} representation.
10  * @author Kai Moritz
11  */
12 @Converter
13 public class DurationConverter implements AttributeConverter<Duration, String>
14 {
15   @Override
16   public String convertToDatabaseColumn(Duration duration)
17   {
18     if (duration == null)
19       return null;
20     return duration.toString();
21   }
22
23   @Override
24   public Duration convertToEntityAttribute(String string)
25   {
26     if (string == null)
27       return null;
28     return Duration.parse(string);
29   }
30 }