| ShortType.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions 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="ShortType.java.html"><b><i>View Source</i></b></a>
38 *
39 * @author Brian Wing Shun Chan
40 *
41 */
42 public class ShortType implements UserType {
43
44 public final static short DEFAULT_VALUE = 0;
45
46 public final static int[] SQL_TYPES = new int[] {Types.SMALLINT};
47
48 public Object assemble(Serializable cached, Object owner) {
49 return cached;
50 }
51
52 public Object deepCopy(Object obj) {
53 return obj;
54 }
55
56 public Serializable disassemble(Object value) {
57 return (Serializable)value;
58 }
59
60 public boolean equals(Object x, Object y) {
61 if (x == y) {
62 return true;
63 }
64 else if (x == null || y == null) {
65 return false;
66 }
67 else {
68 return x.equals(y);
69 }
70 }
71
72 public int hashCode(Object x) {
73 return x.hashCode();
74 }
75
76 public boolean isMutable() {
77 return false;
78 }
79
80 public Object nullSafeGet(ResultSet rs, String[] names, Object obj)
81 throws HibernateException, SQLException {
82
83 Short value = (Short)Hibernate.SHORT.nullSafeGet(rs, names[0]);
84
85 if (value == null) {
86 return new Short(DEFAULT_VALUE);
87 }
88 else {
89 return value;
90 }
91 }
92
93 public void nullSafeSet(PreparedStatement ps, Object obj, int index)
94 throws HibernateException, SQLException {
95
96 if (obj == null) {
97 obj = new Short(DEFAULT_VALUE);
98 }
99
100 Hibernate.SHORT.nullSafeSet(ps, obj, index);
101 }
102
103 public Object replace(Object original, Object target, Object owner) {
104 return original;
105 }
106
107 public Class<Short> returnedClass() {
108 return Short.class;
109 }
110
111 public int[] sqlTypes() {
112 return SQL_TYPES;
113 }
114
115 }