Skip to content

Commit

Permalink
HHH-6862 Use generic Maps for more StatefulPersistenceContext fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne authored and stliu committed Nov 30, 2011
1 parent f98e15e commit 67d1c70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,39 @@ public class StatefulPersistenceContext implements PersistenceContext {
private SessionImplementor session;

// Loaded entity instances, by EntityKey
private Map entitiesByKey;
private Map<EntityKey, Object> entitiesByKey;

// Loaded entity instances, by EntityUniqueKey
private Map entitiesByUniqueKey;
private Map<EntityUniqueKey, Object> entitiesByUniqueKey;

// Identity map of EntityEntry instances, by the entity instance
private Map<Object,EntityEntry> entityEntries;

// Entity proxies, by EntityKey
private Map proxiesByKey;
private Map<EntityKey, Object> proxiesByKey;

// Snapshots of current database state for entities
// that have *not* been loaded
private Map entitySnapshotsByKey;
private Map<EntityKey, Object> entitySnapshotsByKey;

// Identity map of array holder ArrayHolder instances, by the array instance
private Map arrayHolders;
private Map<Object, PersistentCollection> arrayHolders;

// Identity map of CollectionEntry instances, by the collection wrapper
private Map collectionEntries;
private Map<PersistentCollection, CollectionEntry> collectionEntries;

// Collection wrappers, by the CollectionKey
private Map collectionsByKey; //key=CollectionKey, value=PersistentCollection
private Map<CollectionKey, PersistentCollection> collectionsByKey;

// Set of EntityKeys of deleted objects
private HashSet nullifiableEntityKeys;
private HashSet<EntityKey> nullifiableEntityKeys;

// properties that we have tried to load, and not found in the database
private HashSet nullAssociations;
private HashSet<AssociationKey> nullAssociations;

// A list of collection wrappers that were instantiating during result set
// processing, that we will need to initialize at the end of the query
private List nonlazyCollections;
private List<PersistentCollection> nonlazyCollections;

// A container for collections we load up when the owning entity is not
// yet loaded ... for now, this is purely transient!
Expand Down Expand Up @@ -223,9 +223,9 @@ public void clear() {
final LazyInitializer li = ((HibernateProxy) o).getHibernateLazyInitializer();
li.unsetSession();
}
Map.Entry[] collectionEntryArray = IdentityMap.concurrentEntries( collectionEntries );
for ( Map.Entry aCollectionEntryArray : collectionEntryArray ) {
((PersistentCollection) aCollectionEntryArray.getKey()).unsetSession( getSession() );
Map.Entry<PersistentCollection, CollectionEntry>[] collectionEntryArray = IdentityMap.concurrentEntries( collectionEntries );
for ( Map.Entry<PersistentCollection, CollectionEntry> aCollectionEntryArray : collectionEntryArray ) {
aCollectionEntryArray.getKey().unsetSession( getSession() );
}
arrayHolders.clear();
entitiesByKey.clear();
Expand Down Expand Up @@ -444,7 +444,7 @@ public boolean isEntryFor(Object entity) {
* Get the collection entry for a persistent collection
*/
public CollectionEntry getCollectionEntry(PersistentCollection coll) {
return (CollectionEntry) collectionEntries.get(coll);
return collectionEntries.get(coll);
}

/**
Expand Down Expand Up @@ -822,7 +822,7 @@ public void addNewCollection(CollectionPersister persister, PersistentCollection
private void addCollection(PersistentCollection coll, CollectionEntry entry, Serializable key) {
collectionEntries.put( coll, entry );
CollectionKey collectionKey = new CollectionKey( entry.getLoadedPersister(), key );
PersistentCollection old = ( PersistentCollection ) collectionsByKey.put( collectionKey, coll );
PersistentCollection old = collectionsByKey.put( collectionKey, coll );
if ( old != null ) {
if ( old == coll ) {
throw new AssertionFailure("bug adding collection twice");
Expand Down Expand Up @@ -877,7 +877,7 @@ public CollectionEntry addInitializedCollection(CollectionPersister persister, P
* Get the collection instance associated with the <tt>CollectionKey</tt>
*/
public PersistentCollection getCollection(CollectionKey collectionKey) {
return (PersistentCollection) collectionsByKey.get(collectionKey);
return collectionsByKey.get( collectionKey );
}

/**
Expand All @@ -902,7 +902,7 @@ public void initializeNonLazyCollections() throws HibernateException {
int size;
while ( ( size = nonlazyCollections.size() ) > 0 ) {
//note that each iteration of the loop may add new elements
( (PersistentCollection) nonlazyCollections.remove( size - 1 ) ).forceInitialization();
nonlazyCollections.remove( size - 1 ).forceInitialization();
}
}
finally {
Expand All @@ -917,7 +917,7 @@ public void initializeNonLazyCollections() throws HibernateException {
* Get the <tt>PersistentCollection</tt> object for an array
*/
public PersistentCollection getCollectionHolder(Object array) {
return (PersistentCollection) arrayHolders.get(array);
return arrayHolders.get(array);
}

/**
Expand All @@ -931,7 +931,7 @@ public void addCollectionHolder(PersistentCollection holder) {
}

public PersistentCollection removeCollectionHolder(Object array) {
return (PersistentCollection) arrayHolders.remove(array);
return arrayHolders.remove(array);
}

/**
Expand All @@ -957,9 +957,9 @@ public CollectionEntry getCollectionEntryOrNull(Object collection) {
if ( coll == null ) {
//it might be an unwrapped collection reference!
//try to find a wrapper (slowish)
Iterator wrappers = IdentityMap.keyIterator(collectionEntries);
Iterator<PersistentCollection> wrappers = IdentityMap.keyIterator( collectionEntries );
while ( wrappers.hasNext() ) {
PersistentCollection pc = (PersistentCollection) wrappers.next();
PersistentCollection pc = wrappers.next();
if ( pc.isWrapper(collection) ) {
coll = pc;
break;
Expand Down Expand Up @@ -1517,9 +1517,9 @@ public void serialize(ObjectOutputStream oos) throws IOException {

oos.writeInt( nullifiableEntityKeys.size() );
if ( tracing ) LOG.trace("Starting serialization of [" + nullifiableEntityKeys.size() + "] nullifiableEntityKey entries");
itr = nullifiableEntityKeys.iterator();
while ( itr.hasNext() ) {
EntityKey entry = ( EntityKey ) itr.next();
Iterator<EntityKey> entityKeyIterator = nullifiableEntityKeys.iterator();
while ( entityKeyIterator.hasNext() ) {
EntityKey entry = entityKeyIterator.next();
entry.serialize( oos );
}
}
Expand Down Expand Up @@ -1590,7 +1590,7 @@ public static StatefulPersistenceContext deserialize(
if ( tracing ) LOG.trace("Starting deserialization of [" + count + "] collectionsByKey entries");
rtn.collectionsByKey = new HashMap( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
for ( int i = 0; i < count; i++ ) {
rtn.collectionsByKey.put( CollectionKey.deserialize( ois, session ), ois.readObject() );
rtn.collectionsByKey.put( CollectionKey.deserialize( ois, session ), (PersistentCollection) ois.readObject() );
}

count = ois.readInt();
Expand All @@ -1607,7 +1607,7 @@ public static StatefulPersistenceContext deserialize(
if ( tracing ) LOG.trace("Starting deserialization of [" + count + "] arrayHolders entries");
rtn.arrayHolders = IdentityMap.instantiate( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
for ( int i = 0; i < count; i++ ) {
rtn.arrayHolders.put( ois.readObject(), ois.readObject() );
rtn.arrayHolders.put( ois.readObject(), (PersistentCollection) ois.readObject() );
}

count = ois.readInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ private IdentityMap(Map<IdentityKey<K>,V> underlyingMap) {
* @param map The map of entries
* @return Collection
*/
public static Map.Entry[] concurrentEntries(Map map) {
return ( (IdentityMap) map ).entryArray();
public static <K,V> Map.Entry<K,V>[] concurrentEntries(Map<K,V> map) {
return ( (IdentityMap<K,V>) map ).entryArray();
}

public static <K,V> List<Entry<K,V>> entries(Map<K,V> map) {
return ( (IdentityMap) map ).entryList();
return ( (IdentityMap<K,V>) map ).entryList();
}

public static Iterator keyIterator(Map map) {
return ( (IdentityMap) map ).keyIterator();
public static <K,V> Iterator<K> keyIterator(Map<K,V> map) {
return ( (IdentityMap<K,V>) map ).keyIterator();
}

public Iterator keyIterator() {
Expand Down

0 comments on commit 67d1c70

Please sign in to comment.