View Javadoc

1   /*
2    *  Copyright 2001-2011 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.time.contrib.hibernate;
17  
18  import java.io.Serializable;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.sql.Types;
23  
24  import org.hibernate.HibernateException;
25  import org.hibernate.type.StandardBasicTypes;
26  import org.hibernate.usertype.UserType;
27  
28  /**
29   * @author gjoseph
30   */
31  public abstract class AbstractStringBasedJodaType implements UserType, Serializable {
32  
33      private static final int[] SQL_TYPES = new int[] { Types.VARCHAR };
34  
35      public int[] sqlTypes() {
36          return SQL_TYPES;
37      }
38  
39      public Object nullSafeGet(ResultSet resultSet, String[] strings, Object object) throws HibernateException, SQLException {
40          String s = (String) StandardBasicTypes.STRING.nullSafeGet(resultSet, strings[0]);
41          if (s == null) {
42              return null;
43          }
44  
45          return fromNonNullString(s);
46      }
47  
48      protected abstract Object fromNonNullString(String s);
49  
50      public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
51          if (value == null) {
52              StandardBasicTypes.STRING.nullSafeSet(preparedStatement, null, index);
53          } else {
54              StandardBasicTypes.STRING.nullSafeSet(preparedStatement, toNonNullString(value), index);
55          }
56      }
57  
58      protected abstract String toNonNullString(Object value);
59  
60      public boolean equals(Object x, Object y) throws HibernateException {
61          if (x == y) {
62              return true;
63          }
64          if (x == null || y == null) {
65              return false;
66          }
67          return x.equals(y);
68      }
69  
70      public int hashCode(Object object) throws HibernateException {
71          return object.hashCode();
72      }
73  
74      public Object deepCopy(Object value) throws HibernateException {
75          return value;
76      }
77  
78      public boolean isMutable() {
79          return false;
80      }
81  
82      public Serializable disassemble(Object value) throws HibernateException {
83          return (Serializable) value;
84      }
85  
86      public Object assemble(Serializable cached, Object value) throws HibernateException {
87          return cached;
88      }
89  
90      public Object replace(Object original, Object target, Object owner) throws HibernateException {
91          return original;
92      }
93  
94  }