package votorola.g.hold; // Copyright 2005-2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import votorola.g.lang.*; /** An unwindable spool of {@linkplain Hold holds}. It contols and implements the closure * of an object - the "unwinding" of its state. As an object takes hold of resources * that later need to be released, it constructs a {@linkplain Hold Hold} for each, and * winds it onto a spool. At closure the object, the spool is unwound, and each hold is * {@linkplain Hold#release() released}. * *

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.

*/ public interface Spool { /** A common instance of a null catcher. */ public static final Catcher0R CATCHER_0 = Catcher0R.i(); // till we figure out how to declare Catcher0R.i() so we can use a direct reference instead // - S p o o l ------------------------------------------------------------------------ /** Adds the hold to the spool or releases it immediately. If the spool is unwinding * then the hold is released immediately, otherwise it is added. * * @throws IllegalStateException if this contract cannot be met (e.g. if the * spool runs out of space, and no other unchecked exception applies). * * @return true if the hold is added; false if it is released instead. */ public boolean add( Hold hold ); /** Returns true if the spool is {@linkplain #unwind() unwinding} or already unwound. * Once true, it never reverts to false. */ public boolean isUnwinding(); /** Commences to unwind this spool. Equivalent to {@linkplain #unwind(Catcher) * unwind}(CATCHER_0). * * @return true if unwinding commences with this call; false if it had already * commenced. */ public boolean unwind(); /** 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. * * @param catcher the catcher for any errors or exceptions that occur during * unwinding. * * @return true if unwinding commences with this call; false if it had already * commenced. */ public boolean unwind( Catcher catcher ); }