Created
January 24, 2014 14:29
-
-
Save b005t3r/8598305 to your computer and use it in GitHub Desktop.
Enum.as
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Enum implements Equalable, Comparable, Hashable, Cloneable { | |
private static const OrderMetadataName:String = "Order"; | |
private static var allConstantByClass:Object = {}; | |
private var value:String; | |
public static function allEnums(clazz:Class):Array { | |
var className:String = getQualifiedClassName(clazz); | |
var allConstants:Array = allConstantByClass[className]; | |
if(allConstants == null) | |
throw new IllegalOperationError("initEnums() was not called for class " + className); | |
return allConstants; | |
} | |
protected static function initEnums(clazz:Class):void { | |
var type:XML = describeType(clazz); | |
var className:String = getQualifiedClassName(clazz); | |
var allConstants:Array = allConstantByClass[className]; | |
if(allConstants != null) | |
throw new IllegalOperationError("initEnums() was already called for class " + className); | |
// set order is available | |
var order:Array = null; | |
for each (var metadataXML:XML in type.factory.metadata) { | |
if(metadataXML.@name != OrderMetadataName) | |
continue; | |
if(order == null) | |
order = []; | |
for each(var argXML:XML in metadataXML.arg) | |
order.push(String(argXML.@value)); | |
} | |
// add constant to collection | |
if(order == null) | |
allConstants = []; | |
else | |
allConstants = new Array(order.length); | |
for each (var constantXML:XML in type.constant) { | |
var constant:* = clazz[constantXML.@name]; | |
constant.value = constantXML.@name; | |
var index:int = order != null | |
? order.indexOf(String(constantXML.@name)) | |
: -1 | |
; | |
if(index >= 0) | |
allConstants[index] = constant; | |
else if(order == null) | |
allConstants.push(constant); | |
else | |
throw new UninitializedError(OrderMetadataName + "metadata does not contain all constants declared in " + className); | |
} | |
if(allConstants.length == 0) | |
throw new UninitializedError(className + "does not declare any constant"); | |
allConstantByClass[className] = allConstants; | |
} | |
public function allEnums():Array { | |
var clazz:Class = Objects.getClass(this); | |
return Enum.allEnums(clazz); | |
} | |
public function index():int { | |
var clazz:Class = Objects.getClass(this); | |
return Enum.allEnums(clazz).indexOf(this); | |
} | |
public function toString():String { | |
return value; | |
} | |
public function equals(object:Equalable):Boolean { | |
var clazz:Class = Objects.getClass(this); | |
if(! (object is clazz)) | |
return false; | |
var allConstants:Array = Enum.allEnums(clazz); | |
var thisIndex:int = allConstants.indexOf(this); | |
var objectIndex:int = allConstants.indexOf(object); | |
return thisIndex == objectIndex; | |
} | |
public function compareTo(object:Comparable):int { | |
var clazz:Class = Objects.getClass(this); | |
if(! (object is clazz)) | |
throw new ArgumentError("object (" + object + ") is not of class " + clazz); | |
var allConstants:Array = Enum.allEnums(clazz); | |
var thisIndex:int = allConstants.indexOf(this); | |
var objectIndex:int = allConstants.indexOf(object); | |
return thisIndex - objectIndex; | |
} | |
public function hashCode():int { | |
return Objects.hashCode(value); | |
} | |
public function clone(cloningContext:CloningContext = null):Cloneable { | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment