/*
 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.

 @internal
 */

package com.ericfeminella.collections
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.utils.Dictionary;

    /**
     *
     * <code>BindableMap</code> provides a wrapper API which facilitates
     * data binding of an <code>IMap</code> implementation.
     *
     * <p>
     * An object which implements the <code>IMap</code> interface can
     * be specified as the source of data binding via an instance of
     * <code>BindableMap</code>.
     * </p>
     *
     * <p>
     * The typical implementation is to utilize <code>BindableMap</code>
     * to provide data binding capabilities to a <code>HashMap</code>.
     * </p>
     *
     * @example The following example demonstrates the typical client
     * implementation of <code>BindableMap</code>.
     *
     * <listing version="3.0">
     *
     * [Bindable]
     * private var _map:BindableMap = new BindableMap( new HashMap() );
     *
     * private function init() : void
     * {
     *     map.put( "a", "value A" );
     *     map.put( "b", "value B" );
     *     map.put( "c", "value C" );
     * }
     *
     * </listing>
     *
     * <p>
     * <code>BindableMap</code> can be used with any <code>IMap</code>
     * implementation, such as <code>LocalPersistanceMap</code> as well
     * as <code>ResourceMap</code>, so as to provide data bindable to
     * a <code>LocalSharedObject</code> or <code>ResourceBundle</code>,
     * respectively.
     * </p>
     *
     * @example The following example demonstrates the typical client
     * implementation of <code>BindableMap</code> utilized to provide
     * data binding capabilities to a <code>LocalSharedObject</code>
     * via a <code>LocalPersistanceMap</code> instance.
     *
     * <listing version="3.0">
     *
     * [Bindable]
     * private var _map:BindableMap = new BindableMap( new LocalPersistanceMap() );
     *
     * private function init() : void
     * {
     *     map.put( "a", "value A" );
     *     map.put( "b", "value B" );
     *     map.put( "c", "value C" );
     * }
     *
     * </listing>
     *
     * @see com.ericfeminella.collections.IMap
     * @see com.ericfeminella.collections.HashMap
     * @see com.ericfeminella.collections.LocalPersistanceMap
     * @see com.ericfeminella.collections.ResourceMap
     *
     *
     */
    public class BindableMap extends EventDispatcher implements IMap
    {
        /**
         *
         * Defines a reference to the underlying <code>IMap</code> instance
         * to which the <code>BindableMap</code> <code>keys</code> and
         * <code>values</code> are stored and bound.
         *
         */
        protected var map:IMap = null;

        /**
         *
         * <code>BindableMap</code> constructor requires an <code>IMap</code>
         * instance to which data binding capabilities are to be provided.
         *
         */
        public function BindableMap(map:IMap)
        {
            this.map = map;
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function containsKey(key:*) : Boolean
        {
            return map.containsKey( key );
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function containsValue(value:*) : Boolean
        {
            return map.containsValue( value );
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function getKey(value:*) : *
        {
            return map.getKey( value );
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function getKeys() : Array
        {
            return map.getKeys();
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function getValue(key:*) : *
        {
            var value:* = map.getValue( key );
            return value;
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function getValues() : Array
        {
            return map.getValues();
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function getEntries() : Array
        {
            return map.getEntries();
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function size() : int
        {
            return map.size();
        }

        [Bindable("mapChangeEvent")]
        /**
         *
         * @inheritDoc
         *
         */
        public function isEmpty() : Boolean
        {
            return map.isEmpty();
        }

        /**
         *
         * @inheritDoc
         *
         */
        public function put(key:*, value:*) : void
        {
            map.put( key, value );
            dispatchEvent( new Event( "mapChangeEvent" ) );
        }

        /**
         *
         * @inheritDoc
         *
         */
        public function putAll(table:Dictionary) : void
        {
            map.putAll( table );
            dispatchEvent( new Event( "mapChangeEvent" ) );
        }

        /**
         *
         * @inheritDoc
         *
         */
        public function putEntry(entry:IHashMapEntry) : void
        {
             map.putEntry( entry );
             dispatchEvent( new Event( "mapChangeEvent" ) );
        }

        /**
         *
         * @inheritDoc
         *
         */
        public function remove(key:*) : void
        {
            map.remove( key );
            dispatchEvent( new Event( "mapChangeEvent" ) );
        }

        /**
         *
         * @inheritDoc
         *
         */
        public function reset() : void
        {
            map.reset();
            dispatchEvent( new Event( "mapChangeEvent" ) );
        }

        /**
         *
         * @inheritDoc
         *
         */
        public function resetAllExcept(keyId:*) : void
        {
            map.resetAllExcept( keyId );
            dispatchEvent( new Event( "mapChangeEvent" ) );
        }

        /**
         *
         * @inheritDoc
         *
         */
        public function clear() : void
        {
            map.clear();
            dispatchEvent( new Event( "mapChangeEvent" ) );
        }

        /**
         *
         * @inheritDoc
         *
         */
        public function clearAllExcept(keyId:*) : void
        {
            map.clearAllExcept( keyId );
            dispatchEvent( new Event( "mapChangeEvent" ) );
        }
    }
}