Base enum class for actionscript. Works pretty much like enums in Java, only you've got
to do one or two things.
To use, you'll want to subclass and have something like the following:
public final class Foo extends Enum
{
public static const ONE :Foo = new Foo("ONE");
public static const TWO :Foo = new Foo("TWO");
finishedEnumerating(Foo);
/ {at}private /
public function Foo (name :String)
{
super(name);
}
public static function valueOf (name :String) :Foo
{
return Enum.valueOf(Foo, name) as Foo;
}
public static function values () :Array
{
return Enum.values(Foo);
}
}
Important notes for Enum implementors:
- make your class final
- create a constructor that calls super(name)
- declare your enum constants const, and with the same String as their name.
- call finishedEnumerating() at the end of your constants.
- your enum objects should be immutable
- implement a static valueOf() and values() methods for extra points, as above.
Note for Enum users: The same Enum class in different ApplicationDomains could cause confusion,
as the Enums will not be equal (neither will their Class objects, or in all probabability,
the Hashable, Equalable, or Comparable classes, so there could be larger problems.
protected var _name:String
The String name of this enum value.
public function Enum(name:String)
Call this constructor in your enum subclass constructor.
Parameters
public function compareTo(other:Object):int
Parameters
Returns
public final function equals(other:Object):Boolean
Parameters
Returns
protected static function finishedEnumerating(clazz:Class):void
This should be called by your enum subclass after you've finished enumating the enum
constants. See the example in the class header documentation.
Parameters
public final function hashCode():int
Returns
public final function name():String
Get the name of this enum.
Returns
public final function ordinal():int
Get the ordinal of this enum.
Note that you should not use the ordinal in normal cases, as it may change if a new
enum is defined. Ordinals should only be used if you are writing a data structure
that generically handles enums in an efficient manner, and you are never persisting
anything where the ordinal can change.
Returns
public function toString():String
Return the String representation of this enum.
Returns
public function valueOf():Object
Return the primitive value of this Object.
The default implementation for Enums is to return the ordinal.
Returns
public static function values(clazz:Class):Array
Get all the enums of the specified class, or null if it's not an enum.
Parameters
Returns
Copyright © 2007-2009 Three Rings Design, Inc.