KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > CharacterArrayClobType


1 //$Id: CharacterArrayClobType.java,v 1.3 2005/04/18 17:41:47 epbernard Exp $
2
package org.hibernate.type;
3
4 import org.hibernate.HibernateException;
5 import org.hibernate.util.ArrayHelper;
6 import org.hibernate.usertype.UserType;
7
8 import java.io.CharArrayReader JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.Reader JavaDoc;
11 import java.io.Serializable JavaDoc;
12 import java.sql.PreparedStatement JavaDoc;
13 import java.sql.ResultSet JavaDoc;
14 import java.sql.SQLException JavaDoc;
15 import java.sql.Types JavaDoc;
16 import java.util.ArrayList JavaDoc;
17
18 /**
19  * Map a Character[] to a Clob
20  * Experimental
21  *
22  * @author Emmanuel Bernard
23  */

24 public class CharacterArrayClobType implements UserType {
25     public static final int BUFFER_SIZE = 4096;
26
27     public int[] sqlTypes() {
28         return new int[] { Types.CLOB };
29     }
30
31     public Class JavaDoc returnedClass() {
32         return Character JavaDoc[].class;
33     }
34
35     public boolean equals(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
36         if (x == y) return true;
37         if (x == null || y == null) return false;
38         if ( x instanceof Character JavaDoc[] ) {
39             Object JavaDoc[] o1 = ( Object JavaDoc[] ) x;
40             Object JavaDoc[] o2 = ( Object JavaDoc[] ) y;
41             return ArrayHelper.isEquals(o1, o2);
42         }
43         else {
44             char[] c1 = ( char[] ) x;
45             char[] c2 = ( char[] ) y;
46             return ArrayHelper.isEquals(c1, c2);
47         }
48     }
49
50     public int hashCode(Object JavaDoc x) throws HibernateException {
51         if ( x instanceof Character JavaDoc[] ) {
52             Object JavaDoc[] o = ( Object JavaDoc[] ) x;
53             return ArrayHelper.hash(o);
54         } else {
55             char[] c = ( char[] ) x;
56             return ArrayHelper.hash(c);
57         }
58     }
59
60     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, Object JavaDoc owner) throws HibernateException, SQLException JavaDoc {
61         Reader JavaDoc reader = rs.getCharacterStream( names[0] );
62         if (reader == null) return null;
63         ArrayList JavaDoc result = new ArrayList JavaDoc();
64         try {
65             char[] charbuf = new char[BUFFER_SIZE];
66             for (int i = reader.read(charbuf) ; i > 0 ; i = reader.read(charbuf) ) {
67                 result.ensureCapacity( result.size() + BUFFER_SIZE );
68                 for (int charIndex = 0 ; charIndex < i ; charIndex++) {
69                     result.add( new Character JavaDoc( charbuf[charIndex] ) );
70                 }
71             }
72         } catch (IOException JavaDoc e) {
73             throw new SQLException JavaDoc( e.getMessage() );
74         }
75         if ( returnedClass().equals( Character JavaDoc[].class ) ) {
76             return result.toArray( new Character JavaDoc[ result.size() ] );
77         }
78         else {
79             //very suboptimal
80
int length = result.size();
81             char[] chars = new char[length];
82             for (int index = 0 ; index < length ; index++) {
83                 chars[index] = ( (Character JavaDoc) result.get(index)).charValue();
84             }
85             return chars;
86         }
87     }
88
89     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
90         if (value != null) {
91             char[] chars;
92             if ( value instanceof Character JavaDoc[] ) {
93                 Character JavaDoc[] character = ( Character JavaDoc[] ) value;
94                 int length = character.length;
95                 chars = new char[length];
96                 for (int i = 0 ; i < length ; i++) {
97                     chars[i] = character[i].charValue();
98                 }
99             }
100             else {
101                 chars = ( char[] ) value;
102             }
103             CharArrayReader JavaDoc reader = new CharArrayReader JavaDoc(chars);
104             st.setCharacterStream( index, reader, chars.length );
105         }
106         else {
107             st.setNull( index, sqlTypes()[0] );
108         }
109     }
110
111     public Object JavaDoc deepCopy(Object JavaDoc value) throws HibernateException {
112         if (value == null) return null;
113         if ( value instanceof Character JavaDoc[] ) {
114             Character JavaDoc[] array = ( Character JavaDoc[] ) value;
115             int length = array.length;
116             Character JavaDoc[] copy = new Character JavaDoc[length];
117             for (int index = 0 ; index < length ; index++) {
118                 copy[index] = new Character JavaDoc( array[index].charValue() );
119             }
120             return copy;
121         }
122         else {
123             char[] array = ( char[] ) value;
124             int length = array.length;
125             char[] copy = new char[length];
126             System.arraycopy(array, 0, copy, 0, length);
127             return copy;
128         }
129     }
130
131     public boolean isMutable() {
132         return true;
133     }
134
135     public Serializable JavaDoc disassemble(Object JavaDoc value) throws HibernateException {
136         return (Serializable JavaDoc) deepCopy(value);
137     }
138
139     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException {
140         return deepCopy(cached);
141     }
142
143     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, Object JavaDoc owner) throws HibernateException {
144         return deepCopy(original);
145     }
146 }
147
Popular Tags