1  /**
     2   * com.sekati.core.KeyInjector
     3   * @version 1.0.1
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9  import com.sekati.crypt.RUID;
    10  
    11  /**
    12   * Give flash runtime objects a unique ID.
    13   * @see pixlib's HashCodeFactory
    14   * @see nectere's KeyInjector.
    15   */
    16  class com.sekati.core.KeyFactory {
    17  
    18  	private static var _key:String = "__RUID";
    19  
    20  	/**
    21  	 * Return the RUID key injected into an object - used as both getter and setter.
    22  	 * @param o (Object) Object to inject RUID key in to
    23  	 * @return (Number) Runtime Unique ID
    24  	 */
    25  	public static function inject(o:Object):Number {
    26  		if (!o[_key]) {
    27  			o[_key] = RUID.create( );
    28  			_global["ASSetPropFlags"]( o, [ _key ], 7, 1 );
    29  		}
    30  		return o[_key];	
    31  	}
    32  
    33  	/**
    34  	 * Injection wrapper to provide saner syntactical getter functionality.
    35  	 */
    36  	public static function getKey(o:Object):Number {
    37  		return KeyFactory.inject( o );	
    38  	}
    39  
    40  	/**
    41  	 * Returns next RUID as String to generate unique name
    42  	 * for an object.
    43  	 * @return String
    44  	 */
    45  	public static function getNextName():String {
    46  		return String( KeyFactory.previewNextKey( ) );
    47  	}
    48  
    49  	/**
    50  	 * Preview the next object RUID to be assigned.
    51  	 * @return Number
    52  	 */
    53  	public static function previewNextKey():Number {
    54  		return RUID.getCurrentId( ) + 1;
    55  	}
    56  
    57  	/**
    58  	 * Debugging method to check if two objects are equal.
    59  	 * @param a (Object)
    60  	 * @param b (Object)
    61  	 * @return Boolean
    62  	 */
    63  	public static function isSameObject(a:Object, b:Object):Boolean {
    64  		return KeyFactory.getKey( a ) == KeyFactory.getKey( b );
    65  	}
    66  
    67  	private function KeyFactory() {
    68  	}
    69  }