danger.util
Class SimpleWeakList

java.lang.Object
  extended by danger.util.SimpleWeakList
Direct Known Subclasses:
ActiveList.WeakDelegateList, ActiveObject.WeakDelegateList


public class SimpleWeakList
extends Object

List of objects which are held onto weakly. This class is meant to be subclassed in order to do truly interesting things with the contents of the list.


Constructor Summary
SimpleWeakList()
          Construct an instance with an initial capacity of 4.
SimpleWeakList(int initialCapacity)
          Construct an instance.
 
Method Summary
protected  void accept(Object obj)
          Method called by accept(Object,Object) in response to a call to forEachAccept() or forEachAccept(Object).
protected  void accept(Object obj, Object arg)
          Method called by forEachAccept(Object).
 void add(Object obj)
          Add an object to the end of the list maintained this instance.
 boolean contains(Object obj)
          Determine if the list already contains an object.
 void forEachAccept()
          Call accept(java.lang.Object) on each live object in the list, in order.
 void forEachAccept(Object arg)
          Call accept(java.lang.Object) on each live object in the list, in order.
 Object get(int n)
          Get the nth object in the list, which may turn out to be a reaped reference.
protected  boolean matchesTarget(Object toCompare, Object target)
          Method called by remove(java.lang.Object) and contains(java.lang.Object).
 void remove(Object obj)
          Remove an object from this instance that had previously been added.
 int size()
          Get the size of the list (which may include reaped references).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleWeakList

public SimpleWeakList(int initialCapacity)
Construct an instance.

Parameters:
initialCapacity - initial capacity of the list; it will expand as needed

SimpleWeakList

public SimpleWeakList()
Construct an instance with an initial capacity of 4.

Method Detail

add

public final void add(Object obj)
Add an object to the end of the list maintained this instance.

Parameters:
obj - non-null; the object to add

remove

public final void remove(Object obj)
Remove an object from this instance that had previously been added.

Parameters:
obj - non-null; an object identifying the object to be removed. If the result of passing an object from the list and this object to matchesTarget(java.lang.Object, java.lang.Object) is true, then the object from the list is removed.
Throws:
IllegalArgumentException - thrown if the obj had not been added to this instance

contains

public final boolean contains(Object obj)
Determine if the list already contains an object.

Parameters:
obj - an object identifying the object to look for. If the result of passing an object from the list and this object to matchesTarget(java.lang.Object, java.lang.Object) is true, then this method returns true.

size

public final int size()
Get the size of the list (which may include reaped references).

Returns:
the size

get

public final Object get(int n)
Get the nth object in the list, which may turn out to be a reaped reference.

Parameters:
n - which element to get
Returns:
null-ok; the nth object, or null if it was reaped

forEachAccept

public final void forEachAccept()
Call accept(java.lang.Object) on each live object in the list, in order.


forEachAccept

public final void forEachAccept(Object arg)
Call accept(java.lang.Object) on each live object in the list, in order.

Parameters:
arg - used as second argument to accept(java.lang.Object).

accept

protected void accept(Object obj)
Method called by accept(Object,Object) in response to a call to forEachAccept() or forEachAccept(Object). This implementation does nothing, but subclasses may override it to do something interesting, if need be.

You should override either this method, or accept(Object,Object), but not both.

Parameters:
obj - non-null; a live object from the list

accept

protected void accept(Object obj,
                      Object arg)
Method called by forEachAccept(Object). This implementation does nothing, but subclasses may override it to do something interesting, if need be.

You should override either this method, or accept(Object), but not both.

Parameters:
obj - non-null; a live object from the list
arg - argument passed into forEachAccept(Object).

matchesTarget

protected boolean matchesTarget(Object toCompare,
                                Object target)
Method called by remove(java.lang.Object) and contains(java.lang.Object). This implementation simply compares the two arguments using ==.

Parameters:
toCompare - non-null; an object in the list
target - non-null; the argument originally passed into remove(java.lang.Object) or contains(java.lang.Object).