From: Kai Moritz Date: Mon, 21 Dec 2015 22:39:45 +0000 (+0100) Subject: Implemented converters from TemporalAmount into Long for various units X-Git-Tag: jpa-converters-1.0.0~23 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=c0f5359d7eefa5256a45fe67b272a6b5e52d24e6;p=jpa-converters Implemented converters from TemporalAmount into Long for various units --- diff --git a/src/main/java/de/juplo/jpa/converters/TemporalAmountToDaysConverter.java b/src/main/java/de/juplo/jpa/converters/TemporalAmountToDaysConverter.java new file mode 100644 index 0000000..f4ca829 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/TemporalAmountToDaysConverter.java @@ -0,0 +1,28 @@ +package de.juplo.jpa.converters; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link TemporalAmount} into a {@link Long} representing days. + * @author Kai Moritz + */ +@Converter +public class TemporalAmountToDaysConverter implements AttributeConverter +{ + @Override + public Long convertToDatabaseColumn(TemporalAmount amount) + { + return amount.get(ChronoUnit.DAYS); + } + + @Override + public TemporalAmount convertToEntityAttribute(Long days) + { + return Duration.ofDays(days); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/TemporalAmountToHoursConverter.java b/src/main/java/de/juplo/jpa/converters/TemporalAmountToHoursConverter.java new file mode 100644 index 0000000..dc4cf1f --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/TemporalAmountToHoursConverter.java @@ -0,0 +1,28 @@ +package de.juplo.jpa.converters; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link TemporalAmount} into a {@link Long} representing hours. + * @author Kai Moritz + */ +@Converter +public class TemporalAmountToHoursConverter implements AttributeConverter +{ + @Override + public Long convertToDatabaseColumn(TemporalAmount amount) + { + return amount.get(ChronoUnit.HOURS); + } + + @Override + public TemporalAmount convertToEntityAttribute(Long hours) + { + return Duration.ofHours(hours); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/TemporalAmountToMicrosConverter.java b/src/main/java/de/juplo/jpa/converters/TemporalAmountToMicrosConverter.java new file mode 100644 index 0000000..328b76d --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/TemporalAmountToMicrosConverter.java @@ -0,0 +1,29 @@ +package de.juplo.jpa.converters; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link TemporalAmount} into a {@link Long} representing + * microseconds. + * @author Kai Moritz + */ +@Converter +public class TemporalAmountToMicrosConverter implements AttributeConverter +{ + @Override + public Long convertToDatabaseColumn(TemporalAmount amount) + { + return amount.get(ChronoUnit.MICROS); + } + + @Override + public TemporalAmount convertToEntityAttribute(Long micros) + { + return Duration.of(micros, ChronoUnit.MICROS); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/TemporalAmountToMillisConverter.java b/src/main/java/de/juplo/jpa/converters/TemporalAmountToMillisConverter.java new file mode 100644 index 0000000..dc3b799 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/TemporalAmountToMillisConverter.java @@ -0,0 +1,29 @@ +package de.juplo.jpa.converters; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link TemporalAmount} into a {@link Long} representing + * milliseconds. + * @author Kai Moritz + */ +@Converter +public class TemporalAmountToMillisConverter implements AttributeConverter +{ + @Override + public Long convertToDatabaseColumn(TemporalAmount amount) + { + return amount.get(ChronoUnit.MILLIS); + } + + @Override + public TemporalAmount convertToEntityAttribute(Long millis) + { + return Duration.ofMillis(millis); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/TemporalAmountToMinutesConverter.java b/src/main/java/de/juplo/jpa/converters/TemporalAmountToMinutesConverter.java new file mode 100644 index 0000000..87907be --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/TemporalAmountToMinutesConverter.java @@ -0,0 +1,28 @@ +package de.juplo.jpa.converters; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link TemporalAmount} into a {@link Long} representing minutes. + * @author Kai Moritz + */ +@Converter +public class TemporalAmountToMinutesConverter implements AttributeConverter +{ + @Override + public Long convertToDatabaseColumn(TemporalAmount amount) + { + return amount.get(ChronoUnit.MINUTES); + } + + @Override + public TemporalAmount convertToEntityAttribute(Long minutes) + { + return Duration.ofMinutes(minutes); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/TemporalAmountToNanosConverter.java b/src/main/java/de/juplo/jpa/converters/TemporalAmountToNanosConverter.java new file mode 100644 index 0000000..d38a1db --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/TemporalAmountToNanosConverter.java @@ -0,0 +1,29 @@ +package de.juplo.jpa.converters; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link TemporalAmount} into a {@link Long} representing + * nanoseconds. + * @author Kai Moritz + */ +@Converter +public class TemporalAmountToNanosConverter implements AttributeConverter +{ + @Override + public Long convertToDatabaseColumn(TemporalAmount amount) + { + return amount.get(ChronoUnit.NANOS); + } + + @Override + public TemporalAmount convertToEntityAttribute(Long nanos) + { + return Duration.ofNanos(nanos); + } +} diff --git a/src/main/java/de/juplo/jpa/converters/TemporalAmountToSecondsConverter.java b/src/main/java/de/juplo/jpa/converters/TemporalAmountToSecondsConverter.java new file mode 100644 index 0000000..bd9fc13 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/TemporalAmountToSecondsConverter.java @@ -0,0 +1,28 @@ +package de.juplo.jpa.converters; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAmount; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link TemporalAmount} into a {@link Long} representing seconds. + * @author Kai Moritz + */ +@Converter +public class TemporalAmountToSecondsConverter implements AttributeConverter +{ + @Override + public Long convertToDatabaseColumn(TemporalAmount amount) + { + return amount.get(ChronoUnit.SECONDS); + } + + @Override + public TemporalAmount convertToEntityAttribute(Long seconds) + { + return Duration.ofSeconds(seconds); + } +}