FileDocCategorySizeDatePackage
PersistentSet.javaAPI DocHibernate 3.2.510931Mon Oct 16 10:47:20 BST 2006org.hibernate.collection

PersistentSet

public class PersistentSet extends AbstractPersistentCollection implements Set
A persistent wrapper for a java.util.Set. The underlying collection is a HashSet.
see
java.util.HashSet
author
Gavin King

Fields Summary
protected Set
set
protected transient List
tempList
Constructors Summary
public PersistentSet()
Empty constructor.

Note: this form is not ever ever ever used by Hibernate; it is, however, needed for SOAP libraries and other such marshalling code.

		// intentionally empty
	
public PersistentSet(org.hibernate.engine.SessionImplementor session)
Constructor matching super. Instantiates a lazy set (the underlying set is un-initialized).

param
session The session to which this set will belong.

		super( session );
	
public PersistentSet(org.hibernate.engine.SessionImplementor session, Set set)
Instantiates a non-lazy set (the underlying set is constructed from the incoming set reference).

param
session The session to which this set will belong.
param
set The underlying set data.

		super(session);
		// Sets can be just a view of a part of another collection.
		// do we need to copy it to be sure it won't be changing
		// underneath us?
		// ie. this.set.addAll(set);
		this.set = set;
		setInitialized();
		setDirectlyAccessible(true);
	
Methods Summary
public booleanadd(java.lang.Object value)

see
java.util.Set#add(Object)

		Boolean exists = isOperationQueueEnabled() ? readElementExistence( value ) : null;
		if ( exists == null ) {
			initialize( true );
			if ( set.add( value ) ) {
				dirty();
				return true;
			}
			else {
				return false;
			}
		}
		else if ( exists.booleanValue() ) {
			return false;
		}
		else {
			queueOperation( new SimpleAdd(value) );
			return true;
		}
	
public booleanaddAll(java.util.Collection coll)

see
java.util.Set#addAll(Collection)

		if ( coll.size() > 0 ) {
			initialize( true );
			if ( set.addAll( coll ) ) {
				dirty();
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	
public voidbeforeInitialize(org.hibernate.persister.collection.CollectionPersister persister, int anticipatedSize)

		this.set = ( Set ) persister.getCollectionType().instantiate( anticipatedSize );
	
public voidbeginRead()

		super.beginRead();
		tempList = new ArrayList();
	
public voidclear()

see
java.util.Set#clear()

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

see
java.util.Set#contains(Object)

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

see
java.util.Set#containsAll(Collection)

		read();
		return set.containsAll(coll);
	
public java.io.Serializabledisassemble(org.hibernate.persister.collection.CollectionPersister persister)


		Serializable[] result = new Serializable[ set.size() ];
		Iterator iter = set.iterator();
		int i=0;
		while ( iter.hasNext() ) {
			result[i++] = persister.getElementType().disassemble( iter.next(), getSession(), null );
		}
		return result;

	
public booleanempty()

		return set.isEmpty();
	
public booleanendRead()

		set.addAll(tempList);
		tempList = null;
		setInitialized();
		return true;
	
public java.util.Iteratorentries(org.hibernate.persister.collection.CollectionPersister persister)

		return set.iterator();
	
public booleanentryExists(java.lang.Object key, int i)

		return true;
	
public booleanequals(java.lang.Object other)

		read();
		return set.equals(other);
	
public booleanequalsSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		Type elementType = persister.getElementType();
		java.util.Map sn = (java.util.Map) getSnapshot();
		if ( sn.size()!=set.size() ) {
			return false;
		}
		else {
			Iterator iter = set.iterator();
			while ( iter.hasNext() ) {
				Object test = iter.next();
				Object oldValue = sn.get(test);
				if ( oldValue==null || elementType.isDirty( oldValue, test, getSession() ) ) return false;
			}
			return true;
		}
	
public java.util.IteratorgetDeletes(org.hibernate.persister.collection.CollectionPersister persister, boolean indexIsFormula)

		Type elementType = persister.getElementType();
		final java.util.Map sn = (java.util.Map) getSnapshot();
		ArrayList deletes = new ArrayList( sn.size() );
		Iterator iter = sn.keySet().iterator();
		while ( iter.hasNext() ) {
			Object test = iter.next();
			if ( !set.contains(test) ) {
				// the element has been removed from the set
				deletes.add(test);
			}
		}
		iter = set.iterator();
		while ( iter.hasNext() ) {
			Object test = iter.next();
			Object oldValue = sn.get(test);
			if ( oldValue!=null && elementType.isDirty( test, oldValue, getSession() ) ) {
				// the element has changed
				deletes.add(oldValue);
			}
		}
		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("Sets don't have indexes");
	
public java.util.CollectiongetOrphans(java.io.Serializable snapshot, java.lang.String entityName)

		java.util.Map sn = (java.util.Map) snapshot;
		return getOrphans( sn.keySet(), set, entityName, getSession() );
	
public java.io.SerializablegetSnapshot(org.hibernate.persister.collection.CollectionPersister persister)

		EntityMode entityMode = getSession().getEntityMode();
		
		//if (set==null) return new Set(session);
		HashMap clonedSet = new HashMap( set.size() );
		Iterator iter = set.iterator();
		while ( iter.hasNext() ) {
			Object copied = persister.getElementType()
					.deepCopy( iter.next(), entityMode, persister.getFactory() );
			clonedSet.put(copied, copied);
		}
		return clonedSet;
	
public java.lang.ObjectgetSnapshotElement(java.lang.Object entry, int i)

		throw new UnsupportedOperationException("Sets don't support updating by element");
	
public inthashCode()

		read();
		return set.hashCode();
	
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 ) {
				set.add( element );
			}
		}
	
public booleanisEmpty()

see
java.util.Set#isEmpty()

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

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

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

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

see
java.util.Set#iterator()

		read();
		return new IteratorProxy( set.iterator() );
	
public booleanneedsInserting(java.lang.Object entry, int i, org.hibernate.type.Type elemType)

		final java.util.Map sn = (java.util.Map) getSnapshot();
		Object oldValue = sn.get(entry);
		// note that it might be better to iterate the snapshot but this is safe,
		// assuming the user implements equals() properly, as required by the Set
		// contract!
		return oldValue==null || elemType.isDirty( oldValue, entry, getSession() );
	
public booleanneedsUpdating(java.lang.Object entry, int i, org.hibernate.type.Type elemType)

		return false;
	
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() );
		if (element!=null) tempList.add(element);
		return element;
	
public booleanremove(java.lang.Object value)

see
java.util.Set#remove(Object)

		Boolean exists = isPutQueueEnabled() ? readElementExistence( value ) : null;
		if ( exists==null ) {
			initialize( true );
			if ( set.remove( value ) ) {
				dirty();
				return true;
			}
			else {
				return false;
			}
		}
		else if ( exists.booleanValue() ) {
			queueOperation( new SimpleRemove(value) );
			return true;
		}
		else {
			return false;
		}
	
public booleanremoveAll(java.util.Collection coll)

see
java.util.Set#removeAll(Collection)

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

see
java.util.Set#retainAll(Collection)

		initialize( true );
		if ( set.retainAll( coll ) ) {
			dirty();
			return true;
		}
		else {
			return false;
		}
	
public intsize()

see
java.util.Set#size()

		return readSize() ? getCachedSize() : set.size();
	
public java.lang.Object[]toArray()

see
java.util.Set#toArray()

		read();
		return set.toArray();
	
public java.lang.Object[]toArray(java.lang.Object[] array)

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

		read();
		return set.toArray(array);
	
public java.lang.StringtoString()

		//if (needLoading) return "asleep";
		read();
		return set.toString();