FileDocCategorySizeDatePackage
PersistentBag.javaAPI DocHibernate 3.2.512970Mon Nov 06 15:00:42 GMT 2006org.hibernate.collection

PersistentBag

public class PersistentBag extends AbstractPersistentCollection implements List
An unordered, unkeyed collection that can contain the same element multiple times. The Java collections API, curiously, has no Bag. Most developers seem to use Lists to represent bag semantics, so Hibernate follows this practice.
author
Gavin King

Fields Summary
protected List
bag
Constructors Summary
public PersistentBag(org.hibernate.engine.SessionImplementor session)

		super(session);
	
public PersistentBag(org.hibernate.engine.SessionImplementor session, Collection coll)

		super(session);
		if (coll instanceof List) {
			bag = (List) coll;
		}
		else {
			bag = new ArrayList();
			Iterator iter = coll.iterator();
			while ( iter.hasNext() ) {
				bag.add( iter.next() );
			}
		}
		setInitialized();
		setDirectlyAccessible(true);
	
public PersistentBag()

Methods Summary
public booleanadd(java.lang.Object object)

see
java.util.Collection#add(Object)

		if ( !isOperationQueueEnabled() ) {
			write();
			return bag.add(object);
		}
		else {
			queueOperation( new SimpleAdd(object) );
			return true;
		}
	
public voidadd(int i, java.lang.Object o)

see
java.util.List#add(int, Object)

		write();
		bag.add(i, o);
	
public booleanaddAll(java.util.Collection values)

see
java.util.Collection#addAll(Collection)

		if ( values.size()==0 ) return false;
		if ( !isOperationQueueEnabled() ) {
			write();
			return bag.addAll(values);
		}
		else {
			Iterator iter = values.iterator();
			while ( iter.hasNext() ) {
				queueOperation( new SimpleAdd( iter.next() ) );
			}
			return values.size()>0;
		}
	
public booleanaddAll(int i, java.util.Collection c)

see
java.util.List#addAll(int, Collection)

		if ( c.size()>0 ) {
			write();
			return bag.addAll(i, c);
		}
		else {
			return false;
		}
	
public voidbeforeInitialize(org.hibernate.persister.collection.CollectionPersister persister, int anticipatedSize)

		this.bag = ( List ) persister.getCollectionType().instantiate( anticipatedSize );
	
public voidclear()

see
java.util.Collection#clear()

		if ( isClearQueueEnabled() ) {
			queueOperation( new Clear() );
		}
		else {
			initialize( true );
			if ( ! bag.isEmpty() ) {
				bag.clear();
				dirty();
			}
		}
	
public booleancontains(java.lang.Object object)

see
java.util.Collection#contains(Object)

		Boolean exists = readElementExistence(object);
		return exists==null ? 
				bag.contains(object) : 
				exists.booleanValue();
	
public booleancontainsAll(java.util.Collection c)

see
java.util.Collection#containsAll(Collection)

		read();
		return bag.containsAll(c);
	
private intcountOccurrences(java.lang.Object element, java.util.List list, org.hibernate.type.Type elementType, org.hibernate.EntityMode entityMode)

		Iterator iter = list.iterator();
		int result=0;
		while ( iter.hasNext() ) {
			if ( elementType.isSame( element, iter.next(), entityMode ) ) result++;
		}
		return result;
	
public java.io.Serializabledisassemble(org.hibernate.persister.collection.CollectionPersister persister)


		int length = bag.size();
		Serializable[] result = new Serializable[length];
		for ( int i=0; i<length; i++ ) {
			result[i] = persister.getElementType().disassemble( bag.get(i), getSession(), null );
		}
		return result;
	
public booleanempty()

		return bag.isEmpty();
	
public java.util.Iteratorentries(org.hibernate.persister.collection.CollectionPersister persister)

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

		return entry!=null;
	
public booleanequals(java.lang.Object obj)
Bag does not respect the collection API and do an JVM instance comparison to do the equals. The semantic is broken not to have to initialize a collection for a simple equals() operation.

see
java.lang.Object#equals(java.lang.Object)

		return super.equals(obj);
	
public booleanequalsSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		Type elementType = persister.getElementType();
		EntityMode entityMode = getSession().getEntityMode();
		List sn = (List) getSnapshot();
		if ( sn.size()!=bag.size() ) return false;
		Iterator iter = bag.iterator();
		while ( iter.hasNext() ) {
			Object elt = iter.next();
			final boolean unequal = countOccurrences(elt, bag, elementType, entityMode) !=
				countOccurrences(elt, sn, elementType, entityMode);
			if ( unequal ) return false;
		}
		return true;
	
public java.lang.Objectget(int i)

see
java.util.List#get(int)

		read();
		return bag.get(i);
	
public java.util.IteratorgetDeletes(org.hibernate.persister.collection.CollectionPersister persister, boolean indexIsFormula)

		//if ( !persister.isOneToMany() ) throw new AssertionFailure("Not implemented for Bags");
		Type elementType = persister.getElementType();
		EntityMode entityMode = getSession().getEntityMode();
		ArrayList deletes = new ArrayList();
		List sn = (List) getSnapshot();
		Iterator olditer = sn.iterator();
		int i=0;
		while ( olditer.hasNext() ) {
			Object old = olditer.next();
			Iterator newiter = bag.iterator();
			boolean found = false;
			if ( bag.size()>i && elementType.isSame( old, bag.get(i++), entityMode ) ) {
			//a shortcut if its location didn't change!
				found = true;
			}
			else {
				//search for it
				//note that this code is incorrect for other than one-to-many
				while ( newiter.hasNext() ) {
					if ( elementType.isSame( old, newiter.next(), entityMode ) ) {
						found = true;
						break;
					}
				}
			}
			if (!found) deletes.add(old);
		}
		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)

		throw new UnsupportedOperationException("Bags don't have indexes");
	
public java.util.CollectiongetOrphans(java.io.Serializable snapshot, java.lang.String entityName)

	    List sn = (List) snapshot;
	    return getOrphans( sn, bag, entityName, getSession() );
	
public java.io.SerializablegetSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		EntityMode entityMode = getSession().getEntityMode();
		ArrayList clonedList = new ArrayList( bag.size() );
		Iterator iter = bag.iterator();
		while ( iter.hasNext() ) {
			clonedList.add( persister.getElementType().deepCopy( iter.next(), entityMode, persister.getFactory() ) );
		}
		return clonedList;
	
public java.lang.ObjectgetSnapshotElement(java.lang.Object entry, int i)

		List sn = (List) getSnapshot();
		return sn.get(i);
	
public inthashCode()

see
java.lang.Object#hashCode()

		return super.hashCode();
	
public intindexOf(java.lang.Object o)

see
java.util.List#indexOf(Object)

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

		Serializable[] array = (Serializable[]) disassembled;
		int size = array.length;
		beforeInitialize( persister, size );
		for ( int i = 0; i < size; i++ ) {
			Object element = persister.getElementType().assemble( array[i], getSession(), owner );
			if ( element!=null ) {
				bag.add( element );
			}
		}
	
public booleanisEmpty()

see
java.util.Collection#isEmpty()

		return readSize() ? getCachedSize()==0 : bag.isEmpty();
	
public booleanisRowUpdatePossible()

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

		return ( (Collection) snapshot ).isEmpty();
	
public booleanisWrapper(java.lang.Object collection)

		return bag==collection;
	
public java.util.Iteratoriterator()

see
java.util.Collection#iterator()

		read();
		return new IteratorProxy( bag.iterator() );
	
public intlastIndexOf(java.lang.Object o)

see
java.util.List#lastIndexOf(Object)

		read();
		return bag.lastIndexOf(o);
	
public java.util.ListIteratorlistIterator()

see
java.util.List#listIterator()

		read();
		return new ListIteratorProxy( bag.listIterator() );
	
public java.util.ListIteratorlistIterator(int i)

see
java.util.List#listIterator(int)

		read();
		return new ListIteratorProxy( bag.listIterator(i) );
	
public booleanneedsInserting(java.lang.Object entry, int i, org.hibernate.type.Type elemType)

		//if ( !persister.isOneToMany() ) throw new AssertionFailure("Not implemented for Bags");
		List sn = (List) getSnapshot();
		final EntityMode entityMode = getSession().getEntityMode();
		if ( sn.size()>i && elemType.isSame( sn.get(i), entry, entityMode ) ) {
		//a shortcut if its location didn't change!
			return false;
		}
		else {
			//search for it
			//note that this code is incorrect for other than one-to-many
			Iterator olditer = sn.iterator();
			while ( olditer.hasNext() ) {
				Object old = olditer.next();
				if ( elemType.isSame( old, entry, entityMode ) ) return false;
			}
			return true;
		}
	
public booleanneedsRecreate(org.hibernate.persister.collection.CollectionPersister persister)

		return !persister.isOneToMany();
	
public booleanneedsUpdating(java.lang.Object entry, int i, org.hibernate.type.Type elemType)

		//if ( !persister.isOneToMany() ) throw new AssertionFailure("Not implemented for Bags");
		return false;
	
public intoccurrences(java.lang.Object o)

		read();
		Iterator iter = bag.iterator();
		int result=0;
		while ( iter.hasNext() ) {
			if ( o.equals( iter.next() ) ) result++;
		}
		return result;
	
public java.lang.ObjectreadFrom(java.sql.ResultSet rs, org.hibernate.persister.collection.CollectionPersister persister, org.hibernate.loader.CollectionAliases descriptor, java.lang.Object owner)

		// note that if we load this collection from a cartesian product
		// the multiplicity would be broken ... so use an idbag instead
		Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() ) ;
		if (element!=null) bag.add(element);
		return element;
	
public booleanremove(java.lang.Object o)

see
java.util.Collection#remove(Object)

		initialize( true );
		if ( bag.remove( o ) ) {
			dirty();
			return true;
		}
		else {
			return false;
		}
	
public java.lang.Objectremove(int i)

see
java.util.List#remove(int)

		write();
		return bag.remove(i);
	
public booleanremoveAll(java.util.Collection c)

see
java.util.Collection#removeAll(Collection)

		if ( c.size()>0 ) {
			initialize( true );
			if ( bag.removeAll( c ) ) {
				dirty();
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	
public booleanretainAll(java.util.Collection c)

see
java.util.Collection#retainAll(Collection)

		initialize( true );
		if ( bag.retainAll( c ) ) {
			dirty();
			return true;
		}
		else {
			return false;
		}
	
public java.lang.Objectset(int i, java.lang.Object o)

see
java.util.List#set(int, Object)

		write();
		return bag.set(i, o);
	
public intsize()

see
java.util.Collection#size()

		return readSize() ? getCachedSize() : bag.size();
	
public java.util.ListsubList(int start, int end)

see
java.util.List#subList(int, int)

		read();
		return new ListProxy( bag.subList(start, end) );
	
public java.lang.Object[]toArray()

see
java.util.Collection#toArray()

		read();
		return bag.toArray();
	
public java.lang.Object[]toArray(java.lang.Object[] a)

see
java.util.Collection#toArray(Object[])

		read();
		return bag.toArray(a);
	
public java.lang.StringtoString()

		read();
		return bag.toString();