Packageorg.as3collections.iterators
Classpublic class ListMapIterator
InheritanceListMapIterator Inheritance Object
Implements IListMapIterator

An iterator to iterate over implementations of IListMap interface. ListMapIterator allows to traverse the map in either direction.

IMPORTANT:

A ListMapIterator has no current mapping; its cursor position always lies between the mapping that would be returned by a call to previous() and the mapping that would be returned by a call to next(). An iterator for a map of length n has n+1 possible cursor positions, as illustrated by the carets (^) below:

                                 Element(0)        Element(1)        Element(2)        ... Element(n-1)

cursor positions:     ^                     ^                      ^                     ^                             ^

Note that the remove() and set() methods are not defined in terms of the cursor position; they are defined to operate on the last mapping returned by a call to next() or previous().

For further information do not hesitate to see the examples at the end of the page.

This documentation is partially based in the Java Collections Framework JavaDoc documentation. For further information see Java Collections Framework

View the examples



Public Methods
 MethodDefined By
  
ListMapIterator(source:IListMap, position:int = 0)
Constructor, creates a new ListMapIterator object.
ListMapIterator
  
hasNext():Boolean
Returns true if the iteration has more elements.
ListMapIterator
  
hasPrevious():Boolean
Returns true if the iteration has more mappings when traversing the map in the reverse direction.
ListMapIterator
  
next():*
Returns the next value in the iteration.
ListMapIterator
  
nextIndex():int
Returns the index of the mapping that would be returned by a subsequent call to next.
ListMapIterator
  
Returns the internal pointer of the iteration.
ListMapIterator
  
Returns the previous value in the iteration.
ListMapIterator
  
Returns the index of the mapping that would be returned by a subsequent call to previous.
ListMapIterator
  
put(key:*, value:*):void
Associates the specified value with the specified key in this map.
ListMapIterator
  
remove():void
Removes from the map the last mapping that was returned by next or previous.
ListMapIterator
  
reset():void
Resets the internal pointer of the iterator.
ListMapIterator
  
set(key:*, value:*):void
Replaces the last mapping returned by next or previous with the specified mapping.
ListMapIterator
Constructor Detail
ListMapIterator()Constructor
public function ListMapIterator(source:IListMap, position:int = 0)

Constructor, creates a new ListMapIterator object.

Parameters
source:IListMap — the source ListMapIterator to iterate over.
 
position:int (default = 0) — indicates the first mapping that would be returned by an initial call to next. An initial call to previous would return the mapping with the specified position minus one.

Throws
ArgumentError — if the source argument is null.
Method Detail
hasNext()method
public function hasNext():Boolean

Returns true if the iteration has more elements.

Returns
Booleantrue if the iteration has more elements.
hasPrevious()method 
public function hasPrevious():Boolean

Returns true if the iteration has more mappings when traversing the map in the reverse direction.

Returns
Booleantrue if the iteration has more mappings when traversing the map in the reverse direction.
next()method 
public function next():*

Returns the next value in the iteration. The pointer operation returns the key associated with the returned value.

Returns
*

Throws
NoSuchElementError — if the iteration has no more mappings.
 
ConcurrentModificationError — if the map was changed directly (without using the iterator) during iteration.
nextIndex()method 
public function nextIndex():int

Returns the index of the mapping that would be returned by a subsequent call to next. (Returns map size if the map iterator is at the end of the map.)

Returns
int — the index of the mapping that would be returned by a subsequent call to next, or map size if map iterator is at end of map.
pointer()method 
public function pointer():*

Returns the internal pointer of the iteration.

In this implementation the pointer is a key.

Returns
* — the internal pointer of the iteration.
previous()method 
public function previous():*

Returns the previous value in the iteration. The pointer operation returns the key associated with the returned value.

Returns
*

Throws
NoSuchElementError — if the iteration has no previous mappings.
 
ConcurrentModificationError — if the map was changed directly (without using the iterator) during iteration.
previousIndex()method 
public function previousIndex():int

Returns the index of the mapping that would be returned by a subsequent call to previous. (Returns -1 if the map iterator is at the beginning of the map.)

Returns
int — the index of the mapping that would be returned by a subsequent call to previous, or -1 if map iterator is at beginning of map.
put()method 
public function put(key:*, value:*):void

Associates the specified value with the specified key in this map. The mapping is inserted immediately before the next mapping that would be returned by next, if any, and after the next mapping that would be returned by previous, if any. (If the map contains no mappings, the new mapping becomes the sole mapping on the map.) The new mapping is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new mapping. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.)

Parameters

key:* — key with which the specified value is to be associated.
 
value:* — value to be associated with the specified key.


Throws
ConcurrentModificationError — if the map was changed directly (without using the iterator) during iteration.
 
ArgumentError — if the map already contains the specified key.
remove()method 
public function remove():void

Removes from the map the last mapping that was returned by next or previous. This call can only be made once per call to next or previous. It can be made only if IListMapIterator.add has not been called after the last call to next or previous.


Throws
org.as3coreaddendum.errors:UnsupportedOperationError — if the remove operation is not supported by this iterator.
 
org.as3coreaddendum.errors:IllegalStateError — if the next method has not yet been called, or the remove method has already been called after the last call to the next method.
 
ConcurrentModificationError — if the map was changed directly (without using the iterator) during iteration.
reset()method 
public function reset():void

Resets the internal pointer of the iterator.

set()method 
public function set(key:*, value:*):void

Replaces the last mapping returned by next or previous with the specified mapping. This call can be made only if neither IListMapIterator.remove nor IListMapIterator.add have been called after the last call to next or previous.

Parameters

key:* — key with which the specified value is to be associated.
 
value:* — value to be associated with the specified key.


Throws
org.as3coreaddendum.errors:UnsupportedOperationError — if the set operation is not supported by this iterator.
 
org.as3coreaddendum.errors:ClassCastError — if the class of the specified key or value prevents it from being added to this map.
 
org.as3coreaddendum.errors:IllegalStateError — if neither next or previous have been called, or remove or add have been called after the last call to next or previous.
 
ArgumentError — if the map already contains the specified key and it is not the replaced key.
Examples
Example 1
     import org.as3collections.IListMap;
     import org.as3collections.IListMapIterator;
     import org.as3collections.maps.ArrayListMap;
     
     var map1:IListMap = new ArrayListMap();
     map1.put("element-1", 1);
     map1.put("element-3", 3);
     map1.put("element-5", 5);
     
     map1                                // ["element-1"=1,"element-3"=3,"element-5"=5]
     
     var it:IListMapIterator = map1.listMapIterator();
     var e:int;
     
     while (it.hasNext())
     {
     
         ITERATION N.1
     
         it.pointer()                    // null
         it.nextIndex()                  // 0
         it.previousIndex()              // -1
     
         e = it.next();
         e                               // 1
     
         it.pointer()                    // "element-1"
         it.nextIndex()                  // 1
         it.previousIndex()              // 0
     
         ITERATION N.2
     
         it.pointer()                    // "element-1"
         it.nextIndex()                  // 1
         it.previousIndex()              // 0
     
         e = it.next();
         e                               // 3
     
         it.pointer()                    // "element-3"
         it.nextIndex()                  // 2
         it.previousIndex()              // 1
     
         if (e == 3)
         {
             //map1.put("element-4", 4)  // ConcurrentModificationError: During the iteration, the map was changed directly (without use the iterator).
             it.put("element-4", 4);
             map1                        // ["element-1"=1,"element-3"=3,"element-4"=4,"element-5"=5]
         }
     
         ITERATION N.3
     
         it.pointer()                    // "element-4"
         it.nextIndex()                  // 3
         it.previousIndex()              // 2
     
         e = it.next();
         e                               // 5
     
         it.pointer()                    // "element-5"
         it.nextIndex()                  // 4
         it.previousIndex()              // 3
     
         if (e == 5)
         {
             it.remove();
             map1                        // ["element-1"=1,"element-3"=3,"element-4"=4]
         }
     }
     
Example 2
     import org.as3collections.IListMap;
     import org.as3collections.IListMapIterator;
     import org.as3collections.maps.ArrayListMap;
     
     var map1:IListMap = new ArrayListMap();
     map1.put("element-1", 1);
     map1.put("element-3", 3);
     map1.put("element-5", 5);
     
     map1                                // ["element-1"=1,"element-3"=3,"element-5"=5]
     
     var it:IListMapIterator = map1.listIterator(map1.size());
     var e:int;
     
     while (it.hasPrevious())
     
     {
     
         ITERATION N.1
     
         it.pointer()                    // "element-5"
         it.nextIndex()                  // 3
         it.previousIndex()              // 2
     
         e = it.previous();
         e                               // 5
     
         it.pointer()                    // "element-3"
         it.nextIndex()                  // 2
         it.previousIndex()              // 1
     
         if (e == 5)
         {
             it.remove()
             map1                        // ["element-1"=1,"element-3"=3]
         }
     
         ITERATION N.2
     
         it.pointer()                    // "element-3"
         it.nextIndex()                  // 2
         it.previousIndex()              // 1
     
         e = it.previous();
         e                               // 3
     
         it.pointer()                    // "element-1"
         it.nextIndex()                  // 1
         it.previousIndex()              // 0
     
         if (e == 3)
         {
             //map1.put("element-4", 4); // ConcurrentModificationError: During the iteration, the map was changed directly (without use the iterator).
             it.put("element-4", 4);
             map1                        // [1,4,3]
         }
     
         ITERATION N.3
     
         it.pointer()                    // "element-3"
         it.nextIndex()                  // 2
         it.previousIndex()              // 1
     
         e = it.previous();
         e                               // 4
     
         it.pointer()                    // "element-1"
         it.nextIndex()                  // 1
         it.previousIndex()              // 0
     
         ITERATION N.4
     
         it.pointer()                    // "element-1"
         it.nextIndex()                  // 1
         it.previousIndex()              // 0
     
         e = it.previous();
         e                               // 1
     
         it.pointer()                    // null
         it.nextIndex()                  // 0
         it.previousIndex()              // -1
     }