package textbender.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 "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender 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 Textbender Software. THE TEXTBENDER 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 TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.util.Collection; import textbender.g.lang.*; /** An unwindable spool of {@linkplain Hold 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 {@linkplain Hold Holds}, * and wind them onto a spool. The spool unwinds later, at closure, * and {@linkplain Hold#release() 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. *

*/ public interface Spool extends Collection { /** A common instance of a null catcher. */ public static final Catcher0 catcher0 = new Catcher0(); // - C o l l e c t i o n -------------------------------------------------------------- /** Adds the hold to the spool, or releases it immediately. * If the spool is unwinding 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 ); // - S p o o l ------------------------------------------------------------------------ /** Returns true if the spool is {@linkplain #unwind() unwinding} (or unwound). * Once true, it never reverts to false. */ public boolean isUnwinding(); /** Commences to unwind this spool. * Equivalent to {@linkplain #unwind(Catcher) unwind}(catcher0). * * @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 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 ); }