From 95a159016ff39a882687dbb0029eeb1244ffd962 Mon Sep 17 00:00:00 2001 From: Tom Conkling Date: Sat, 4 Jul 2009 22:48:21 +0000 Subject: [PATCH] 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 --- src/as/com/threerings/util/Enum.as | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/as/com/threerings/util/Enum.as b/src/as/com/threerings/util/Enum.as index b416f7f6e..f27fe2bb8 100644 --- a/src/as/com/threerings/util/Enum.as +++ b/src/as/com/threerings/util/Enum.as @@ -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);