1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  
     5  import org.aswing.utils.*;
     6  
     7  /**
     8   * @author firdosh
     9   */
    10  class org.aswing.utils.Vector implements IVector{
    11  	
    12  	private var _elements:Array;
    13  	public static var CASEINSENSITIVE:Number=1;
    14  	public static var DESCENDING :Number=2;
    15  	public static var UNIQUESORT :Number=4;
    16  	public static var RETURNINDEXEDARRAY :Number=8;	
    17  	public static var NUMERIC  :Number=16;	
    18  	
    19  	public function Vector(){
    20  		_elements=new Array();	
    21  	}
    22  	
    23  	/**
    24  	 * Call the operation by pass each element once.
    25  	 * <p>
    26  	 * for example:
    27  	 * <pre>
    28  	 * //hide all component in vector components
    29  	 * components.each( 
    30  	 *     function(c:Component){
    31  	 *         c.setVisible(false);
    32  	 *     });
    33  	 * <pre>
    34  	 * @param operation the operation function for each element
    35  	 */
    36  	public function each(operation:Function):Void{
    37  		for(var i:Number=0; i<_elements.length; i++){
    38  			operation(_elements[i]);
    39  		}
    40  	}
    41  	
    42  	/**
    43  	 * Call the operation by pass each element once without the specified element.
    44  	 * <p>
    45  	 * for example:
    46  	 * <pre>
    47  	 * //hide all component in vector components without firstOne component
    48  	 * var firstOne:Component = the first one;
    49  	 * components.eachWithout( 
    50  	 * 	   firstOne,
    51  	 *     function(c:Component){
    52  	 *         c.setVisible(false);
    53  	 *     });
    54  	 * <pre>
    55  	 * @param obj the which will not be operated.
    56  	 * @param operation the operation function for each element
    57  	 */
    58  	public function eachWithout(obj:Object, operation:Function):Void{
    59  		for(var i:Number=0; i<_elements.length; i++){
    60  			if(_elements[i] != obj){
    61  				operation(_elements[i]);
    62  			}
    63  		}
    64  	}	
    65  	
    66  	public function get(i:Number):Object{
    67  		return _elements[i];
    68  	}
    69  	
    70  	public function elementAt(i:Number):Object{
    71  		return get(i);
    72  	}
    73  	
    74  	public function append(obj:Object, index:Number):Void{
    75  		if(index == undefined){			
    76  			_elements.push(obj);
    77  		}else{
    78  			_elements.splice(index, 0, obj);
    79  		}
    80  	}
    81  	public function appendAll(arr:Array, index:Number):Void{
    82  		if(index == undefined || index == 0){			
    83  			_elements = _elements.concat(arr);
    84  		}else{
    85  			var right:Array = _elements.splice(index);
    86  			_elements = _elements.concat(arr);
    87  			_elements = _elements.concat(right);
    88  		}
    89  	}
    90  	
    91  	public function replaceAt(index:Number, obj:Object):Object{
    92  		if(index<0 || index>= size()){
    93  			return null;
    94  		}
    95  		else{		
    96  			var oldObj:Object = _elements[index];
    97  			_elements[index] = obj;		
    98  			return oldObj;
    99  		}
   100  	}
   101  	
   102  	public function removeAt(index:Number):Object{
   103  		if(index<0 || index>= size()){
   104  			return null;
   105  		}
   106  		else{		
   107  			var obj:Object = _elements[index];
   108  			_elements.splice(index, 1);		
   109  			return obj;
   110  		}
   111  	}
   112  	public function remove(obj:Object):Object{
   113  		var i:Number = indexOf(obj);
   114  			if(i>=0){
   115  				return removeAt(i);
   116  			}else{
   117  				return null;
   118  			}
   119  	}
   120  	
   121  	/**
   122  	 * Removes from this List all of the elements whose index is between fromIndex, 
   123  	 * inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). 
   124  	 * This call shortens the ArrayList by (toIndex - fromIndex) elements. (If toIndex==fromIndex, 
   125  	 * this operation has no effect.) 
   126  	 * @return the elements were removed from the vector
   127  	 */
   128  	public function removeRange(fromIndex:Number, toIndex:Number):Array{
   129  		return _elements.splice(fromIndex, toIndex-fromIndex+1);
   130  	}
   131  	public function indexOf(obj:Object):Number{
   132  		for(var i:Number = 0; i<_elements.length; i++){
   133  			if(_elements[i] == obj){
   134  				return i;
   135  			}
   136  		}
   137  		return -1;
   138  	}
   139  	public function contains(obj:Object):Boolean{
   140  		return indexOf(obj) >=0;
   141  	}
   142  	public function first():Object{
   143  		return _elements[0];
   144  	}
   145  	public function last():Object{
   146  		return _elements[_elements.length-1];
   147  	}
   148  	
   149  	
   150  	public function size():Number{
   151  		return _elements.length;
   152  	}
   153  	public function setElementAt(index:Number,element:Object):Void{
   154  		replaceAt(index,element);
   155  	}
   156  	public function getSize():Number{
   157  		return size();
   158  	}
   159  	public function clear():Void{
   160  		_elements.splice(0);
   161  		_elements=new Array();
   162  		
   163  	}
   164  	public function sort(compare:Object, options:Number):Array{
   165  		return _elements.sort(compare, options);
   166  	}
   167  	public function sortOn(key:Object, options:Number):Array{
   168  		return _elements.sortOn(key, options);
   169  	}
   170  	public function toString(Void):String{
   171  		return "Vector : " + _elements.toString();
   172  	}
   173  	
   174  	public function clone(Void):Vector{
   175  		var cloned:Vector=new Vector();
   176  		for (var i:Number=0; i<_elements.length; i++){
   177  			cloned.append(_elements[i]);
   178  		}		
   179  		return cloned;
   180  	}
   181  	
   182  	public function isEmpty():Boolean{
   183  		if(_elements.length>0)
   184  			return false;
   185  		else
   186  			return true;
   187  	}
   188  	
   189  	public function toArray():Array{
   190  		var arr:Array = new Array();
   191  		for(var i:Number = 0; i<_elements.length; i++){
   192  			arr.push(_elements[i]);
   193  		}
   194  		return arr;
   195  	}
   196  	
   197  	
   198  	
   199  		
   200  }
   201