Class WeakTable

java.lang.Object
com.sun.jini.collection.WeakTable

public class WeakTable extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static interface 
    Handler for clients that need to know when a key is removed from the table because it has been collected.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new WeakTable object to maintain the maps.
    Create a new WeakTable object to maintain the maps that calls back the designated object when keys are collected.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(Object key)
    Return the value associated with given key, or null if no value can be found.
    getOrAdd(Object key, Object proxy)
    Return the object that this key maps to.
    Remove the object that the given key maps to.
    void
    Remove any blank entries from the table.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WeakTable

      public WeakTable()
      Create a new WeakTable object to maintain the maps.
    • WeakTable

      public WeakTable(WeakTable.KeyGCHandler handler)
      Create a new WeakTable object to maintain the maps that calls back the designated object when keys are collected.
  • Method Details

    • getOrAdd

      public Object getOrAdd(Object key, Object proxy)
      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

      public Object get(Object key)
      Return the value associated with given key, or null if no value can be found.
    • remove

      public Object remove(Object key)
      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.