textbender.g.hold
Interface Spool

All Superinterfaces:
Collection<Hold>, Iterable<Hold>
All Known Implementing Classes:
Spool0, Spool1, SpoolEDT, SpoolT

public interface Spool
extends Collection<Hold>

An unwindable spool of holds. It models the 'closure' of an object, and the unwinding of its state. As an object takes hold of resources that later need to be released, it may formally model them Holds, and wind them onto a spool. The spool unwinds later, at closure, and releases the holds.

An example is listener registration. This is state that often needs to be unwound, in order to prevent memory leaks. Using a spool, both registration and unregistration may be coded together in line. For example:

     registry.addListener( C.this );
     spool.add( new Hold()
     {
         public void release() { registry.removeListener( C.this ); }
     });
     

The life cycle of spools follows two common patterns. In one, a spool is created internally by the object, and later unwound by it (in a public close() method, for example) In the other pattern, a spool is passed to the object by a contruction parameter, and later unwound by its creator (effectively destroying the object, and any others that had wound themselves onto it). The latter is the more common pattern; but textbender's codebase is full of examples of both.


Field Summary
static Catcher0<Hold> catcher0
          A common instance of a null catcher.
 
Method Summary
 boolean add(Hold hold)
          Adds the hold to the spool, or releases it immediately.
 boolean isUnwinding()
          Returns true if the spool is unwinding (or unwound).
 boolean unwind()
          Commences to unwind this spool.
 boolean unwind(Catcher<Hold> catcher)
          Commences to unwind this spool, removing and releasing each of its holds.
 
Methods inherited from interface java.util.Collection
addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Field Detail

catcher0

static final Catcher0<Hold> catcher0
A common instance of a null catcher.

Method Detail

add

boolean add(Hold hold)
Adds the hold to the spool, or releases it immediately. If the spool is unwinding the hold is released immediately; otherwise it is added.

Specified by:
add in interface Collection<Hold>
Returns:
true if the hold is added; false if it is released instead
Throws:
IllegalStateException - if this contract cannot be met (e.g. if the spool runs out of space, and no other unchecked exception applies)

isUnwinding

boolean isUnwinding()
Returns true if the spool is unwinding (or unwound). Once true, it never reverts to false.


unwind

boolean unwind()
Commences to unwind this spool. Equivalent to unwind(catcher0).

Returns:
true if unwinding commences with this call; false if it had already commenced

unwind

boolean unwind(Catcher<Hold> catcher)
Commences to unwind this spool, removing and releasing each of its holds. Works in LIFO order. Once unwinding is commenced, subsequent calls to this method have no effect.

Parameters:
catcher - for any errors or exceptions that occur during unwinding
Returns:
true if unwinding commences with this call; false if it had already commenced