net.sourceforge.cilib.cooperative.algorithmiterators
Class RandomAlgorithmIterator<E extends Algorithm>

java.lang.Object
  extended by net.sourceforge.cilib.cooperative.algorithmiterators.SequentialAlgorithmIterator<E>
      extended by net.sourceforge.cilib.cooperative.algorithmiterators.RandomAlgorithmIterator<E>
Type Parameters:
E - The Algorithm type.
All Implemented Interfaces:
Serializable, Iterator<E>, ListIterator<E>, AlgorithmIterator<E>, Cloneable

public class RandomAlgorithmIterator<E extends Algorithm>
extends SequentialAlgorithmIterator<E>

Iterate through a list of Algorithms in a random order. With this class it is possible to move forwards and backwards in an iteration even though the order is random. To achieve this an list of random indices are generated and the indices are used as the iteration order. The iteration order will be different for every RandomAlgorithmIterator object.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class net.sourceforge.cilib.cooperative.algorithmiterators.SequentialAlgorithmIterator
algorithms, index
 
Constructor Summary
RandomAlgorithmIterator()
          Construct a new RandomAlgorithmIterator.
RandomAlgorithmIterator(List<E> a)
          Construct a new RandomAlgorithmIterator for the supplied list.
RandomAlgorithmIterator(RandomAlgorithmIterator rhs)
          Construct a new RandomAlgorithmIterator from the supplied one.
 
Method Summary
 void add(E algorithm)
          Inserts the specified element into the list.
 E current()
          Returns the element at the current index.
 RandomAlgorithmIterator<E> getClone()
          Clone this list iterator and return it.
 E next()
          Gets the next element in the list.
 E previous()
          Gets the previous element in the list.
 void remove()
          Removes from the list the last element that was returned by next or previous.
 void set(E algorithm)
          Replaces the last element returned by next or previous with the specified element.
 void setAlgorithms(List<E> a)
          Sets the list that will be iterated through.
 
Methods inherited from class net.sourceforge.cilib.cooperative.algorithmiterators.SequentialAlgorithmIterator
hasNext, hasPrevious, nextIndex, previousIndex
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomAlgorithmIterator

public RandomAlgorithmIterator()
Construct a new RandomAlgorithmIterator.


RandomAlgorithmIterator

public RandomAlgorithmIterator(List<E> a)
Construct a new RandomAlgorithmIterator for the supplied list. Generates the random sequence of indices.

Parameters:
a - the list that will be iterated through randomly.

RandomAlgorithmIterator

public RandomAlgorithmIterator(RandomAlgorithmIterator rhs)
Construct a new RandomAlgorithmIterator from the supplied one. This list iterator will use the same random sequence of indices from the supplied RandomAlgorithmIterator.

Parameters:
rhs - the RandomAlgorithmIterator that should be copied.
Method Detail

getClone

public RandomAlgorithmIterator<E> getClone()
Clone this list iterator and return it.

Specified by:
getClone in interface AlgorithmIterator<E extends Algorithm>
Specified by:
getClone in interface Cloneable
Overrides:
getClone in class SequentialAlgorithmIterator<E extends Algorithm>
Returns:
a clone of this list iterator.
See Also:
Object.clone()

next

public E next()
Gets the next element in the list. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

Specified by:
next in interface Iterator<E extends Algorithm>
Specified by:
next in interface ListIterator<E extends Algorithm>
Overrides:
next in class SequentialAlgorithmIterator<E extends Algorithm>
Returns:
the next element in the list.
Throws:
{@link - NoSuchElementException} when the current index is already at the end of the list.

previous

public E previous()
Gets the previous element in the list. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)

Specified by:
previous in interface ListIterator<E extends Algorithm>
Overrides:
previous in class SequentialAlgorithmIterator<E extends Algorithm>
Returns:
the previous element in the list.
Throws:
{@link - NoSuchElementException} when the current index is already at the beginning of the list.

add

public void add(E algorithm)
Inserts the specified element into the list. The element is inserted immediately before the next element that would be returned by next, if any, and after the next element that would be returned by previous, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.) WARNING: Using the add method too much during an iteration causes randomness to dissapear.

Specified by:
add in interface ListIterator<E extends Algorithm>
Overrides:
add in class SequentialAlgorithmIterator<E extends Algorithm>
Parameters:
algorithm - The Algorithm to add.

remove

public void remove()
Removes from the list the last element that was returned by next or previous. The index of the current element is also repositioned to prevent the iteration from skipping elements (due to the manner in which the .next() and .previous() methods work).

Specified by:
remove in interface Iterator<E extends Algorithm>
Specified by:
remove in interface ListIterator<E extends Algorithm>
Overrides:
remove in class SequentialAlgorithmIterator<E extends Algorithm>
Throws:
{@link - IndexOutOfBoundsException} when the current element index is past the beginning or end of the list.

set

public void set(E algorithm)
Replaces the last element returned by next or previous with the specified element.

Specified by:
set in interface ListIterator<E extends Algorithm>
Overrides:
set in class SequentialAlgorithmIterator<E extends Algorithm>
Parameters:
algorithm - The element with which to replace the last element returned by next or previous.

setAlgorithms

public void setAlgorithms(List<E> a)
Sets the list that will be iterated through. Sets the current iteration position to -1, i.e. iteration hasn't started yet. Generates the random sequence of indices.

Specified by:
setAlgorithms in interface AlgorithmIterator<E extends Algorithm>
Overrides:
setAlgorithms in class SequentialAlgorithmIterator<E extends Algorithm>
Parameters:
a - The list of Algorithms to set.

current

public E current()
Returns the element at the current index.

Specified by:
current in interface AlgorithmIterator<E extends Algorithm>
Overrides:
current in class SequentialAlgorithmIterator<E extends Algorithm>
Returns:
The element at the current index.


Copyright © 2009 CIRG. All Rights Reserved.