1//////////////////////////////////////////////////////////////////////////////// 2// 3// ADOBE SYSTEMS INCORPORATED 4// Copyright 2004-2006 Adobe Systems Incorporated 5// All Rights Reserved. 6// 7// NOTICE: Adobe permits you to use, modify, and distribute this file 8// in accordance with the terms of the license agreement accompanying it. 9// 10//////////////////////////////////////////////////////////////////////////////// 11 12package mx.utils 13{ 14 15/** 16 * The ArrayUtil utility class is an all-static class 17 * with methods for working with arrays within Flex. 18 * You do not create instances of ArrayUtil; 19 * instead you call static methods such as the 20 * <code>ArrayUtil.toArray()</code> method. 21 */ 22public class ArrayUtil 23{ 24 include "../core/Version.as"; 25 26 //-------------------------------------------------------------------------- 27 // 28 // Class methods 29 // 30 //-------------------------------------------------------------------------- 31 32 /** 33 * Ensures that an Object can be used as an Array. 34 * 35 * <p>If the Object is already an Array, it returns the object. 36 * If the object is not an Array, it returns an Array 37 * in which the only element is the Object. 38 * As a special case, if the Object is null, 39 * it returns an empty Array.</p> 40 * 41 * @param obj Object that you want to ensure is an array. 42 * 43 * @return An Array. If the original Object is already an Array, 44 * the original Array is returned. Otherwise, a new Array whose 45 * only element is the Object is returned or an empty Array if 46 * the Object was null. 47 */ 48 public static function toArray(obj:Object):Array 49 { 50 if (!obj) 51 return []; 52 53 else if (obj is Array) 54 return obj as Array; 55 56 else 57 return [ obj ]; 58 } 59 60 /** 61 * Returns the index of the item in the Array. 62 * 63 * Note that in this implementation the search is linear and is therefore 64 * O(n). 65 * 66 * @param item The item to find in the Array. 67 * 68 * @param source The Array to search for the item. 69 * 70 * @return The index of the item, and -1 if the item is not in the list. 71 */ 72 public static function getItemIndex(item:Object, source:Array):int 73 { 74 var n:int = source.length; 75 for (var i:int = 0; i < n; i++) 76 { 77 if (source[i] === item) 78 return i; 79 } 80 81 return -1; 82 } 83} 84 85} 86