/*
 Copyright (c) 2006 - 2009  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.

 @internal
 */

package com.ericfeminella.introspection
{
    import com.ericfeminella.utils.AbstractStaticType;
    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, and determining if a specific value has been defined
     * by a Class constant.
     * 
     * @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 StaticDefinitionUtils extends AbstractStaticType
    {
        /**
         * 
         * All static class which determines if a static member has been defined 
         * by the specified class.
         * 
         * @example The following class defines four static members, that is, two 
         * static variables, one static constant and one static method the definitions 
         * are verified via StaticDefinitionUtils
         * 
         * <listing version="3.0">
         * 
         * package 
         * {
         *    public class Version
         *    {
         *        public static const MAJOR:int = 2;
         *        public static var MINOR:int = 1;
         *        public static var BUILD:int = 12;
         *        
         *        public static function getVersion () : String
         *        {
         *            // code for formating build...
         *        }
         *    }
         * }
         * 
         * trace( StaticDefinitionUtils.isClassMember( "MAJOR", Version ) ); //true
         * trace( StaticDefinitionUtils.isClassMember( "MINOR", Version ) ); //true
         * trace( StaticDefinitionUtils.isClassMember( "BUILD", Version ) ); //true
         * trace( StaticDefinitionUtils.isClassMember( "getVersion", Version ) ); //true
         * 
         * </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 isMember(memberName:String, Type:Class) : Boolean
        {
            return Object( Type ).hasOwnProperty( memberName );
        }
        
        /**
         * 
         * Determine if a specific value has been assigned to a constant defined with the 
         * specified Class object
         * 
         * @example
         * <listing version="3.0">
         * 
         * // The following class defines three static constants which represent build 
         * // constants, the values are verified via StaticDefinitionUtils
         * 
         * package 
         * {
         *    public class Version
         *    {
         *        public static const MAJOR:int = 2;
         *        public static const MINOR:int = 1;
         *        public static const BUILD:int = 12;
         *    }
         * }
         * 
         * trace( StaticDefinitionUtils.isValueAssignedToMember( 1, Version ) ); // true
         * trace( StaticDefinitionUtils.isValueAssignedToMember( 2, Version ) ); // true
         * trace( StaticDefinitionUtils.isValueAssignedToMember( 12, Version ) ); // true
         * 
         * </listing>
         * 
         * @param   value in which to determine if a class constant is assigned
         * @param   Type from which to determine class constant values
         * @return  true if the value is defined, otherwise false
         * 
         */        
        public static function isValueAssignedToConstant(value:*, Type:Class) : Boolean
        {
            var constants:XMLList = describeType( Type ).constant.@name;
            var isAssigned:Boolean = false;
            
            var n:int = constants.length();
            
            for (var i:int = 0; i < n; i++) 
            {
                if ( Type[ constants[i] ] == value )
                {
                    isAssigned = true;
                    break;
                }
            }
            return isAssigned;
        }
        
        /**
         * 
         * Determine if a specific value has been assigned to a constant defined with the 
         * specified Class object
         * 
         * @example
         * <listing version="3.0">
         * 
         * // The following class defines three static constants which represent build 
         * // constants, the values are verified via StaticDefinitionUtils
         * 
         * package 
         * {
         *    public class Version
         *    {
         *        public static const MAJOR:int = 2;
         *        public static const MINOR:int = 1;
         *        public static var BUILD:int = 12;
         *    }
         * }
         * 
         * trace( StaticDefinitionUtils.isValueAssignedToMember( 1, Version ) ); // true
         * trace( StaticDefinitionUtils.isValueAssignedToMember( 2, Version ) ); // true
         * trace( StaticDefinitionUtils.isValueAssignedToMember( 12, Version ) ); // true
         * 
         * </listing>
         * 
         * @param   value in which to determine if a class constant is assigned
         * @param   Type from which to determine class constant values
         * @return  true if the value is defined, otherwise false
         * 
         */        
        public static function isValueAssignedToVariable(value:*, Type:Class) : Boolean
        {
            var variables:XMLList = describeType( Type ).variable.@name;
            var isAssigned:Boolean = false;
            
            var n:int = variables.length();
            
            for (var i:int = 0; i < n; i++) 
            {
                if ( Type[ variables[i] ] == value )
                {
                    isAssigned = true;
                    break;
                }
            }
            return isAssigned;
        }
    }
}