KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ArrayType.java,v 1.25 2005/07/05 17:42:19 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.lang.reflect.Array JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import org.hibernate.EntityMode;
13 import org.hibernate.HibernateException;
14 import org.hibernate.collection.PersistentArrayHolder;
15 import org.hibernate.collection.PersistentCollection;
16 import org.hibernate.engine.SessionFactoryImplementor;
17 import org.hibernate.engine.SessionImplementor;
18 import org.hibernate.persister.collection.CollectionPersister;
19
20 /**
21  * A type for persistent arrays.
22  * @author Gavin King
23  */

24 public class ArrayType extends CollectionType {
25
26     private final Class JavaDoc elementClass;
27     private final Class JavaDoc arrayClass;
28
29     public ArrayType(String JavaDoc role, String JavaDoc propertyRef, Class JavaDoc elementClass, boolean isEmbeddedInXML) {
30         super(role, propertyRef, isEmbeddedInXML);
31         this.elementClass = elementClass;
32         arrayClass = Array.newInstance(elementClass, 0).getClass();
33     }
34
35     public Class JavaDoc getReturnedClass() {
36         return arrayClass;
37     }
38
39     public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable JavaDoc key)
40     throws HibernateException {
41         return new PersistentArrayHolder(session, persister);
42     }
43
44     /**
45      * Not defined for collections of primitive type
46      */

47     public Iterator JavaDoc getElementsIterator(Object JavaDoc collection) {
48         return Arrays.asList( (Object JavaDoc[]) collection ).iterator();
49     }
50
51     public PersistentCollection wrap(SessionImplementor session, Object JavaDoc array) {
52         return new PersistentArrayHolder(session, array);
53     }
54
55     public boolean isArrayType() {
56         return true;
57     }
58
59     public String JavaDoc toLoggableString(Object JavaDoc value, SessionFactoryImplementor factory) throws HibernateException {
60         if (value==null) return "null";
61         int length = Array.getLength(value);
62         List JavaDoc list = new ArrayList JavaDoc(length);
63         Type elemType = getElementType(factory);
64         for ( int i=0; i<length; i++ ) {
65             list.add( elemType.toLoggableString( Array.get(value, i), factory ) );
66         }
67         return list.toString();
68     }
69     
70     public Object JavaDoc instantiate(Object JavaDoc original) {
71         return Array.newInstance( elementClass, Array.getLength(original) );
72     }
73
74     public Object JavaDoc replaceElements(
75         Object JavaDoc original,
76         Object JavaDoc target,
77         Object JavaDoc owner,
78         Map JavaDoc copyCache,
79         SessionImplementor session)
80     throws HibernateException {
81         
82         int length = Array.getLength(original);
83         if ( length!=Array.getLength(target) ) {
84             //note: this affects the return value!
85
target=instantiate(original);
86         }
87         
88         Type elemType = getElementType( session.getFactory() );
89         for ( int i=0; i<length; i++ ) {
90             Array.set( target, i, elemType.replace( Array.get(original, i), null, session, owner, copyCache ) );
91         }
92         
93         return target;
94     
95     }
96
97     public Object JavaDoc indexOf(Object JavaDoc array, Object JavaDoc element) {
98         int length = Array.getLength(array);
99         for ( int i=0; i<length; i++ ) {
100             //TODO: proxies!
101
if ( Array.get(array, i)==element ) return new Integer JavaDoc(i);
102         }
103         return null;
104     }
105
106     protected boolean initializeImmediately(EntityMode entityMode) {
107         return true;
108     }
109
110     public boolean hasHolder(EntityMode entityMode) {
111         return true;
112     }
113     
114
115 }
116
Popular Tags