/*-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is Fever Framework code.
The Initial Developer of the Original Code is Romain Ecarnot.
Portions created by Initial Developer are Copyright (C) 2006
the Initial Developer. All Rights Reserved.
Contributor(s):
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-----------------------------------------------------------------------------*/
import fever.events.EventPriority;
import fever.utils.ASType;
/**
* Array structure used in {@link fever.events.PriorityEventBroadcaster}
* class.
* Manages listeners priority value.
*
* @see fever.events.PriorityEventBroadcaster
*
* @author Romain Ecarnot
*/
class fever.events.PriorityListenerArray extends Array
{
//-------------------------------------------------------------------------
// Public API
//-------------------------------------------------------------------------
/**
* Constructor
*
*
Can use {@code Array} instanciation way to build * {@code * var a : new PriorityListenerArray( myListenerArray1, myListenerArray2, ...); * } */ public function PriorityListenerArray() { splice.apply( this, [0, 0].concat( arguments ) ); } /** * Returns index {@code Number} of passed-in {@code listener}. * * @param listener Listener to check. * * @return {@code Number} index of listener or {@code -1} if listener not exist. */ public function getIndex( listener ) : Number { if ( typeof( listener ) == "function") listener = listener.t; var l:Number = this.length; while ( --l > -1 ) { var o = this[l].listener; if ( o == listener ) { return l; } else if ( typeof( o ) == "function" ) { if (o.t == listener) return l; } } return -1; } /** * Indicates if passed-in {@code listener} is referenced in structure. * * @param listener Listener to check. * @return {@code true} is listener exist, either {@code false} */ public function listenerExists( listener ) : Boolean { return ( getIndex( listener ) != -1 ); } /** * Adds passed-in {@code listener} into structure. * *
Example * {@code * a.push( myListener, EventPriority.HIGHER ); * } * * @param listener Listener to add. * * @return {@code true} if listener has been successfully added, either {@code false} * (if already exist) */ public function insert( listener, priority : Number ) : Boolean { priority = ( priority != undefined && typeof( priority ) == ASType.NUMBER ) ? priority : EventPriority.DEFAULT; if ( !listenerExists( listener ) ) { push( { listener : listener, priority : priority } ); return true; } else return false; } /** * Removes passed-in {@code listener} from structure. * * @param listener Listener to remove. * @return {@code true} if listener has been found and removed, either {@code false} */ public function remove( listener ) : Boolean { var i:Number = getIndex( listener ); if (i != -1) { splice(i, 1); return true; } else return false; } /** * Indicates if strucutre is empty. * *
Example
*
* var b : Boolean = a.isEmpty( myListener );
*
*
* @return {@code true} if structure contains no listeners, either {@code false}
*/
public function isEmpty() : Boolean
{
return this.length < 1;
}
/**
* Returns the string representation of this instance.
*
*
Overrides {@code Array.toString} method. * * @return {@code String} representation of this instance */ public function toString() : String { return 'PriorityListenerArray'; } }