Implemented converters from TemporalAmount into Long for various units
[jpa-converters] / src / main / java / de / juplo / jpa / converters / TemporalAmountToMicrosConverter.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
12  * microseconds.
13  * @author Kai Moritz
14  */
15 @Converter
16 public class TemporalAmountToMicrosConverter implements AttributeConverter<TemporalAmount, Long>
17 {
18   @Override
19   public Long convertToDatabaseColumn(TemporalAmount amount)
20   {
21     return amount.get(ChronoUnit.MICROS);
22   }
23
24   @Override
25   public TemporalAmount convertToEntityAttribute(Long micros)
26   {
27     return Duration.of(micros, ChronoUnit.MICROS);
28   }
29 }