ActionScript 3.0

com.hello.formatters.GuidFormatter

Today I needed a fomatter for GUID’s.

I made a quick and dirty extension to the Formatter base class that uses our existing com.hello.core.Guid class to fetch a format constant and then use an instance of the mx.formatters.SwitchSymbolFormatter to apply the pattern to the string representation of the Guid we want to format.

The implementation looks like this… don’t think too much about the details of the implementation – it has been simplified to serve as example of the general idea.

package com.hello.formatters

{

import com.hello.core.Guid;

import mx.formatters.Formatter;

import mx.formatters.SwitchSymbolFormatter;

public
class GuidFormatter extends Formatter

{

public
function GuidFormatter()

{

super();

}


override
public
function format( value:Object ) : String

{

var result:String = “”;


var valueStringLength:Number = value.toString().length;

switch( valueStringLength )

{

case 36:

result = value.toString();

break;


case 32:

var dataFormatter:SwitchSymbolFormatter = new SwitchSymbolFormatter();

result = dataFormatter.formatValue( Guid.FORMAT, value );

break;


default:

error = ERROR_MESSAGE;

break;

}


return result;

}


public
static
const ERROR_MESSAGE:String = “guidFormatterErrror”;

}

}

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