1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.dao.orm.hibernate;
21  
22  import java.io.Serializable;
23  
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  import java.sql.Types;
28  
29  import org.hibernate.Hibernate;
30  import org.hibernate.HibernateException;
31  import org.hibernate.usertype.UserType;
32  
33  /**
34   * <a href="ShortType.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class ShortType implements UserType {
40  
41      public final static short DEFAULT_VALUE = 0;
42  
43      public final static int[] SQL_TYPES = new int[] {Types.SMALLINT};
44  
45      public Object assemble(Serializable cached, Object owner) {
46          return cached;
47      }
48  
49      public Object deepCopy(Object obj) {
50          return obj;
51      }
52  
53      public Serializable disassemble(Object value) {
54          return (Serializable)value;
55      }
56  
57      public boolean equals(Object x, Object y) {
58          if (x == y) {
59              return true;
60          }
61          else if (x == null || y == null) {
62              return false;
63          }
64          else {
65              return x.equals(y);
66          }
67      }
68  
69      public int hashCode(Object x) {
70          return x.hashCode();
71      }
72  
73      public boolean isMutable() {
74          return false;
75      }
76  
77      public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
78          throws HibernateException, SQLException {
79  
80          Short value = (Short)Hibernate.SHORT.nullSafeGet(rs, names[0]);
81  
82          if (value == null) {
83              return new Short(DEFAULT_VALUE);
84          }
85          else {
86              return value;
87          }
88      }
89  
90      public void nullSafeSet(PreparedStatement ps, Object obj, int index)
91          throws HibernateException, SQLException {
92  
93          if (obj == null) {
94              obj = new Short(DEFAULT_VALUE);
95          }
96  
97          Hibernate.SHORT.nullSafeSet(ps, obj, index);
98      }
99  
100     public Object replace(Object original, Object target, Object owner) {
101         return original;
102     }
103 
104     public Class<Short> returnedClass() {
105         return Short.class;
106     }
107 
108     public int[] sqlTypes() {
109         return SQL_TYPES;
110     }
111 
112 }