Implemented converters from TemporalAmount into Long for various units
authorKai Moritz <kai@juplo.de>
Mon, 21 Dec 2015 22:39:45 +0000 (23:39 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 15 Aug 2017 17:44:40 +0000 (19:44 +0200)
src/main/java/de/juplo/jpa/converters/TemporalAmountToDaysConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/TemporalAmountToHoursConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/TemporalAmountToMicrosConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/TemporalAmountToMillisConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/TemporalAmountToMinutesConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/TemporalAmountToNanosConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/TemporalAmountToSecondsConverter.java [new file with mode: 0644]

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 (file)
index 0000000..f4ca829
--- /dev/null
@@ -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<TemporalAmount, Long>
+{
+  @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 (file)
index 0000000..dc4cf1f
--- /dev/null
@@ -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<TemporalAmount, Long>
+{
+  @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 (file)
index 0000000..328b76d
--- /dev/null
@@ -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<TemporalAmount, Long>
+{
+  @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 (file)
index 0000000..dc3b799
--- /dev/null
@@ -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<TemporalAmount, Long>
+{
+  @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 (file)
index 0000000..87907be
--- /dev/null
@@ -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<TemporalAmount, Long>
+{
+  @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 (file)
index 0000000..d38a1db
--- /dev/null
@@ -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<TemporalAmount, Long>
+{
+  @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 (file)
index 0000000..bd9fc13
--- /dev/null
@@ -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<TemporalAmount, Long>
+{
+  @Override
+  public Long convertToDatabaseColumn(TemporalAmount amount)
+  {
+    return amount.get(ChronoUnit.SECONDS);
+  }
+
+  @Override
+  public TemporalAmount convertToEntityAttribute(Long seconds)
+  {
+    return Duration.ofSeconds(seconds);
+  }
+}