Utility function for converting from ordinal to Enum

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5849 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Tom Conkling
2009-07-04 22:48:21 +00:00
parent 21712c5883
commit 95a159016f
+13 -1
View File
@@ -143,6 +143,18 @@ public class Enum
return Integer.compare(this.ordinal(), Enum(other).ordinal());
}
/**
* Return the Enum of the given class with the given ordinal value.
*/
public static function fromOrdinal (clazz :Class, ordinal :int) :Enum
{
var values :Array = (_enums[ClassUtil.getClass(clazz)] as Array);
if (values != null && ordinal >= 0 && ordinal < values.length) {
return values[ordinal];
}
throw new ArgumentError("No such enum [class=" + clazz + ", ordinal=" + ordinal + "].");
}
/**
* Turn a String name into an Enum constant.
*/
@@ -181,7 +193,7 @@ public class Enum
protected var _name :String;
/** An array of enums for each enum class. */
private static const _enums :Dictionary = new Dictionary(true);
private static const _enums :Dictionary = new Dictionary(true);
/** Is further instantiation of enum constants for a class allowed? */
private static const _blocked :Dictionary = new Dictionary(true);