KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > typeparameters > DefaultValueIntegerType


1 package org.hibernate.test.typeparameters;
2
3 import java.io.Serializable JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.sql.Types JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import org.apache.commons.logging.LogFactory;
11 import org.hibernate.HibernateException;
12 import org.hibernate.usertype.ParameterizedType;
13 import org.hibernate.usertype.UserType;
14
15
16 /**
17  * @author Michi
18  */

19 public class DefaultValueIntegerType implements UserType, ParameterizedType, Serializable JavaDoc {
20
21     private Integer JavaDoc defaultValue;
22
23     public int[] sqlTypes() {
24         return new int[] {Types.INTEGER};
25     }
26
27     public Class JavaDoc returnedClass() {
28         return int.class;
29     }
30
31     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
32         if (x==y) return true;
33         if (x==null || y==null) return false;
34         return x.equals(y);
35     }
36
37     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner) throws HibernateException, SQLException JavaDoc {
38         Number JavaDoc result = (Number JavaDoc) rs.getObject(names[0]);
39         return result==null ? defaultValue : new Integer JavaDoc(result.intValue());
40     }
41
42     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
43         if (value == null || defaultValue.equals(value) ) {
44             LogFactory.getLog( getClass() ).trace("binding null to parameter: " + index);
45             st.setNull(index, Types.INTEGER);
46         } else {
47             LogFactory.getLog( getClass() ).trace("binding " + value + " to parameter: " + index);
48             st.setInt(index, ((Integer JavaDoc)value).intValue());
49         }
50     }
51
52     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
53         return new Integer JavaDoc(((Integer JavaDoc)value).intValue());
54     }
55
56     public boolean isMutable() {
57         return false;
58     }
59
60     public int hashCode(Object JavaDoc x) throws HibernateException {
61         return x.hashCode();
62     }
63
64     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner)
65     throws HibernateException {
66         return cached;
67     }
68
69     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
70         return (Serializable JavaDoc) value;
71     }
72
73     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner)
74     throws HibernateException {
75         return original;
76     }
77
78     public void setParameterValues(Properties JavaDoc parameters) {
79         this.defaultValue = Integer.valueOf((String JavaDoc) parameters.get("default"));
80     }
81
82 }
83
Popular Tags