Prevented possible NullPointerException's
[jpa-converters] / src / main / java / de / juplo / jpa / converters / YearMonthConverter.java
1 package de.juplo.jpa.converters;
2
3 import java.time.YearMonth;
4 import javax.persistence.AttributeConverter;
5 import javax.persistence.Converter;
6
7
8 /**
9  * Converts a {@link YearMonth} into its {@link String} representation.
10  * @author Kai Moritz
11  */
12 @Converter(autoApply = true)
13 public class YearMonthConverter implements AttributeConverter<YearMonth, String>
14 {
15   @Override
16   public String convertToDatabaseColumn(YearMonth yearmonth)
17   {
18     if (yearmonth == null)
19       return null;
20     return yearmonth.toString();
21   }
22
23   @Override
24   public YearMonth convertToEntityAttribute(String string)
25   {
26     if (string == null)
27       return null;
28     return YearMonth.parse(string);
29   }
30 }