/*
 Copyright (c) 2006 Eric J. Feminella  <eric@ericfeminella.com> 
 All rights reserved.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy 
 of this software and associated documentation files (the "Software"), to deal 
 in the Software without restriction, including without limitation the rights 
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished 
 to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all 
 copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @ignore
 */

package com.ericfeminella.utils
{
    import flash.utils.describeType;
    
    /**
     * 
     * All static utility class which provides a mechanism for determining 
     * if a static constant has been defined by a specific Class object as
     * well as operations for inspecting constants which have been defined 
     * by a class object
     * 
     * @see http://livedocs.adobe.com/labs/flex/3/langref/statements.html#const
     * @see http://livedocs.adobe.com/labs/flex/3/langref/statements.html#static
     * 
     * 
     */    
    public final class StaticConstantDefinitionUtil
    {
        /**
         * 
         * Determines if a constant has been defined by the specified class. This method 
         * is useful if you need to enforce that the specified class defines the static 
         * constants from which a value has been set.
         * 
         * @example
         * <listing version="3.0">
         * 
         * // The following class defines three static constants which represents typical 
         * // version scheme, the definitions are verified via StaticConstantDefinitionUtil
         * 
         * package 
         * {
         *    public class Version
         *    {
         *        public static const MAJOR:int = 2;
         *        public static const MINOR:int = 1;
         *        public static const BUILD:int = 12;
         *    }
         * }
         * 
         * import com.ericfeminella.utils.StaticConstantDefinitionUtil;
         * 
         * trace( StaticConstantDefinitionUtil.isDefinedByClass("MAJOR", Version) ); //true
         * trace( StaticConstantDefinitionUtil.isDefinedByClass("MINOR", Version) ); //true
         * trace( StaticConstantDefinitionUtil.isDefinedByClass("BUILD", Version) ); //true
         * trace( StaticConstantDefinitionUtil.isDefinedByClass("REVISION", Version) ); //false
         * 
         * </listing>
         * 
         * @param  static constant which definition is to be determined
         * @param  Class object from which to determine definition 
         * @return true if the constant is defined by the specified class, otherwise false
         * 
         */        
        public static function isDefinedByClass(memberName:String, Type:Class) : Boolean
        {
            var constants:XMLList = describeType( Type ).constant.@name;
            return constants.contains( memberName );
        }
        
        /**
         * 
         * Retrieves all static constants which have been defined by the specified class
         * 
         * @example
         * <listing version="3.0">
         * 
         * // The following class defines three static constants which represent alpha
         * // charachters, the definitions are verified via StaticConstantDefinitionUtil
         * 
         * package 
         * {
         *    public class Version
         *    {
         *        public static const MAJOR:int = 2;
         *        public static const MINOR:int = 1;
         *        public static const BUILD:int = 12;
         *    }
         * }
         * 
         * import com.ericfeminella.utils.StaticConstantDefinitionUtil;
         * trace(StaticConstantDefinitionUtil.getDefinedConstantsByClass(Version));//MAJOR,MINOR,BUILD
         * 
         * </listing>
         * 
         * @param  Class object from which to determine constant definitions  
         * @return String Array containing the names of all static constants
         * 
         */        
        public static function getDefinedConstantsByClass(Type:Class) : Array
        {
            var constants:XMLList = describeType( Type ).constant.@name;
            var names:Array = new Array();
            
            var n:int = constants.length();
            
            for (var i:int = 0; i < n; i++) 
            {
                names.push( constants[i] );
            }
            
            return names;
        }
    }
}