1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.orm.hibernate;
24  
25  import java.io.Serializable;
26  
27  import java.sql.PreparedStatement;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  import java.sql.Types;
31  
32  import org.hibernate.Hibernate;
33  import org.hibernate.HibernateException;
34  import org.hibernate.usertype.UserType;
35  
36  /**
37   * <a href="FloatType.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class FloatType implements Serializable, UserType {
42  
43      public final static float DEFAULT_VALUE = 0.0F;
44  
45      public final static int[] SQL_TYPES = new int[] {Types.FLOAT};
46  
47      public Object assemble(Serializable cached, Object owner) {
48          return cached;
49      }
50  
51      public Object deepCopy(Object obj) {
52          return obj;
53      }
54  
55      public Serializable disassemble(Object value) {
56          return (Serializable)value;
57      }
58  
59      public boolean equals(Object x, Object y) {
60          if (x == y) {
61              return true;
62          }
63          else if (x == null || y == null) {
64              return false;
65          }
66          else {
67              return x.equals(y);
68          }
69      }
70  
71      public int hashCode(Object x) {
72          return x.hashCode();
73      }
74  
75      public boolean isMutable() {
76          return false;
77      }
78  
79      public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
80          throws HibernateException, SQLException {
81  
82          Float value = (Float)Hibernate.FLOAT.nullSafeGet(rs, names[0]);
83  
84          if (value == null) {
85              return new Float(DEFAULT_VALUE);
86          }
87          else {
88              return value;
89          }
90      }
91  
92      public void nullSafeSet(PreparedStatement ps, Object obj, int index)
93          throws HibernateException, SQLException {
94  
95          if (obj == null) {
96              obj = new Float(DEFAULT_VALUE);
97          }
98  
99          Hibernate.FLOAT.nullSafeSet(ps, obj, index);
100     }
101 
102     public Object replace(Object original, Object target, Object owner) {
103         return original;
104     }
105 
106     public Class<Float> returnedClass() {
107         return Float.class;
108     }
109 
110     public int[] sqlTypes() {
111         return SQL_TYPES;
112     }
113 
114 }