Simpler deployment.
[redakcja.git] / redakcja / static / filebrowser / uploadify / com / adobe / utils / ArrayUtil.as
1 /*\r
2   Copyright (c) 2008, Adobe Systems Incorporated\r
3   All rights reserved.\r
4 \r
5   Redistribution and use in source and binary forms, with or without \r
6   modification, are permitted provided that the following conditions are\r
7   met:\r
8 \r
9   * Redistributions of source code must retain the above copyright notice, \r
10     this list of conditions and the following disclaimer.\r
11   \r
12   * Redistributions in binary form must reproduce the above copyright\r
13     notice, this list of conditions and the following disclaimer in the \r
14     documentation and/or other materials provided with the distribution.\r
15   \r
16   * Neither the name of Adobe Systems Incorporated nor the names of its \r
17     contributors may be used to endorse or promote products derived from \r
18     this software without specific prior written permission.\r
19 \r
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
21   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
22   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
23   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
24   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
25   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
26   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
27   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
28   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
29   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
30   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
31 */\r
32 \r
33 package com.adobe.utils\r
34 {\r
35         \r
36         /**\r
37         *       Class that contains static utility methods for manipulating and working\r
38         *       with Arrays.\r
39         * \r
40         *       Note that all APIs assume that they are working with well formed arrays.\r
41         *       i.e. they will only manipulate indexed values.  \r
42         * \r
43         *       @langversion ActionScript 3.0\r
44         *       @playerversion Flash 9.0\r
45         *       @tiptext\r
46         */              \r
47         public class ArrayUtil\r
48         {\r
49                                 \r
50                 /**\r
51                 *       Determines whether the specified array contains the specified value.    \r
52                 * \r
53                 *       @param arr The array that will be checked for the specified value.\r
54                 *\r
55                 *       @param value The object which will be searched for within the array\r
56                 * \r
57                 *       @return True if the array contains the value, False if it does not.\r
58                 *\r
59                 *       @langversion ActionScript 3.0\r
60                 *       @playerversion Flash 9.0\r
61                 *       @tiptext\r
62                 */                      \r
63                 public static function arrayContainsValue(arr:Array, value:Object):Boolean\r
64                 {\r
65                         return (arr.indexOf(value) != -1);\r
66                 }       \r
67                 \r
68                 /**\r
69                 *       Remove all instances of the specified value from the array,\r
70                 * \r
71                 *       @param arr The array from which the value will be removed\r
72                 *\r
73                 *       @param value The object that will be removed from the array.\r
74                 *\r
75                 *       @langversion ActionScript 3.0\r
76                 *       @playerversion Flash 9.0\r
77                 *       @tiptext\r
78                 */              \r
79                 public static function removeValueFromArray(arr:Array, value:Object):void\r
80                 {\r
81                         var len:uint = arr.length;\r
82                         \r
83                         for(var i:Number = len; i > -1; i--)\r
84                         {\r
85                                 if(arr[i] === value)\r
86                                 {\r
87                                         arr.splice(i, 1);\r
88                                 }\r
89                         }                                       \r
90                 }\r
91 \r
92                 /**\r
93                 *       Create a new array that only contains unique instances of objects\r
94                 *       in the specified array.\r
95                 *\r
96                 *       Basically, this can be used to remove duplication object instances\r
97                 *       from an array\r
98                 * \r
99                 *       @param arr The array which contains the values that will be used to\r
100                 *       create the new array that contains no duplicate values.\r
101                 *\r
102                 *       @return A new array which only contains unique items from the specified\r
103                 *       array.\r
104                 *\r
105                 *       @langversion ActionScript 3.0\r
106                 *       @playerversion Flash 9.0\r
107                 *       @tiptext\r
108                 */      \r
109                 public static function createUniqueCopy(a:Array):Array\r
110                 {\r
111                         var newArray:Array = new Array();\r
112                         \r
113                         var len:Number = a.length;\r
114                         var item:Object;\r
115                         \r
116                         for (var i:uint = 0; i < len; ++i)\r
117                         {\r
118                                 item = a[i];\r
119                                 \r
120                                 if(ArrayUtil.arrayContainsValue(newArray, item))\r
121                                 {\r
122                                         continue;\r
123                                 }\r
124                                 \r
125                                 newArray.push(item);\r
126                         }\r
127                         \r
128                         return newArray;\r
129                 }\r
130                 \r
131                 /**\r
132                 *       Creates a copy of the specified array.\r
133                 *\r
134                 *       Note that the array returned is a new array but the items within the\r
135                 *       array are not copies of the items in the original array (but rather \r
136                 *       references to the same items)\r
137                 * \r
138                 *       @param arr The array that will be copies\r
139                 *\r
140                 *       @return A new array which contains the same items as the array passed\r
141                 *       in.\r
142                 *\r
143                 *       @langversion ActionScript 3.0\r
144                 *       @playerversion Flash 9.0\r
145                 *       @tiptext\r
146                 */                      \r
147                 public static function copyArray(arr:Array):Array\r
148                 {       \r
149                         return arr.slice();\r
150                 }\r
151                 \r
152                 /**\r
153                 *       Compares two arrays and returns a boolean indicating whether the arrays\r
154                 *       contain the same values at the same indexes.\r
155                 * \r
156                 *       @param arr1 The first array that will be compared to the second.\r
157                 *\r
158                 *       @param arr2 The second array that will be compared to the first.\r
159                 *\r
160                 *       @return True if the arrays contains the same values at the same indexes.\r
161                         False if they do not.\r
162                 *\r
163                 *       @langversion ActionScript 3.0\r
164                 *       @playerversion Flash 9.0\r
165                 *       @tiptext\r
166                 */              \r
167                 public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean\r
168                 {\r
169                         if(arr1.length != arr2.length)\r
170                         {\r
171                                 return false;\r
172                         }\r
173                         \r
174                         var len:Number = arr1.length;\r
175                         \r
176                         for(var i:Number = 0; i < len; i++)\r
177                         {\r
178                                 if(arr1[i] !== arr2[i])\r
179                                 {\r
180                                         return false;\r
181                                 }\r
182                         }\r
183                         \r
184                         return true;\r
185                 }\r
186         }\r
187 }\r