From c6566a9706d8642d33eabceab0dbd679ed477ff6 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 21 Dec 2015 23:45:00 +0100 Subject: [PATCH] Implemented to-String converters for MonthDay, YearMonth and ZoneOffset --- .../jpa/converters/MonthDayConverter.java | 26 +++++++++++++++++++ .../jpa/converters/YearMonthConverter.java | 26 +++++++++++++++++++ .../jpa/converters/ZoneOffsetConverter.java | 26 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 src/main/java/de/juplo/jpa/converters/MonthDayConverter.java create mode 100644 src/main/java/de/juplo/jpa/converters/YearMonthConverter.java create mode 100644 src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java 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 index 0000000..143ced1 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/MonthDayConverter.java @@ -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 +{ + @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 index 0000000..47718d9 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/YearMonthConverter.java @@ -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 +{ + @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 index 0000000..f948b54 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/ZoneOffsetConverter.java @@ -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 +{ + @Override + public String convertToDatabaseColumn(ZoneOffset offset) + { + return offset.getId(); + } + + @Override + public ZoneOffset convertToEntityAttribute(String string) + { + return ZoneOffset.of(string); + } +} -- 2.20.1