KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > persistence > EnumUserType


1 package org.hibernate.ce.auction.persistence;
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.hibernate.HibernateException;
11 import org.hibernate.usertype.EnhancedUserType;
12 import org.hibernate.usertype.ParameterizedType;
13
14 /** Cool:
15  * for (Direction direction : EnumSet.range(Direction.North, Direction.South)){
16       log("North-south: " + direction);
17     }
18
19      http://www.javapractices.com/Topic1.cjp
20
21  * @author Gavin King
22  */

23 public class EnumUserType implements EnhancedUserType, ParameterizedType {
24
25     private Class JavaDoc<Enum JavaDoc> enumClass;
26
27     public void setParameterValues(Properties JavaDoc parameters) {
28         String JavaDoc enumClassName = parameters.getProperty("enumClassName");
29         try {
30             enumClass = (Class JavaDoc<Enum JavaDoc>) Class.forName(enumClassName);
31         }
32         catch (ClassNotFoundException JavaDoc cnfe) {
33             throw new HibernateException("Enum class not found", cnfe);
34         }
35     }
36
37     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner)
38     throws HibernateException {
39         return cached;
40     }
41
42     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
43         return value;
44     }
45
46     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
47         return (Enum JavaDoc) value;
48     }
49
50     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
51         return x==y;
52     }
53
54     public int hashCode(Object JavaDoc x) throws HibernateException {
55         return x.hashCode();
56     }
57
58     public boolean isMutable() {
59         return false;
60     }
61
62     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner)
63     throws HibernateException, SQLException JavaDoc {
64         String JavaDoc name = rs.getString( names[0] );
65         return rs.wasNull() ? null : Enum.valueOf(enumClass, name);
66     }
67
68     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index)
69     throws HibernateException, SQLException JavaDoc {
70         if (value==null) {
71             st.setNull(index, Types.VARCHAR);
72         }
73         else {
74             st.setString( index, ( (Enum JavaDoc) value ).name() );
75         }
76     }
77
78     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner)
79     throws HibernateException {
80         return original;
81     }
82
83     public Class JavaDoc returnedClass() {
84         return enumClass;
85     }
86
87     public int[] sqlTypes() {
88         return new int[] { Types.VARCHAR };
89     }
90
91     public Object JavaDoc fromXMLString(String JavaDoc xmlValue) {
92         return Enum.valueOf(enumClass, xmlValue);
93     }
94
95     public String JavaDoc objectToSQLString(Object JavaDoc value) {
96         return '\'' + ( (Enum JavaDoc) value ).name() + '\'';
97     }
98
99     public String JavaDoc toXMLString(Object JavaDoc value) {
100         return ( (Enum JavaDoc) value ).name();
101     }
102
103 }
104
Popular Tags