Actionscript Vector class initialization with a source Array

What you maybe already know:

Vector class is a powerful class introduced with flash player 10. It is fundamentally a special typed array into which all elements must be the same type and it is faster than a normal Array.

What you maybe don’t know:

It’s possible to initialize a Vector with an Array or another Vector as a source but you must use the Vector function not the class constructor. The class constructor in fact has the following signature:

Vector(length:uint = 0, fixed:Boolean = false)

Vector function instead has this:


So, rather than using such approach:

var vector:Vector.<String> = new Vector.<String>();
vector[0] = "value1";
vector[1] = "value2";
vector[2] = "value3";
// ...and so on

We can do the following (Notice the absence of “this” keyword):

// create a vector with an inline Array as source
var vector:Vector.<String> = Vector.<String>(["v1", "v2", "v3"]);

// create a vector with an inline Vector as source
var vector2:Vector.<String> = Vector.<String>(vector);

// Testing (both trace will print the same string)

ArrayCollection: filterFuntion with multiple filter functions using decorator pattern

I was looking for an elegant solution to implement multiple filter functions on an ArrayCollection (which provides a method filterFunction() that can be assigned dynamically in order to filter data inside the collection). The first and only valid solution I found was that one from Cristian Rotundu, he has extended ArrayCollection class providing a filterFunctions properties which accepts an Array of functions which will be executed in sequence (into a loop) once filterFunction is triggered. Anyway I didn’t want to subclass ArrayCollection and by using the decorator pattern I implemented my own multiple filters version.

So, this is what I done:

  1. I defined an interface IFilter, which declares only one method: apply()
  2. I defined a dummy Filter class (which implements IFilter in a basic way just to agree to the interface), that is used as a basic empty filter to decorate. It also has a constant called ALL_VALUES which is used as a wildcard for filters (it’s a simple string with value “*”, which means “all values are allowed”)
  3. I defined an abstract AbstractFilterDecorator class
  4. I created as many subclass of AbstractFilterDecorator as the filters I need (yes I treat filters as classes not mere functions)

The implementation code is the following:

var data:ArrayCollection = ArrayCollection(grid.dataProvider);
var filter:IFilter = new Filter();
filter = new LevelFilter(filter, levelValue);
filter = new CategoryFilter(filter, categoryValue);
filter = new DateFilter(filter, dateValue, "DD/MM/YY");
data.filterFunction = filter.apply;

The filter object is wrapped by decorators which all accepts 2 common arguments: an IFilter reference and an Object representing a value. The DateFilter has a third parameter which is used to configure an internal DateFormatter (from mx.formatters package).
I’m pretty satisfied :)
Uh… and if you are worried about performance, it is fast enough because filter object are created once and then the function assigned to filterFunction is a reference to the resulting decorated filter.

I realized a very simple example application which can be downloaded here.
This following is the content of the mxml file:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="vertical">

			import mx.collections.ArrayCollection;
			import com.daveoncode.filters.*;
			private function applyFilters():void {
				var data:ArrayCollection  = ArrayCollection(userGrid.dataProvider);
				var filter:IFilter = new Filter();
				filter = new AgeFilter(filter, ageFilterValue.text);
				filter = new DateFilter(filter, dateFilterValue.selectedDate);
				filter = new SexFilter(filter, sexFilterValue.value);
				data.filterFunction = filter.apply;
	<mx:Panel title="Filters" width="600">
			<mx:FormItem label="Age:">
				<mx:TextInput id="ageFilterValue" width="30" />
			<mx:FormItem label="Sex:">
				<mx:ComboBox id="sexFilterValue" dataProvider="{[{data: Filter.ALL_VALUES, label: 'ALL'}, {data: 'm', label: 'Male'}, {data: 'f', label: 'Female'}]}" />
			<mx:FormItem label="Join date:">
				<mx:DateField id="dateFilterValue" formatString="DD/MM/YY" />
				<mx:Button label="Apply filters" click="{this.applyFilters()}" />
	<mx:Panel title="Users" width="600">
		<mx:DataGrid id="userGrid" width="100%">
						<mx:Object age="24" sex="f" name="Susan" joinDate="{new Date(2007, 5, 15)}" />
						<mx:Object age="36" sex="f" name="Ashley" joinDate="{new Date(1998, 7, 20)}" />
						<mx:Object age="24" sex="f" name="Jennifer" joinDate="{new Date(2001, 3, 24)}" />
						<mx:Object age="19" sex="f" name="Emma" joinDate="{new Date(2002, 3, 24)}" />
						<mx:Object age="44" sex="f" name="Carol" joinDate="{new Date(1999, 9, 16)}" />
						<mx:Object age="28" sex="m" name="Peter" joinDate="{new Date(2005, 3, 12)}" />
						<mx:Object age="35" sex="m" name="Mike" joinDate="{new Date(2008, 10, 10)}" />
						<mx:Object age="26" sex="m" name="Dave" joinDate="{new Date(2008, 10, 10)}" />
						<mx:Object age="44" sex="m" name="William" joinDate="{new Date(2004, 9, 16)}" />
						<mx:Object age="24" sex="m" name="Sean" joinDate="{new Date(2006, 3, 24)}" />

Converting Java to Actionscript… maybe is not so hard (I hope)

After valuate my own realization of a certain library, I opted to try the porting of an already existent one… which is made in Java. This library has a lot of classes and interfaces and the manually translation would be very time-expensive, considering also that it uses a lot of classes and functions which are unavailable in Actionscript 3. Fortunately I found a small and useful AIR application which automatically converts .java files to .as files (the application is here:, this application anyway helps a lot but many extra-work is required to properly convert the classes… for example Actionscript has not the abstract key, variable can’t be market as final and so on, so we have to manually adjust these issues. Another useful stuff I found is an Actionscript library which aim is to provide to AS3 developers the same (more or less) features and power of Java Collection framework, which provide classes like HashMap, ArrayList, LinkedList and so on (the library can be found here: Despite these helps I’m still facing some incompatibility issues… classes/methods AS3 is missing, but implementing them by myself is quite simple. For example I implemented the Java’s System.arraycopy() in 2 minutes, by following the JavaDoc ).

In conclusion Actionscript and Java have a  lot of stuff in common, the syntax and structure is quite the same, except for some issues like abstract classes and methods, multiple methods and constructors declaration (override). The main difference is that Java provides an huge number of Classes that Actionscript doesn’t, however it seems not so hard to translate Java code and implement Java functionality in Actionscript.

I hope I won’t have to deny my words :)