/*
 Copyright (c) 2006 - 2008  Eric J. Feminella  <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE 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 SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.

 @internal
 */

package com.ericfeminella.collections
{
    /**
     *
     * <code>ArrayList</code> is a convenience class which extends <code>Array</code>
     * in order to provide access to an <code>Iterator</code> of the collection.
     *
     * <p>
     * <code>ArrayList</code> provides the same API as the top level <code>Array</code>
     * class, with the only exception being <code>[]</code> notation can not be used as
     * with <code>Array</code>. New instances of <code>ArrayList</code> must be instantiated
     * via <code>new ArrayList()</code>.
     * </p>
     *
     * <p>
     * <code>ArrayList</code> is also not an <code>ArrayList</code> in terms of boxing and
     * unboxing to explicit types. References to each index of an <code>ArrayList</code>
     * return an object of the type which was placed in the <code>Array</code>.
     * </p>
     *
     * @example A basic example is as follows:
     *
     * <listing version="3.0" >
     *
     * var list:ArrayList = new ArrayList()
     * list.push( "A" );
     * list.push( "B" );
     * list.push( "C" );
     *
     * var it:Iterator = list.iterator;
     *
     * while ( it.hasNext() )
     * {
     *     trace( it.next(), it.position());
     * }
     *
     * //outputs:
     * //A
     * //B
     * //C
     *
     * </listing>
     *
     * @see com.ericfeminella.collections.Iterator
     *
     */
    public class ArrayList extends Array implements Iterable
    {
        /**
         *
         * <code>ArrayList</code> constructor accepts an optional argument of
         * type <code>Array</code>
         *
         * @param underlying <code>Array</code> wrapped by the collection
         *
         */
        public function ArrayList(numElements:int = 0)
        {
            super( numElements );
        }

        /**
         *
         * Retrieves the <code>Iterator</code> object for the <code>ArrayList</code>.
         *
         * @return <code>Iterator</code>
         *
         */
        public function get iterator() : Iterator
        {
            return new ArrayIterator( this );
        }

        /**
         *
         * Convenience factory method which creates an <code>ArrayList</code>
         * from an <code>Array</code>
         *
         * @param  <code>Array</code> from which an <code>ArrayList</code> is to be created
         * @return <code>ArrayList</code> referencing the underlying <code>Array</code>
         *
         */
        public static function createArrayList(array:Array) : ArrayList
        {
            var n:uint = array.length;

            var list:ArrayList = new ArrayList( n );

            for (var i:int = 0; i < n; i++)
            {
                list.push( array[i] );
            }
            return list;
        }
    }
}