public interface Spool
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 construction 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 the codebase [of textbender] has examples of both. In any case, once unwound, a spool is never resused.
Modifier and Type | Field and Description |
---|---|
static Catcher0R<Hold> |
CATCHER_0
A common instance of a null catcher.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(Hold hold)
Adds the hold to the spool or releases it immediately.
|
boolean |
isUnwinding()
Returns true if the spool is unwinding or already 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.
|
boolean add(Hold hold)
IllegalStateException
- if this contract cannot be met (e.g. if the
spool runs out of space, and no other unchecked exception applies).boolean isUnwinding()
boolean unwind()
boolean unwind(Catcher<Hold> catcher)
catcher
- the catcher for any errors or exceptions that occur during
unwinding.