1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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="ShortType.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class ShortType implements Serializable, UserType {
34  
35      public final static short DEFAULT_VALUE = 0;
36  
37      public final static int[] SQL_TYPES = new int[] {Types.SMALLINT};
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          Short value = (Short)Hibernate.SHORT.nullSafeGet(rs, names[0]);
75  
76          if (value == null) {
77              return new Short(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 Short(DEFAULT_VALUE);
89          }
90  
91          Hibernate.SHORT.nullSafeSet(ps, obj, index);
92      }
93  
94      public Object replace(Object original, Object target, Object owner) {
95          return original;
96      }
97  
98      public Class<Short> returnedClass() {
99          return Short.class;
100     }
101 
102     public int[] sqlTypes() {
103         return SQL_TYPES;
104     }
105 
106 }