ActionScript 3.0, Adobe Flex

TypeComparison FilterFunction Implementation

When applying a filterFunction to a collection the code can get a little slightly messy. Finding that I many times want to filter a collection containing objects of different types by the type of object and in order to reduce the mess, I have created a small class which encapsulates this filterfunction. I wish Function was not declared Final… all the cool things what would be made possible then 🙂

Anyways… while we are waiting for true reflection to find its way into our midst, here is a small implementation resembling a helper class that does the job nicely.

 
 

Download source here

     
 

/**
* Copyright(c) 2008 HelloGroup A/S, some rights reserved.
* Your reuse is governed by the Creative Commons Attribution 3.0 Denmark License
**/
package com.hello.collections
{
    public class TypeCompareFunction
    {
        public var type:Class;

        public function TypeCompareFunction( type:Class )
        {
            super();

            this.type = type;
        }

        public function getFunction() : Function
        {
            return compare;
        }

        private function compare( item:Object ) : Boolean
        {
            return item is type;
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s