/*
 Copyright (c) 2007 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.resources
{
    import mx.resources.IResourceBundle;

    /**
     *
     * All static class which serves as a utility for performing
     * detailed inspection on a ResourceBundle
     *
     * <p>
     * The following example demonstrates a typical use-case which
     * utilizes ResourceInspector to determine specific detailed
     * information defined in a .properties file
     * </p>
     *
     * @example
     * <listing version="3.0">
     *
     * //'resources.properties' content:
     * //lang=English
     * //version=1.5
     *
     * import com.ericfeminella.resources.ResourceInspector;
     * import mx.resources.ResourceBundle;
     *
     * [ResourceBundle("resource")]
     * private static const rb:ResourceBundle;
     *
     * private function init() : void
     * {
     *        trace( "keys: " + ResourceInspector.getKeys( rb ) );
     *        trace( "values: " + ResourceInspector.getValues( rb ) );
     *        trace( "has key 'lang': " + ResourceInspector.hasKey( rb, 'lang' ) );
     *        trace( "has value 1.5: " + ResourceInspector.hasValue( rb, 1.5 ) );
     *        trace( "Get key by value: " + ResourceInspector.getKeyByValue( rb, 1.5 ) );
     *        trace( "length: " + ResourceInspector.length( rb ) );
     * }
     *
     * //outputs the following:
     * //keys: lang,version
     * //values: English,1.5
     * //has key 'lang': true
     * //has value 1.5: true
     * //Get key by value: version
     * //length: 2
     *
     * </listing>
     *
     * @see mx.resources.IResourceBundle#content
     *
     *
     */
    public final class ResourceInspector
    {
        /**
         *
         * Retrieves all keys which have been defined in the
         * specified ResourceBundle
         *
         * @param  ResourceBundle from which to retrieve keys
         * @return String Array containing all keys
         *
         */
        public static function getKeys(rb:IResourceBundle) : Array
        {
            var content:Object = rb.content;
            var key:String;
            var keys:Array = [];

            for (key in content)
            {
                keys.push( key );
            }

            return keys;
        }

        /**
         *
         * Retrieves all values which have been defined in the
         * specified ResourceBundle for each key
         *
         * @param  ResourceBundle from which to retrieve values
         * @return String Array containing all keys
         *
         */
        public static function getValues(rb:IResourceBundle) : Array
        {
            var content:Object = rb.content;
            var key:String;
            var values:Array = [];

            for (key in content)
            {
                values.push( content[key] );
            }
            return values;
        }

        /**
         *
         * Determines if the specified key defined in the ResourceBundle
         *
         * @param  ResourceBundle from which to determine key definition
         * @param  key in which to search for
         * @return true if specified key is defined, otherwise false
         *
         */
        public static function hasKey(rb:IResourceBundle, key:String) : Boolean
        {
            var result:Boolean = false;

            var prop:String;
            var content:Object = rb.content;

            for (prop in content)
            {
                if (prop == key)
                {
                    result = true;
                }
            }
            return result;
        }

        /**
         *
         * Determines if the specified value is assigned to a key
         * in the ResourceBundle
         *
         * @param  ResourceBundle from which to determine value
         * @param  value in which to search for
         * @return true if specified value is defined, otherwise false
         *
         */
        public static function hasValue(rb:IResourceBundle, value:*) : Boolean
        {
            var result:Boolean = false;

            var content:Object = rb.content;
            var key:String;

            for (key in content)
            {
                if (content[key] == value)
                {
                    result = true;
                }
            }
            return result;
        }

        /**
         *
         * Determines the key from which the specified value has been
         * assigned
         *
         * @param  ResourceBundle from which to determine key
         * @return the key which has been assigned the specified value
         *
         */
        public static function getKeyByValue(rb:IResourceBundle, value:*) : String
        {
            var content:Object = rb.content;
            var prop:String;

            var key:String = null;

            for (prop in content)
            {
                if (content[prop] == value)
                {
                    key = prop;
                }
            }
            return key;
        }

        /**
         *
         * Determines the amount of keys defined in the specified
         * ResourceBundle
         *
         * @param  ResourceBundle from which to determine key length
         * @return key length for the specified ResourceBundle
         * @see    ResourceInspector#getKeys
         *
         */
        public static function length(rb:IResourceBundle) : int
        {
            return getKeys( rb ).length;
        }
    }
}