package com.ericfeminella.utils
{
import com.ericfeminella.exceptions.StaticClassInstantiationException;
import flash.utils.getDefinitionByName;
/**
*
* Provides a mechanism which enforces a class of static type is
* never instantiated.
*
* <p>
* All static classes (classes which ONLY contain static members)
* are to extend <code>AbstractStaticType</code> in order to ensure
* instances can never be instantiated.
* </p>
*
* <p>
* By extending <code>AbstractStaticType</code> it is implied that a
* class instance can not be instantiated.
* </p>
*
* <p>
* Typically Helper, Utility and Enumeration classes will extend
* AbstractStaticType to ensure clients do not misuse an API by
* attempting to instantiate an instance of the static type
* </p>
*
* @example
*
* <listing version="3.0">
*
* package com.ericfeminella.utils
* {
* public final class CalculationUtils extends AbstractStaticType
* {
* public static function calculateX() : String {
* // implementation...
* }
* public static function calculateY() : String {
* // implementation...
* }
* }
* }
*
* // Attempt to instantiate instance of CalculationUtils
*
* var util:CalculationUtils = new CalculationUtils();
*
* // results in the following exception being throw
* // Illegal instantiation of static type 'com.ericfeminella.utils.CalculationUtils'
*
* </listing>
*
*/
public class AbstractStaticType
{
/**
*
* <code>AbstractStaticType</code> constructor determines the sub
* class which was instantiated and throws an error specifying the
* static class which was instantiated.
*
*/
public function AbstractStaticType()
{
var ClassObject:Class = null;
var exception:Error = new Error();
if ( exception.getStackTrace() != null )
{
var stackFrame:String = exception.getStackTrace().split("at ")[2];
var fullyQualifiedName:String = stackFrame.split("()")[0];
ClassObject = getDefinitionByName( fullyQualifiedName ) as Class;
}
throw new StaticClassInstantiationException( ClassObject );
}
}
}