Implemented to-String converters for MonthDay, YearMonth and ZoneOffset
authorKai Moritz <kai@juplo.de>
Mon, 21 Dec 2015 22:45:00 +0000 (23:45 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 15 Aug 2017 17:44:40 +0000 (19:44 +0200)
src/main/java/de/juplo/jpa/converters/MonthDayConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/YearMonthConverter.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java [new file with mode: 0644]

diff --git a/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java b/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java
new file mode 100644 (file)
index 0000000..143ced1
--- /dev/null
@@ -0,0 +1,26 @@
+package de.juplo.jpa.converters;
+
+import java.time.MonthDay;
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+
+/**
+ * Converts a {@link MonthDay} into its {@link String} representation.
+ * @author Kai Moritz
+ */
+@Converter(autoApply = true)
+public class MonthDayConverter implements AttributeConverter<MonthDay, String>
+{
+  @Override
+  public String convertToDatabaseColumn(MonthDay monthday)
+  {
+    return monthday.toString();
+  }
+
+  @Override
+  public MonthDay convertToEntityAttribute(String string)
+  {
+    return MonthDay.parse(string);
+  }
+}
diff --git a/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java b/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java
new file mode 100644 (file)
index 0000000..47718d9
--- /dev/null
@@ -0,0 +1,26 @@
+package de.juplo.jpa.converters;
+
+import java.time.YearMonth;
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+
+/**
+ * Converts a {@link YearMonth} into its {@link String} representation.
+ * @author Kai Moritz
+ */
+@Converter(autoApply = true)
+public class YearMonthConverter implements AttributeConverter<YearMonth, String>
+{
+  @Override
+  public String convertToDatabaseColumn(YearMonth yearmonth)
+  {
+    return yearmonth.toString();
+  }
+
+  @Override
+  public YearMonth convertToEntityAttribute(String string)
+  {
+    return YearMonth.parse(string);
+  }
+}
diff --git a/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java b/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java
new file mode 100644 (file)
index 0000000..f948b54
--- /dev/null
@@ -0,0 +1,26 @@
+package de.juplo.jpa.converters;
+
+import java.time.ZoneOffset;
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+
+/**
+ * Converts a {@link ZoneOffset} into its {@link String} representation as ID.
+ * @author Kai Moritz
+ */
+@Converter(autoApply = true)
+public class ZoneOffsetConverter implements AttributeConverter<ZoneOffset, String>
+{
+  @Override
+  public String convertToDatabaseColumn(ZoneOffset offset)
+  {
+    return offset.getId();
+  }
+
+  @Override
+  public ZoneOffset convertToEntityAttribute(String string)
+  {
+    return ZoneOffset.of(string);
+  }
+}