1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.lang.reflect.Array ; 6 import java.util.ArrayList ; 7 import java.util.Arrays ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 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 24 public class ArrayType extends CollectionType { 25 26 private final Class elementClass; 27 private final Class arrayClass; 28 29 public ArrayType(String role, String propertyRef, Class elementClass, boolean isEmbeddedInXML) { 30 super(role, propertyRef, isEmbeddedInXML); 31 this.elementClass = elementClass; 32 arrayClass = Array.newInstance(elementClass, 0).getClass(); 33 } 34 35 public Class getReturnedClass() { 36 return arrayClass; 37 } 38 39 public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) 40 throws HibernateException { 41 return new PersistentArrayHolder(session, persister); 42 } 43 44 47 public Iterator getElementsIterator(Object collection) { 48 return Arrays.asList( (Object []) collection ).iterator(); 49 } 50 51 public PersistentCollection wrap(SessionImplementor session, Object array) { 52 return new PersistentArrayHolder(session, array); 53 } 54 55 public boolean isArrayType() { 56 return true; 57 } 58 59 public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { 60 if (value==null) return "null"; 61 int length = Array.getLength(value); 62 List list = new ArrayList (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 instantiate(Object original) { 71 return Array.newInstance( elementClass, Array.getLength(original) ); 72 } 73 74 public Object replaceElements( 75 Object original, 76 Object target, 77 Object owner, 78 Map copyCache, 79 SessionImplementor session) 80 throws HibernateException { 81 82 int length = Array.getLength(original); 83 if ( length!=Array.getLength(target) ) { 84 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 indexOf(Object array, Object element) { 98 int length = Array.getLength(array); 99 for ( int i=0; i<length; i++ ) { 100 if ( Array.get(array, i)==element ) return new Integer (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 |