FileDocCategorySizeDatePackage
PersistentArrayHolder.javaAPI DocHibernate 3.2.57263Wed Jul 05 13:17:28 BST 2006org.hibernate.collection

PersistentArrayHolder

public class PersistentArrayHolder extends AbstractPersistentCollection
A persistent wrapper for an array. Lazy initialization is NOT supported. Use of Hibernate arrays is not really recommended.
author
Gavin King

Fields Summary
protected Object
array
private static final Log
log
private transient Class
elementClass
private transient List
tempList
Constructors Summary
public PersistentArrayHolder(org.hibernate.engine.SessionImplementor session, Object array)


	     
		super(session);
		this.array = array;
		setInitialized();
	
public PersistentArrayHolder(org.hibernate.engine.SessionImplementor session, org.hibernate.persister.collection.CollectionPersister persister)

		super(session);
		elementClass = persister.getElementClass();
	
Methods Summary
public voidbeforeInitialize(org.hibernate.persister.collection.CollectionPersister persister, int anticipatedSize)

		//if (tempList==null) throw new UnsupportedOperationException("Can't lazily initialize arrays");
	
public voidbeginRead()

		super.beginRead();
		tempList = new ArrayList();
	
public java.io.Serializabledisassemble(org.hibernate.persister.collection.CollectionPersister persister)

		int length = Array.getLength(array);
		Serializable[] result = new Serializable[length];
		for ( int i=0; i<length; i++ ) {
			result[i] = persister.getElementType().disassemble( Array.get(array,i), getSession(), null );
		}

		/*int length = tempList.size();
		Serializable[] result = new Serializable[length];
		for ( int i=0; i<length; i++ ) {
			result[i] = persister.getElementType().disassemble( tempList.get(i), session );
		}*/

		return result;

	
public java.util.Iteratorelements()

		//if (array==null) return tempList.iterator();
		int length = Array.getLength(array);
		java.util.List list = new ArrayList(length);
		for (int i=0; i<length; i++) {
			list.add( Array.get(array, i) );
		}
		return list.iterator();
	
public booleanempty()

		return false;
	
public booleanendRead()

		setInitialized();
		array = Array.newInstance( elementClass, tempList.size() );
		for ( int i=0; i<tempList.size(); i++) {
			Array.set(array, i, tempList.get(i) );
		}
		tempList=null;
		return true;
	
public java.util.Iteratorentries(org.hibernate.persister.collection.CollectionPersister persister)

		return elements();
	
public booleanentryExists(java.lang.Object entry, int i)

		return entry!=null;
	
public booleanequalsSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		Type elementType = persister.getElementType();
		Serializable snapshot = getSnapshot();
		int xlen = Array.getLength(snapshot);
		if ( xlen!= Array.getLength(array) ) return false;
		for ( int i=0; i<xlen; i++) {
			if ( elementType.isDirty( Array.get(snapshot, i), Array.get(array, i), getSession() ) ) return false;
		}
		return true;
	
public java.lang.ObjectgetArray()

		return array;
	
public java.util.IteratorgetDeletes(org.hibernate.persister.collection.CollectionPersister persister, boolean indexIsFormula)

		java.util.List deletes = new ArrayList();
		Serializable sn = getSnapshot();
		int snSize = Array.getLength(sn);
		int arraySize = Array.getLength(array);
		int end;
		if ( snSize > arraySize ) {
			for ( int i=arraySize; i<snSize; i++ ) deletes.add( new Integer(i) );
			end = arraySize;
		}
		else {
			end = snSize;
		}
		for ( int i=0; i<end; i++ ) {
			if ( Array.get(array, i)==null && Array.get(sn, i)!=null ) deletes.add( new Integer(i) );
		}
		return deletes.iterator();
	
public java.lang.ObjectgetElement(java.lang.Object entry)

		return entry;
	
public java.lang.ObjectgetIndex(java.lang.Object entry, int i, org.hibernate.persister.collection.CollectionPersister persister)

		return new Integer(i);
	
public java.util.CollectiongetOrphans(java.io.Serializable snapshot, java.lang.String entityName)

		Object[] sn = (Object[]) snapshot;
		Object[] arr = (Object[]) array;
		ArrayList result = new ArrayList();
		for (int i=0; i<sn.length; i++) result.add( sn[i] );
		for (int i=0; i<sn.length; i++) identityRemove( result, arr[i], entityName, getSession() );
		return result;
	
public java.io.SerializablegetSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		EntityMode entityMode = getSession().getEntityMode();
		int length = /*(array==null) ? tempList.size() :*/ Array.getLength(array);
		Serializable result = (Serializable) Array.newInstance( persister.getElementClass(), length );
		for ( int i=0; i<length; i++ ) {
			Object elt = /*(array==null) ? tempList.get(i) :*/ Array.get(array, i);
			try {
				Array.set( result, i, persister.getElementType().deepCopy(elt, entityMode, persister.getFactory()) );
			}
			catch (IllegalArgumentException iae) {
				log.error("Array element type error", iae);
				throw new HibernateException( "Array element type error", iae );
			}
		}
		return result;
	
public java.lang.ObjectgetSnapshotElement(java.lang.Object entry, int i)

		Serializable sn = getSnapshot();
		return Array.get(sn, i);
	
public java.lang.ObjectgetValue()

		return array;
	
public voidinitializeFromCache(org.hibernate.persister.collection.CollectionPersister persister, java.io.Serializable disassembled, java.lang.Object owner)

		Serializable[] cached = (Serializable[]) disassembled;

		array = Array.newInstance( persister.getElementClass(), cached.length );

		for ( int i=0; i<cached.length; i++ ) {
			Array.set( array, i, persister.getElementType().assemble( cached[i], getSession(), owner ) );
		}
	
public booleanisDirectlyAccessible()

		return true;
	
public booleanisSnapshotEmpty(java.io.Serializable snapshot)

		return Array.getLength( snapshot ) == 0;
	
public booleanisWrapper(java.lang.Object collection)

		return array==collection;
	
public booleanneedsInserting(java.lang.Object entry, int i, org.hibernate.type.Type elemType)

		Serializable sn = getSnapshot();
		return Array.get(array, i)!=null && ( i >= Array.getLength(sn) || Array.get(sn, i)==null );
	
public booleanneedsUpdating(java.lang.Object entry, int i, org.hibernate.type.Type elemType)

		Serializable sn = getSnapshot();
		return i<Array.getLength(sn) &&
				Array.get(sn, i)!=null &&
				Array.get(array, i)!=null &&
				elemType.isDirty( Array.get(array, i), Array.get(sn, i), getSession() );
	
public java.lang.ObjectreadFrom(java.sql.ResultSet rs, org.hibernate.persister.collection.CollectionPersister persister, org.hibernate.loader.CollectionAliases descriptor, java.lang.Object owner)


		Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
		int index = ( (Integer) persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() ) ).intValue();
		for ( int i = tempList.size(); i<=index; i++) {
			tempList.add(i, null);
		}
		tempList.set(index, element);
		return element;