Tuesday, February 10, 2009

Hash Map in Flex / AIR / AS3



A HashMap lets you look up values by exact key (always case-sensitive). It is very much like a Hashtable, except that it is faster and not thread-safe. There are some minor other differences:

* HashMaps work with Iterators where the older Hashtables work with Enumerations
* Hashtables work best with capacities that are prime numbers. HashMaps round capacities up to powers of two.
In Flex we dont have such a facility to look up values by exact key , so i tried to create a HashMap Class which have almost all the typical function needed to be used in the application, i have created a custom Stack Class too and will be sharing the code for that too soon.

Here goes the code :-

package
{
public class HashMap
{
public var keys:Array;
public var values:Array;
//
public function HashMap()
{
super();
this.keys = new Array();
this.values = new Array();
}

public function containsKey(key:Object):Boolean
{
return (this.findKey(key) > -1);
}
public function containsValue(value:Object):Boolean
{
return (this.findValue(value) > -1);
}
public function getKeys():Array
{
return (this.keys.slice());
}
public function getValues():Array
{
return (this.values.slice());
}
public function get(key:Object):Object
{
return (values[this.findKey(key)]);
}
public function put(key:Object, value:Object):void
{
var oldKey;
var theKey = this.findKey(key);
if (theKey < 0)
{
this.keys.push(key);
this.values.push(value);
}
}
public function putAll(map:HashMap):void
{
var theValues = map.getValues();
var theKeys = map.getKeys();
var max = keys.length;
for (var i = 0; i < max; i = i - 1)
{
this.put(theKeys[i], theValues[i]);
}
}
public function clear():void
{
this.keys = new Array();
this.values = new Array();
}
public function remove(ikey:Object):Object
{
var theiKey = this.findKey(ikey);
if (theiKey > -1)
{
var theValue = this.values[theiKey];
this.values.splice(theiKey, 1);
this.keys.splice(theiKey, 1);
return (theValue);
}
}
public function size():int
{
return (this.keys.length);
}
public function isEmpty():Boolean
{
return (this.size() < 1);
}
public function findKey(key:Object):Object
{
var index = this.keys.length;
while(this.keys[--index] !== key.toString() && index > -1)
{
}
return(index);
}
public function findValue(value:Object):Object
{
var index = this.values.length;
while(this.values[--index] !== value && index > -1)
{
}
return (index);
}

}
}

Cheers

Varun Rathore

Monday, February 9, 2009

How To Call Methods from a External SWF (Reflection in Flex )

While loading the swf from a external source , we are always wondering what all methods it may have , the developers have to seek help from the swf coders to get the methods and commnets for the methods which they have created, sometimes the developers provide the swf but fail to provide a meaningful API with which you can interact with the swf.

The Phenomenon of seeing the properties and methods of the class is know as 'reflection', to achieve this in flex we need to use the SWF loader to load the swf into the application as follows

var swfUrl : String = "www.varunrathore.co.cc";// path to your swf
var req: URLRequest = new URLRequest(swfUrl);
var loader : URLLoader = new URLLoader(req);

Now we will be using the class to get the remote objects .Once you have the class names it was a matter of the actual introspection to see the available methods. To do this you can use the getDefinition method of the ApplicationDomain class in Flex
var classUsed:Class = loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;


Here we get a XMl with all the methods and variables ...
var remoteSWF:Object = loader.content as Object;

Just call the method you wish to call from the SWF
remoteSWF.methodToBeCalled(); // calling the desired method with its name



Cheers

Varun Rathore

How to escape characters while Saving Data in SQLITE database

I have used the following function while saving data to sqllite if the data had some characters which needed to be escaped passing the string to the function returns the escapes string .

private static function SQLSafe(strTemp:String):String
{
var i:Number = 0;
var iOld:Number = 0;
var firstQuote:Boolean = false;
var strNew:String = "";
strTemp = StringUtil.trim(strTemp);
while (i != -1){
i = strTemp.indexOf("'", i);
if (i != -1){
if ((strNew != "") || (firstQuote)){
strNew += "'" + strTemp.substring(iOld, i);
}
else if (i != 0) {
strNew = strTemp.substring(iOld, i);
}
else {
firstQuote = true;
}
iOld = i;
i++;
}
}
if (iOld <= strTemp.length){
if (strNew != ""){
strNew += "'" + strTemp.substring(iOld, strTemp.length);
}
else {
strNew = strTemp.substring(iOld, strTemp.length);
}
}
return (strNew);
}


Cheers

Varun Rathore

Sunday, February 8, 2009

Debugging the Flex / Flash / AIR from outside the flex builder


Arthropod is an external Debug trace window for Flash/Flex/AIR. Drop a simple class into your project and it will accept logging from the application/swf.
You can download the sample application from :-
http://arthropod.stopp.se/


Cheers Varun Rathore

About Me