PageRenderTime 25ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mx/collections/ISort.as

http://github.com/reflex/reflex-framework
ActionScript | 325 lines | 21 code | 10 blank | 294 comment | 0 complexity | 7d5ea2004a1dff02c03df5f5ddde9b4f MD5 | raw file
Possible License(s): Unlicense
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // ADOBE SYSTEMS INCORPORATED
  4. // Copyright 2005-2010 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. package mx.collections
  12. {
  13. /**
  14. * The <code>ISort</code> interface defines the interface for classes that
  15. * provide the sorting information required to sort the
  16. * data of a collection view.
  17. *
  18. * @see mx.collections.ICollectionView
  19. * @see mx.collections.ISortField
  20. *
  21. * @langversion 3.0
  22. * @playerversion Flash 9
  23. * @playerversion AIR 1.1
  24. * @productversion Flex 4.5
  25. */
  26. public interface ISort
  27. {
  28. //--------------------------------------------------------------------------
  29. //
  30. // Properties
  31. //
  32. //--------------------------------------------------------------------------
  33. /**
  34. * The method used to compare items when sorting.
  35. * If you specify this property, Flex ignores any
  36. * <code>compareFunction</code> properties that you specify in the
  37. * <code>ISortField</code> objects that you use in this class.
  38. *
  39. * <p>The compare function must have the following signature:</p>
  40. *
  41. * <pre><code>
  42. *
  43. * function [name](a:Object, b:Object, fields:Array = null):int
  44. *
  45. * </code></pre>
  46. *
  47. * <p>This function must return the following value:
  48. * <ul>
  49. * <li>-1, if the <code>Object a</code> should appear before the
  50. * <code>Object b</code> in the sorted sequence</li>
  51. * <li>0, if the <code>Object a</code> equals the
  52. * <code>Object b</code></li>
  53. * <li>1, if the <code>Object a</code> should appear after the
  54. * <code>Object b</code> in the sorted sequence</li>
  55. * </ul></p>
  56. * <p>To return to the internal comparision function, set this value to
  57. * <code>null</code>.</p>
  58. * <p>
  59. * The <code>fields</code> array specifies the object fields
  60. * to compare.
  61. * Typically the algorithm will compare properties until the field list is
  62. * exhausted or a non-zero value can be returned.
  63. * For example:</p>
  64. *
  65. * <pre><code>
  66. * function myCompare(a:Object, b:Object, fields:Array = null):int
  67. * {
  68. * var result:int = 0;
  69. * var i:int = 0;
  70. * var propList:Array = fields ? fields : internalPropList;
  71. * var len:int = propList.length;
  72. * var propName:String;
  73. * while (result == 0 &amp;&amp; (i &lt; len))
  74. * {
  75. * propName = propList[i];
  76. * result = compareValues(a[propName], b[propName]);
  77. * i++;
  78. * }
  79. * return result;
  80. * }
  81. *
  82. * function compareValues(a:Object, b:Object):int
  83. * {
  84. * if (a == null &amp;&amp; b == null)
  85. * return 0;
  86. *
  87. * if (a == null)
  88. * return 1;
  89. *
  90. * if (b == null)
  91. * return -1;
  92. *
  93. * if (a &lt; b)
  94. * return -1;
  95. *
  96. * if (a &gt; b)
  97. * return 1;
  98. *
  99. * return 0;
  100. * }
  101. * </code></pre>
  102. *
  103. * <p>The default value is an internal compare function that can perform
  104. * a string, numeric, or date comparison in ascending or descending order.
  105. * Specify your own function only if you need a need a custom
  106. * comparison algorithm. This is normally only the case if a calculated
  107. * field is used in a display.</p>
  108. *
  109. * <p>Alternatively you can specify separate compare functions for each
  110. * sort field by using the <code>ISortField</code> class
  111. * <code>compareFunction</code> property; This way you can use the default
  112. * comparison for some fields and a custom comparison for others.</p>
  113. *
  114. * @langversion 3.0
  115. * @playerversion Flash 9
  116. * @playerversion AIR 1.1
  117. * @productversion Flex 4.5
  118. */
  119. function get compareFunction():Function;
  120. function set compareFunction(value:Function):void;
  121. /**
  122. * An <code>Array</code> of <code>ISortField</code> objects that specifies the fields to compare.
  123. * The order of the ISortField objects in the array determines
  124. * field priority order when sorting.
  125. * The default sort comparator checks the sort fields in array
  126. * order until it determinines a sort order for the two
  127. * fields being compared.
  128. *
  129. * @default null
  130. *
  131. * @see ISortField
  132. *
  133. * @langversion 3.0
  134. * @playerversion Flash 9
  135. * @playerversion AIR 1.1
  136. * @productversion Flex 4.5
  137. */
  138. function get fields():Array;
  139. function set fields(value:Array):void;
  140. /**
  141. * Indicates if the sort should be unique.
  142. * Unique sorts fail if any value or combined value specified by the
  143. * fields listed in the fields property result in an indeterminate or
  144. * non-unique sort order; that is, if two or more items have identical
  145. * sort field values. An error is thrown if the sort is not unique.
  146. * The sorting logic uses this <code>unique</code> property value only if sort
  147. * field(s) are specified explicitly. If no sort fields are specified
  148. * explicitly, no error is thrown even when there are identical value
  149. * elements.
  150. *
  151. * @default false
  152. *
  153. * @langversion 3.0
  154. * @playerversion Flash 9
  155. * @playerversion AIR 1.1
  156. * @productversion Flex 4.5
  157. */
  158. function get unique():Boolean;
  159. function set unique(value:Boolean):void;
  160. //--------------------------------------------------------------------------
  161. //
  162. // Methods
  163. //
  164. //--------------------------------------------------------------------------
  165. /**
  166. * Finds the specified object within the specified array (or the insertion
  167. * point if asked for), returning the index if found or -1 if not.
  168. * The <code>ListCollectionView</code> class <code>find<i>xxx</i>()</code>
  169. * methods use this method to find the requested item; as a general rule,
  170. * it is easier to use these functions, and not <code>findItem()</code>
  171. * to find data in <code>ListCollectionView</code>-based objects.
  172. * You call the <code>findItem()</code> method directly when writing a
  173. * class that supports sorting, such as a new <code>ICollectionView</code>
  174. * implementation.
  175. * The input items array need to be sorted before calling this function.
  176. * Otherwise this function will not be able to find the specified value
  177. * properly.
  178. *
  179. * @param items the Array within which to search.
  180. * @param values Object containing the properties to look for (or
  181. * the object to search for, itself).
  182. * The object must consist of field name/value pairs, where
  183. * the field names are names of fields specified by the
  184. * <code>fields</code> property, in the same order they
  185. * are used in that property.
  186. * You do not have to specify all of the fields from the
  187. * <code>fields</code> property, but you
  188. * cannot skip any in the order.
  189. * Therefore, if the <code>fields</code>
  190. * properity lists three fields, you can specify its first
  191. * and second fields in this parameter, but you cannot
  192. * specify only the first and third fields.
  193. * @param mode String containing the type of find to perform.
  194. * Valid values are:
  195. * <table>
  196. * <tr>
  197. * <th>ANY_INDEX_MODE</th>
  198. * <th>Return any position that
  199. * is valid for the values.</th>
  200. * </tr>
  201. * <tr>
  202. * <th>FIRST_INDEX_MODE</th>
  203. * <th>Return the position
  204. * where the first occurrance of the values is found.</th>
  205. * </tr>
  206. * <tr>
  207. * <th>LAST_INDEX_MODE</th>
  208. * <th>Return the position where the
  209. * last ocurrance of the specified values is found.
  210. * </th>
  211. * </tr>
  212. * </table>
  213. * @param returnInsertionIndex If the method does not find an item
  214. * identified by the <code>values</code> parameter,
  215. * and this parameter is <code>true</code> the
  216. * <code>findItem()</code>
  217. * method returns the insertion point for the values,
  218. * that is the point in the sorted order where you
  219. * should insert the item.
  220. * @param compareFunction a comparator function to use to find the item.
  221. * If you do not specify this parameter or , or if you
  222. * provide a <code>null</code> value,
  223. * <code>findItem()</code> function uses the
  224. * compare function determined by the <code>ISort</code>
  225. * instance's <code>compareFunction</code> property,
  226. * passing in the array of fields determined
  227. * by the values object and the current
  228. * <code>SortFields</code>.
  229. *
  230. * If you provide a non-null value, <code>findItem()</code>
  231. * function uses it as the compare function.
  232. *
  233. * The signature of the function passed as
  234. * <code>compareFunction</code> must be as follows:
  235. * <code>function myCompareFunction(a:Object, b:Object):int</code>.
  236. * Note that there is no third argument unlike the
  237. * compare function for <code>ISort.compareFunction()</code>
  238. * property.
  239. * @return int The index in the array of the found item.
  240. * If the <code>returnInsertionIndex</code> parameter is
  241. * <code>false</code> and the item is not found, returns -1.
  242. * If the <code>returnInsertionIndex</code> parameter is
  243. * <code>true</code> and the item is not found, returns
  244. * the index of the point in the sorted array where the
  245. * values would be inserted.
  246. *
  247. * @throws SortError If there are any parameter errors,
  248. * the find critieria is not compatible with the sort
  249. * or the comparator function for the sort can not be determined.
  250. *
  251. * @langversion 3.0
  252. * @playerversion Flash 9
  253. * @playerversion AIR 1.1
  254. * @productversion Flex 4.5
  255. */
  256. function findItem(
  257. items:Array,
  258. values:Object,
  259. mode:String,
  260. returnInsertionIndex:Boolean = false,
  261. compareFunction:Function = null):int;
  262. /**
  263. * Return whether the specified property is used to control the sort.
  264. * The function cannot determine a definitive answer if the sort uses a
  265. * custom comparator; it always returns <code>true</code> in this case.
  266. *
  267. * @param property The name of the field that to test.
  268. * @return Whether the property value might affect the sort outcome.
  269. * If the sort uses the default compareFunction, returns
  270. * <code>true</code> if the
  271. * <code>property</code> parameter specifies a sort field.
  272. * If the sort or any <code>ISortField</code> uses a custom comparator,
  273. * there's no way to know, so return <code>true</code>.
  274. *
  275. * @langversion 3.0
  276. * @playerversion Flash 9
  277. * @playerversion AIR 1.1
  278. * @productversion Flex 4.5
  279. */
  280. function propertyAffectsSort(property:String):Boolean;
  281. /**
  282. * Goes through the <code>fields</code> array and calls
  283. * <code>reverse()</code> on each of the <code>ISortField</code> objects in
  284. * the array. If the field was descending now it is ascending,
  285. * and vice versa.
  286. *
  287. * <p>Note: an <code>ICollectionView</code> does not automatically
  288. * update when the objects in the <code>fields</code> array are modified;
  289. * call its <code>refresh()</code> method to update the view.</p>
  290. *
  291. * @langversion 3.0
  292. * @playerversion Flash 9
  293. * @playerversion AIR 1.1
  294. * @productversion Flex 4.5
  295. */
  296. function reverse():void;
  297. /**
  298. * Apply the current sort to the specified array (not a copy).
  299. * To prevent the array from being modified, create a copy
  300. * use the copy in the <code>items</code> parameter.
  301. *
  302. * <p>Flex <code>ICollectionView</code> implementations call the
  303. * <code>sort</code> method automatically and ensure that the sort is
  304. * performed on a copy of the underlying data.</p>
  305. *
  306. * @param items Array of items to sort.
  307. *
  308. * @langversion 3.0
  309. * @playerversion Flash 9
  310. * @playerversion AIR 1.1
  311. * @productversion Flex 4.5
  312. */
  313. function sort(items:Array):void;
  314. }
  315. }