/*
 Copyright (c) 2006 - 2009  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.commons.collections
{
    import mx.collections.ArrayCollection;

    /**
     * 
     * <code>TypedCollection</code> provides a Type-safe <code>IList</code>
     * implementation for working with <code>ArrayCollection</code> instances.
     * 
     */
    public class TypedCollection extends ArrayCollection
    {
        /**
         *
         * Specifies the <code>Class</code> object or <code>interface</code> from which 
         * objects added to the collection must be of type. 
         * 
         */        
        protected var Type:Class;
        
        /**
         *
         * <code>AbstractTypeSafeCollection</code> requires the source <code>Array</code>
         * as well as the specific Type from which the TypeSafe Collection will accept
         * object instances.
         * 
         *  
         * @param source Array containing object of the specified Type
         * @param Class from which all objects added to the colleciton must belong
         * 
         */        
        public function TypedCollection(source:Array, Type:Class)
        {
            this.source = source;
            this.Type   = Type;
            
            if ( source != null )
            {
                var n:int = source.length;
                
                for ( var i:int = 0; i < n; i++ )
                {
                    validateItem( source[ i ] );
                }
            }
        }
        
        /**
         * 
         * @inheritDoc
         * 
         */        
        override public function addItem(item:Object) : void
        {
            validateItem( item );
            super.addItem( item );
        }
        
        /**
         * 
         * @inheritDoc
         * 
         */      
        override public function addItemAt(item:Object, index:int) : void
        {
            validateItem( item );
            super.addItemAt( item, index );
        }
        
        /**
         * 
         * @inheritDoc
         * 
         */     
        override public function setItemAt(item:Object, index:int) : Object
        {
            throw new Error( "TypedCollection.setItemAt is an Unsupported Operation" );
        }
        
        /**
         * 
         * <code>verifyType</code> validates objects added to the collection to 
         * determine that they are of the specified type defined by the constructor
         * <code>Type</code> <code>Class</code> argument.
         * 
         */          
        protected function validateItem(item:Object) : void
        {
            if ( item is Type )
            {
                return;
            }
            throw new Error( "Unsupported type specified. Only objects of type " + Type + " are valid" );    
        }
    }
}