Package com.sun.jini.collection
Class WeakTable
java.lang.Object
com.sun.jini.collection.WeakTable
This class is designed to allow weakly held keys to weakly held objects. For example, it can be
used for smart proxy objects that maintain equality for references to the same remote server. If
a single VM twice invokes a remote method that returns a proxy for the same JavaSpaces(TM)
server, the references returned by that method should be the same. This allows
==
tests to work for proxies to remote servers the same as they would for direct references to
remote servers, which also maintain this property. Here is an example that uses this class to
ensure that exactly one copy of a java.io.Resolvable object exists in each VM:
private WeakTable knownProxies;
public Object readResolve() {
// deferred creation means this table is not allocated on the server
if (knownProxies == null)
knownProxies = new WeakTable();
return knownProxies.getOrAdd(remoteServer, this);
}
- Author:
- Sun Microsystems, Inc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceHandler for clients that need to know when a key is removed from the table because it has been collected. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new WeakTable object to maintain the maps.WeakTable(WeakTable.KeyGCHandler handler) Create a new WeakTable object to maintain the maps that calls back the designated object when keys are collected. -
Method Summary
Modifier and TypeMethodDescriptionReturn the value associated with given key, ornullif no value can be found.Return the object that this key maps to.Remove the object that the given key maps to.voidRemove any blank entries from the table.
-
Constructor Details
-
WeakTable
public WeakTable()Create a new WeakTable object to maintain the maps. -
WeakTable
Create a new WeakTable object to maintain the maps that calls back the designated object when keys are collected.
-
-
Method Details
-
getOrAdd
Return the object that this key maps to. If it currently maps to no object, set it to map to the given object. Return either the existing entry or the new one, whichever is used. -
get
Return the value associated with given key, ornullif no value can be found. -
remove
Remove the object that the given key maps to. If found return the object, otherwise return null. -
removeBlanks
public void removeBlanks()Remove any blank entries from the table. This can be invoked by a reaping thread if you like.
-