PageRenderTime 134ms CodeModel.GetById 129ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/src/com/ericfeminella/collections/CollectionIterator.as

http://transcriptstudio4isha.googlecode.com/
ActionScript | 173 lines | 45 code | 14 blank | 114 comment | 2 complexity | f95c7529c78e2ecad3e26e407bd187ce MD5 | raw file
  1/*
  2 Copyright (c) 2006 - 2008  Eric J. Feminella  <eric@ericfeminella.com>
  3 All rights reserved.
  4
  5 Permission is hereby granted, free of charge, to any person obtaining a copy
  6 of this software and associated documentation files (the "Software"), to deal
  7 in the Software without restriction, including without limitation the rights
  8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 copies of the Software, and to permit persons to whom the Software is furnished
 10 to do so, subject to the following conditions:
 11
 12 The above copyright notice and this permission notice shall be included in all
 13 copies or substantial portions of the Software.
 14
 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 16 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 17 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 18 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 19 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 20 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21
 22 @internal
 23 */
 24
 25package com.ericfeminella.collections
 26{
 27    import mx.collections.CursorBookmark;
 28    import mx.collections.ICollectionView;
 29    import mx.collections.IViewCursor;
 30
 31    /**
 32     *
 33     * Concrete <code>Iterator</code> implementation which provides an API
 34     * for iterating over an <code>ICollectionView</code>.
 35     *
 36     * @example The following is a basic <code>CollectionIterator</code> 
 37     * example:
 38     * 
 39     * <listing version="3.0">
 40     *
 41     * var collection:ICollectionView = new ArrayCollection(["a","b","c"]);
 42     * var it:Iterator = new CollectionIterator( collection );
 43     *
 44     * while ( it.hasNext() )
 45     * {
 46     *     trace( it.next(), it.position() );
 47     * }
 48     *
 49     * // a, 0
 50     * // b, 1
 51     * // c, 2
 52     *
 53     * </listing>
 54     *
 55     * @see com.ericfeminella.collections.Iterator
 56     * @see mx.collections.ICollectionView
 57     * @see mx.collections.CursorBookmark
 58     * @see mx.collections.IViewCursor
 59     *
 60     */
 61    public class CollectionIterator implements Iterator
 62    {
 63        /**
 64         *
 65         * Defines the <code>ICollectionView</code> instance
 66         *
 67         * @see mx.collections.ICollectionView
 68         *
 69         */
 70        protected var collection:ICollectionView;
 71
 72        /**
 73         *
 74         * Defines the <code>IViewCursor</code> instance to the aggregate
 75         *
 76         * @see mx.collections.IViewCursor
 77         *
 78         */
 79        protected var cursor:IViewCursor;
 80
 81        /**
 82         *
 83         * Contains the current indexed position in the collection
 84         *
 85         */
 86        protected var index:int;
 87
 88        /**
 89         *
 90         * Instantiates a new <code>CollectionIterator</code> instance
 91         *
 92         * @param the collection in which to iterate over
 93         *
 94         */
 95        public function CollectionIterator(collection:ICollectionView)
 96        {
 97            setCollection( collection );
 98        }
 99
100        /**
101         *
102         * Sets the collection in which to iterate over
103         *
104         * @param an ICollectionView instance
105         *
106         */
107        public function setCollection(collection:ICollectionView) : void
108        {
109            this.collection = collection;
110            cursor = collection.createCursor();
111        }
112
113        /**
114         *
115         * Determines if there are elements remaining in the ICollectionView
116         *
117         * @return  true if an element remains, false if not
118         *
119         */
120        public function hasNext() : Boolean
121        {
122            var result:Boolean = true;
123
124            if ( cursor.beforeFirst || cursor.afterLast )
125            {
126                result=  false;
127            }
128
129            return result;
130        }
131
132        /**
133         *
134         * Returns the next item in the ICollectionView
135         *
136         * @retrun an arbitrary object in the collection
137         *
138         */
139        public function next() : *
140        {
141            var current:Object = cursor.current;
142
143            index = cursor.bookmark.getViewIndex();
144            cursor.moveNext();
145
146            return current;
147        }
148
149        /**
150         *
151         * Resets the IViewCursor to a zero based indexed position
152         *
153         * @see mx.collections.CursorBookmark#FIRST
154         *
155         */
156        public function reset() : void
157        {
158            cursor.seek( CursorBookmark.FIRST );
159        }
160
161        /**
162         *
163         * Determines the IViewCursor position of the ICollectionView
164         *
165         * @return  the current position of the aggreagate
166         *
167         */
168        public function position() : int
169        {
170            return index;
171        }
172    }
173}