1 package com.vladmihalcea.hibernate.type.basic.internal;
2
3 import org.hibernate.type.descriptor.WrapperOptions;
4 import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
5
6 import java.time.Month;
7 import java.util.Objects;
8
9 /**
10  * @author Martin Panzer
11  */

12 public class Iso8601MonthMonthTypeDescriptor
13         extends AbstractTypeDescriptor<Month> {
14
15     public static final Iso8601MonthMonthTypeDescriptor INSTANCE = new Iso8601MonthMonthTypeDescriptor();
16
17     public Iso8601MonthMonthTypeDescriptor() {
18         super(Month.class);
19     }
20
21     @Override
22     public boolean areEqual(Month one, Month another) {
23         return Objects.equals(one, another);
24     }
25
26     @Override
27     public String toString(Month value) {
28         return value.toString();
29     }
30
31     @Override
32     public Month fromString(String string) {
33         return Month.valueOf(string);
34     }
35
36     @SuppressWarnings({"unchecked"})
37     @Override
38     public <X> X unwrap(Month value, Class<X> type, WrapperOptions options) {
39         if (value == null) {
40             return null;
41         }
42         if (Number.class.isAssignableFrom(type)) {
43             return (X) (Number) value.getValue();
44         }
45         throw unknownUnwrap(type);
46     }
47
48     @Override
49     public <X> Month wrap(X value, WrapperOptions options) {
50         if (value == null) {
51             return null;
52         }
53         if (value instanceof Number) {
54             int numericValue = ((Number) (value)).intValue();
55             return Month.of(numericValue);
56         }
57         throw unknownWrap(value.getClass());
58     }
59 }
60