1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import java.io.Serializable;
18  
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.Hibernate;
25  import org.hibernate.HibernateException;
26  import org.hibernate.usertype.UserType;
27  
28  /**
29   * <a href="DoubleType.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class DoubleType implements Serializable, UserType {
34  
35      public static final double DEFAULT_VALUE = 0.0;
36  
37      public static final int[] SQL_TYPES = new int[] {Types.DOUBLE};
38  
39      public Object assemble(Serializable cached, Object owner) {
40          return cached;
41      }
42  
43      public Object deepCopy(Object obj) {
44          return obj;
45      }
46  
47      public Serializable disassemble(Object value) {
48          return (Serializable)value;
49      }
50  
51      public boolean equals(Object x, Object y) {
52          if (x == y) {
53              return true;
54          }
55          else if (x == null || y == null) {
56              return false;
57          }
58          else {
59              return x.equals(y);
60          }
61      }
62  
63      public int hashCode(Object x) {
64          return x.hashCode();
65      }
66  
67      public boolean isMutable() {
68          return false;
69      }
70  
71      public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
72          throws HibernateException, SQLException {
73  
74          Double value = (Double)Hibernate.DOUBLE.nullSafeGet(rs, names[0]);
75  
76          if (value == null) {
77              return new Double(DEFAULT_VALUE);
78          }
79          else {
80              return value;
81          }
82      }
83  
84      public void nullSafeSet(PreparedStatement ps, Object obj, int index)
85          throws HibernateException, SQLException {
86  
87          if (obj == null) {
88              obj = new Double(DEFAULT_VALUE);
89          }
90  
91          Hibernate.DOUBLE.nullSafeSet(ps, obj, index);
92      }
93  
94      public Object replace(Object original, Object target, Object owner) {
95          return original;
96      }
97  
98      public Class<Double> returnedClass() {
99          return Double.class;
100     }
101 
102     public int[] sqlTypes() {
103         return SQL_TYPES;
104     }
105 
106 }