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