FileDocCategorySizeDatePackage
PersistentMap.javaAPI DocHibernate 3.2.512482Wed Mar 07 12:32:58 GMT 2007org.hibernate.collection

PersistentMap

public class PersistentMap extends AbstractPersistentCollection implements Map
A persistent wrapper for a java.util.Map. Underlying collection is a HashMap.
see
java.util.HashMap
author
Gavin King

Fields Summary
protected Map
map
Constructors Summary
public PersistentMap()
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 PersistentMap(org.hibernate.engine.SessionImplementor session)
Instantiates a lazy map (the underlying map is un-initialized).

param
session The session to which this map will belong.

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

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

		super(session);
		this.map = map;
		setInitialized();
		setDirectlyAccessible(true);
	
Methods Summary
public voidbeforeInitialize(org.hibernate.persister.collection.CollectionPersister persister, int anticipatedSize)

		this.map = ( Map ) persister.getCollectionType().instantiate( anticipatedSize );
	
public voidclear()

see
java.util.Map#clear()

		if ( isClearQueueEnabled() ) {
			queueOperation( new Clear() );
		}
		else {
			initialize( true );
			if ( ! map.isEmpty() ) {
				dirty();
				map.clear();
			}
		}
	
public booleancontainsKey(java.lang.Object key)

see
java.util.Map#containsKey(Object)

		Boolean exists = readIndexExistence(key);
		return exists==null ? map.containsKey(key) : exists.booleanValue();
	
public booleancontainsValue(java.lang.Object value)

see
java.util.Map#containsValue(Object)

		Boolean exists = readElementExistence(value);
		return exists==null ? 
				map.containsValue(value) : 
				exists.booleanValue();
	
public java.io.Serializabledisassemble(org.hibernate.persister.collection.CollectionPersister persister)


		Serializable[] result = new Serializable[ map.size() * 2 ];
		Iterator iter = map.entrySet().iterator();
		int i=0;
		while ( iter.hasNext() ) {
			Map.Entry e = (Map.Entry) iter.next();
			result[i++] = persister.getIndexType().disassemble( e.getKey(), getSession(), null );
			result[i++] = persister.getElementType().disassemble( e.getValue(), getSession(), null );
		}
		return result;

	
public booleanempty()

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

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

		return ( (Map.Entry) entry ).getValue()!=null;
	
public java.util.SetentrySet()

see
java.util.Map#entrySet()

		read();
		return new EntrySetProxy( map.entrySet() );
	
public booleanequals(java.lang.Object other)

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

		Type elementType = persister.getElementType();
		Map xmap = (Map) getSnapshot();
		if ( xmap.size()!=this.map.size() ) return false;
		Iterator iter = map.entrySet().iterator();
		while ( iter.hasNext() ) {
			Map.Entry entry = (Map.Entry) iter.next();
			if ( elementType.isDirty( entry.getValue(), xmap.get( entry.getKey() ), getSession() ) ) return false;
		}
		return true;
	
public java.lang.Objectget(java.lang.Object key)

see
java.util.Map#get(Object)

		Object result = readElementByIndex(key);
		return result==UNKNOWN ? map.get(key) : result;
	
public java.util.IteratorgetDeletes(org.hibernate.persister.collection.CollectionPersister persister, boolean indexIsFormula)

		List deletes = new ArrayList();
		Iterator iter = ( (Map) getSnapshot() ).entrySet().iterator();
		while ( iter.hasNext() ) {
			Map.Entry e = (Map.Entry) iter.next();
			Object key = e.getKey();
			if ( e.getValue()!=null && map.get(key)==null ) {
				deletes.add( indexIsFormula ? e.getValue() : key );
			}
		}
		return deletes.iterator();
	
public java.lang.ObjectgetElement(java.lang.Object entry)

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

		return ( (Map.Entry) entry ).getKey();
	
public java.util.CollectiongetOrphans(java.io.Serializable snapshot, java.lang.String entityName)

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

		EntityMode entityMode = getSession().getEntityMode();
		HashMap clonedMap = new HashMap( map.size() );
		Iterator iter = map.entrySet().iterator();
		while ( iter.hasNext() ) {
			Map.Entry e = (Map.Entry) iter.next();
			final Object copy = persister.getElementType()
				.deepCopy( e.getValue(), entityMode, persister.getFactory() );
			clonedMap.put( e.getKey(), copy );
		}
		return clonedMap;
	
public java.lang.ObjectgetSnapshotElement(java.lang.Object entry, int i)

		final Map sn = (Map) getSnapshot();
		return sn.get( ( (Map.Entry) entry ).getKey() );
	
public inthashCode()

		read();
		return map.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+=2 ) {
			map.put(
					persister.getIndexType().assemble( array[i], getSession(), owner ),
					persister.getElementType().assemble( array[i+1], getSession(), owner )
				);
		}
	
public booleanisEmpty()

see
java.util.Map#isEmpty()

		return readSize() ? getCachedSize()==0 : map.isEmpty();
	
public booleanisSnapshotEmpty(java.io.Serializable snapshot)

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

		return map==collection;
	
public java.util.SetkeySet()

see
java.util.Map#keySet()

		read();
		return new SetProxy( map.keySet() );
	
public booleanneedsInserting(java.lang.Object entry, int i, org.hibernate.type.Type elemType)

		final Map sn = (Map) getSnapshot();
		Map.Entry e = (Map.Entry) entry;
		return e.getValue()!=null && sn.get( e.getKey() )==null;
	
public booleanneedsUpdating(java.lang.Object entry, int i, org.hibernate.type.Type elemType)

		final Map sn = (Map) getSnapshot();
		Map.Entry e = (Map.Entry) entry;
		Object snValue = sn.get( e.getKey() );
		return e.getValue()!=null &&
			snValue!=null &&
			elemType.isDirty( snValue, e.getValue(), getSession() );
	
public java.lang.Objectput(java.lang.Object key, java.lang.Object value)

see
java.util.Map#put(Object, Object)

		if ( isPutQueueEnabled() ) {
			Object old = readElementByIndex( key );
			if ( old != UNKNOWN ) {
				queueOperation( new Put( key, value, old ) );
				return old;
			}
		}
		initialize( true );
		Object old = map.put( key, value );
		// would be better to use the element-type to determine
		// whether the old and the new are equal here; the problem being
		// we do not necessarily have access to the element type in all
		// cases
		if ( value != old ) {
			dirty();
		}
		return old;
	
public voidputAll(java.util.Map puts)

see
java.util.Map#putAll(java.util.Map puts)

		if ( puts.size()>0 ) {
			initialize( true );
			Iterator itr = puts.entrySet().iterator();
			while ( itr.hasNext() ) {
				Map.Entry entry = ( Entry ) itr.next();
				put( entry.getKey(), entry.getValue() );
			}
		}
	
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() );
		Object index = persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() );
		if ( element!=null ) map.put(index, element);
		return element;
	
public java.lang.Objectremove(java.lang.Object key)

see
java.util.Map#remove(Object)

		if ( isPutQueueEnabled() ) {
			Object old = readElementByIndex( key );
			queueOperation( new Remove( key, old ) );
			return old;
		}
		else {
			// TODO : safe to interpret "map.remove(key) == null" as non-dirty?
			initialize( true );
			if ( map.containsKey( key ) ) {
				dirty();
			}
			return map.remove( key );
		}
	
public intsize()

see
java.util.Map#size()

		return readSize() ? getCachedSize() : map.size();
	
public java.lang.StringtoString()

		read();
		return map.toString();
	
public java.util.Collectionvalues()

see
java.util.Map#values()

		read();
		return new SetProxy( map.values() );