/*
 Copyright (c) 2006 - 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.utils
{
    import flash.utils.Dictionary;

    /**
     *
     * Pseudo abstract class which enforces derived classes only
     * ever have a single instance instantiated per application,
     * by monitoring the instantiation of derived classes
     *
     * <p>
     * Upon instantiation of a specific derived class the super classes
     * constructor is invoked, and <code>SingletonMonitor</code> parses
     * the current stacktrace to determine the name of the derived class.
     * The derived class name is then used as the unique key in the
     * HashMap instance (<code>singletons</code>). The class name (key)
     * is then assigned a Boolean value of true. When a subsequent
     * instantiation of the derived class occurs the Singleton class
     * constructor checks to see if the derived class has been mapped
     * previously, and if so, an exception is thrown.
     * </p>
     *
     * <p>
     * Derived classes need not explicitly invoke the super classes
     * constructor (<code>SingletonMonitor</code>) as it is implied
     * </p>
     *
     * @example The following example demonstrates how a class can
     * extend <code>SingletonMonitor</code> in order to simplify
     * singleton implementations by abstracting the singleton
     * construction process from the actual implementation code.
     *
     * <listing version="3.0">
     *
     * package
     * {
     *     public class SomeManager extends SingletonMonitor
     *     {
     *         // Defines the singleton instance of SomeManager
     *         public static const instance:SomeManager = new SomeManager();
     *     }
     * }
     *
     * </listing>
     *
     *
     * @example The above example guarantees that only one instance
     * of the example class "Singleton" can ever be instantiated.
     *
     */
    public class SingletonMonitor
    {
        /**
         *
         * Defines a static HashMap which contains a reference to all
         * Singleton class types.
         *
         * <p>
         * A unique key based on the derived class name is added to the
         * map for type which is to only contain a Singleton instance
         * </p>
         *
         */
        private static const instances:Dictionary = new Dictionary();

        /**
         *
         * Instantiates a new <code>SingletonMonitor</code> instance
         * and determines if the derived class has been previously
         * instantiated, if so an Exception is thrown, thus indicating
         * that the class is a Singleton and is to only ever have a
         * single instance instantiated
         *
         */
        public function SingletonMonitor()
        {
            var singletonClass:String = getClassName();

            if ( instances[singletonClass] )
            {
                 throw Error("Singleton Exception. Only one instance is allowed for type: " + singletonClass);
            }
            instances[singletonClass] = true;
        }

        /**
         *
         * @private
         *
         * Determines the class name of the derived class which has
         * been instantiated
         *
         * @return the name of the class which has been instantiated
         * @see    Error#getStackTrace()
         *
         */
        private static function getClassName() : String
        {
            var stackTrace:String = new Error().getStackTrace();
            var stackFrame:String = stackTrace.split("at ")[3];
            var className:String  = stackFrame.split("()")[0];

            return className;
        }
    }
}