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" );
}
}
}