Implemented a Locale-to-String converter
authorKai Moritz <kai@juplo.de>
Mon, 21 Dec 2015 23:19:45 +0000 (00:19 +0100)
committerKai Moritz <kai@juplo.de>
Tue, 15 Aug 2017 17:44:44 +0000 (19:44 +0200)
src/main/java/de/juplo/jpa/converters/LocaleConverter.java [new file with mode: 0644]

diff --git a/src/main/java/de/juplo/jpa/converters/LocaleConverter.java b/src/main/java/de/juplo/jpa/converters/LocaleConverter.java
new file mode 100644 (file)
index 0000000..da84d15
--- /dev/null
@@ -0,0 +1,27 @@
+package de.juplo.jpa.converters;
+
+import java.util.Locale;
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+
+/**
+ * Converts a {@link Locale} into its {@link String} representation as a
+ * well-formed IETF BCP 47 language tag.
+ * @author Kai Moritz
+ */
+@Converter(autoApply = true)
+public class LocaleConverter implements AttributeConverter<Locale, String>
+{
+  @Override
+  public String convertToDatabaseColumn(Locale locale)
+  {
+    return locale.toLanguageTag();
+  }
+
+  @Override
+  public Locale convertToEntityAttribute(String string)
+  {
+    return Locale.forLanguageTag(string);
+  }
+}