uka.util
Class WeakIDMap

java.lang.Object
  extended byuka.util.IDConstants
      extended byuka.util.IDMap
          extended byuka.util.WeakIDMap
All Implemented Interfaces:
Printable, java.io.Serializable
Direct Known Subclasses:
PartitionedObjectSpace

public class WeakIDMap
extends IDMap

This class provides the same functionality as IDMap, but uses weak references. Adding an object to this container does not prevent it from being garbage collected. An identifier stays used until it is removed explicitly, even if the corresponding object was garbage collected.

Author:
Bernhard Haumacher
See Also:
Serialized Form

Nested Class Summary
(package private) static class WeakIDMap.Ref
          Weakly refer to an object, and remember its hash code beyond its death.
 
Field Summary
private  java.lang.ref.ReferenceQueue queue
           
 
Fields inherited from class uka.util.IDMap
idToObject, INVALID_INDEX, space
 
Fields inherited from class uka.util.IDConstants
FIRST_NEW_ID, FIRST_VALID_ID, INVALID_ID, UNUSED_ID
 
Constructor Summary
WeakIDMap()
           
WeakIDMap(IDSpace space)
           
 
Method Summary
protected  java.lang.Object get(int id)
           
protected  int getIDForRef(WeakIDMap.Ref ref)
          Find the identifier for a given reference.
protected  int getIndexForID(int id)
           
 Iterator getObjects()
          Specialized implementation for @T@IDMap.getObjects() that prevents returning null values for already garbage collected objects.
 Iterator getObjects(IntIterator ids)
           
private  void initTransient()
           
 int pollID()
          Polls an internal ReferenceQueue for the next weakly reacheable object and returns the identifier that was associated with it.
protected  void restoreBeforeReadObject()
           
protected  void set(int id, java.lang.Object obj)
          Access to the underlying IDMap.idToObject array is encapsulated through the set(int, java.lang.Object) and get(int) methods.
 int waitForID()
          Wait until an object that is referenced from this map becomes weakly reacheable and return its identifier.
 int waitForID(long timeout)
          Wait at most timeout milliseconds until an object that is referenced from this map becomes weakly reacheable and return its identifier.
 
Methods inherited from class uka.util.IDMap
appendTo, clear, dump, findID, getHash, getID, getIndexForHash, getObject, getSize, getUsedIDs, isUsed, rawFindID, remove, resetObject, setObject
 
Methods inherited from class uka.util.IDConstants
isNew, isValid, normalizeID, toggleNew
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

queue

private transient java.lang.ref.ReferenceQueue queue
Constructor Detail

WeakIDMap

public WeakIDMap()

WeakIDMap

public WeakIDMap(IDSpace space)
Method Detail

initTransient

private void initTransient()

set

protected void set(int id,
                   java.lang.Object obj)
Description copied from class: IDMap
Access to the underlying IDMap.idToObject array is encapsulated through the IDMap.set(int, java.lang.Object) and IDMap.get(int) methods. Subclasses may use this hooks to add functionality whenever an object is stored or retrieved.

Overrides:
set in class IDMap

get

protected java.lang.Object get(int id)
Overrides:
get in class IDMap
See Also:
IDMap.set(int, java.lang.Object)

getIndexForID

protected int getIndexForID(int id)
Overrides:
getIndexForID in class IDMap

getObjects

public Iterator getObjects()
Specialized implementation for @T@IDMap.getObjects() that prevents returning null values for already garbage collected objects.

Overrides:
getObjects in class IDMap

getObjects

public Iterator getObjects(IntIterator ids)
Returns:
all objects that are identified by identifiers from the passed iterator. If an object for a given identifier has already been garbage collected, do not include a null value in the output sequence.

pollID

public int pollID()
Polls an internal ReferenceQueue for the next weakly reacheable object and returns the identifier that was associated with it. If there is no more weakly reacheable object, the method returns IDConstants.INVALID_ID.


waitForID

public int waitForID()
              throws java.lang.InterruptedException
Wait until an object that is referenced from this map becomes weakly reacheable and return its identifier.

Throws:
java.lang.InterruptedException
See Also:
pollID(), ReferenceQueue.remove()

waitForID

public int waitForID(long timeout)
              throws java.lang.InterruptedException
Wait at most timeout milliseconds until an object that is referenced from this map becomes weakly reacheable and return its identifier. If no object becomes unreferenced within timeout, IDConstants.INVALID_ID is returned.

Throws:
java.lang.InterruptedException
See Also:
pollID(), ReferenceQueue.remove(long)

getIDForRef

protected int getIDForRef(WeakIDMap.Ref ref)
Find the identifier for a given reference. No identifier is found, if the reference was already removed from this map. If that's the case, IDConstants.INVALID_ID is returned.

See Also:
IDMap.rawFindID(java.lang.Object, int)

restoreBeforeReadObject

protected void restoreBeforeReadObject()
                                throws java.lang.ClassNotFoundException,
                                       java.io.IOException
Overrides:
restoreBeforeReadObject in class IDMap
Throws:
java.lang.ClassNotFoundException
java.io.IOException