/src/com/ericfeminella/collections/CollectionIterator.as

http://transcriptstudio4isha.googlecode.com/ · ActionScript · 173 lines · 45 code · 14 blank · 114 comment · 2 complexity · f95c7529c78e2ecad3e26e407bd187ce MD5 · raw file

  1. /*
  2. Copyright (c) 2006 - 2008 Eric J. Feminella <eric@ericfeminella.com>
  3. All rights reserved.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is furnished
  9. to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  14. PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  15. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  16. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  17. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. @internal
  19. */
  20. package com.ericfeminella.collections
  21. {
  22. import mx.collections.CursorBookmark;
  23. import mx.collections.ICollectionView;
  24. import mx.collections.IViewCursor;
  25. /**
  26. *
  27. * Concrete <code>Iterator</code> implementation which provides an API
  28. * for iterating over an <code>ICollectionView</code>.
  29. *
  30. * @example The following is a basic <code>CollectionIterator</code>
  31. * example:
  32. *
  33. * <listing version="3.0">
  34. *
  35. * var collection:ICollectionView = new ArrayCollection(["a","b","c"]);
  36. * var it:Iterator = new CollectionIterator( collection );
  37. *
  38. * while ( it.hasNext() )
  39. * {
  40. * trace( it.next(), it.position() );
  41. * }
  42. *
  43. * // a, 0
  44. * // b, 1
  45. * // c, 2
  46. *
  47. * </listing>
  48. *
  49. * @see com.ericfeminella.collections.Iterator
  50. * @see mx.collections.ICollectionView
  51. * @see mx.collections.CursorBookmark
  52. * @see mx.collections.IViewCursor
  53. *
  54. */
  55. public class CollectionIterator implements Iterator
  56. {
  57. /**
  58. *
  59. * Defines the <code>ICollectionView</code> instance
  60. *
  61. * @see mx.collections.ICollectionView
  62. *
  63. */
  64. protected var collection:ICollectionView;
  65. /**
  66. *
  67. * Defines the <code>IViewCursor</code> instance to the aggregate
  68. *
  69. * @see mx.collections.IViewCursor
  70. *
  71. */
  72. protected var cursor:IViewCursor;
  73. /**
  74. *
  75. * Contains the current indexed position in the collection
  76. *
  77. */
  78. protected var index:int;
  79. /**
  80. *
  81. * Instantiates a new <code>CollectionIterator</code> instance
  82. *
  83. * @param the collection in which to iterate over
  84. *
  85. */
  86. public function CollectionIterator(collection:ICollectionView)
  87. {
  88. setCollection( collection );
  89. }
  90. /**
  91. *
  92. * Sets the collection in which to iterate over
  93. *
  94. * @param an ICollectionView instance
  95. *
  96. */
  97. public function setCollection(collection:ICollectionView) : void
  98. {
  99. this.collection = collection;
  100. cursor = collection.createCursor();
  101. }
  102. /**
  103. *
  104. * Determines if there are elements remaining in the ICollectionView
  105. *
  106. * @return true if an element remains, false if not
  107. *
  108. */
  109. public function hasNext() : Boolean
  110. {
  111. var result:Boolean = true;
  112. if ( cursor.beforeFirst || cursor.afterLast )
  113. {
  114. result= false;
  115. }
  116. return result;
  117. }
  118. /**
  119. *
  120. * Returns the next item in the ICollectionView
  121. *
  122. * @retrun an arbitrary object in the collection
  123. *
  124. */
  125. public function next() : *
  126. {
  127. var current:Object = cursor.current;
  128. index = cursor.bookmark.getViewIndex();
  129. cursor.moveNext();
  130. return current;
  131. }
  132. /**
  133. *
  134. * Resets the IViewCursor to a zero based indexed position
  135. *
  136. * @see mx.collections.CursorBookmark#FIRST
  137. *
  138. */
  139. public function reset() : void
  140. {
  141. cursor.seek( CursorBookmark.FIRST );
  142. }
  143. /**
  144. *
  145. * Determines the IViewCursor position of the ICollectionView
  146. *
  147. * @return the current position of the aggreagate
  148. *
  149. */
  150. public function position() : int
  151. {
  152. return index;
  153. }
  154. }
  155. }