001package votorola.g.hold; // Copyright 2005-2006, 2010, 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.
002
003import java.util.*;
004import votorola.g.lang.Catcher;
005
006
007/** An implementation of a spool based on a linked list.
008  */
009public final class Spool1 implements Spool
010{
011
012
013   // - S p o o l ------------------------------------------------------------------------
014
015
016    public boolean add( Hold hold )
017    {
018        if( isUnwinding )
019        {
020            hold.release();
021            return false;
022        }
023
024        if( !list.add( hold )) throw new IllegalStateException();
025
026        return true;
027    }
028
029
030
031    public boolean isUnwinding() { return isUnwinding; }
032
033
034        private boolean isUnwinding;
035
036
037
038    public boolean unwind() { return unwind( CATCHER_0 ); }
039
040
041
042    public boolean unwind( final Catcher<Hold> catcher )
043    {
044        if( isUnwinding ) return false;
045
046        if( catcher == null ) throw new NullPointerException();
047
048        isUnwinding = true;
049        while( !list.isEmpty() )
050        {
051            Hold hold = list.removeLast();
052            try{ hold.release(); }
053            catch( Error r ) { catcher.catchError( hold, r ); }
054            catch( Exception x ) { catcher.catchException( hold, x ); }
055        }
056        return true;
057    }
058
059
060
061//// P r i v a t e ///////////////////////////////////////////////////////////////////////
062
063
064    private final LinkedList<Hold> list = new LinkedList<Hold>();
065      // Composing in the list.  This is better than subclassing it, because LinkedList
066      // has multiple add() methods and the behaviour of this implementation's overrides
067      // would depend on which (if any) the superclass was calling into.  E.g. the add()
068      // implementation of GWT (2.1) calls addLast().
069
070
071
072}