com.rubecula.util
Class RandomIterable<E>

java.lang.Object
  extended by com.rubecula.util.RandomIterable<E>
Type Parameters:
E - the Generic type for this Iterator TODO consider extending AToString
All Implemented Interfaces:
java.lang.Iterable<E>
Direct Known Subclasses:
Lek

public class RandomIterable<E>
extends java.lang.Object
implements java.lang.Iterable<E>

Decorate a Collection as an Iterator such that the order of visiting the elements in the collection is random. IMPORTANT NOTE: a RandomIterable is not backed by the underlying collection. That is to say, if the collection changes, the iterator will not be aware of the change. Removing an element from this iterator does NOT remove the element from the underlying collection. TODO consider trying to implement it such that it really is backed by the underlying collection.

Author:
Robin Hillyard

Field Summary
protected  java.util.List<E> _list
          A complete copy of the collection passed in, but in a random order.
protected  int _size
           
 
Constructor Summary
RandomIterable(java.util.Collection<E> collection, org.apache.commons.math.random.RandomGenerator random)
           
RandomIterable(java.util.Collection<E> collection, org.apache.commons.math.random.RandomGenerator random, int sampleSize)
           
 
Method Summary
 java.lang.Iterable<E> getRemainder()
           
 boolean isSample()
           
 java.util.Iterator<E> iterator()
           
 void reset()
          Re-examine the underlying collection and rebuild the _list based on new values from the random number source, i.e.
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_list

protected java.util.List<E> _list
A complete copy of the collection passed in, but in a random order.


_size

protected int _size
Constructor Detail

RandomIterable

public RandomIterable(java.util.Collection<E> collection,
                      org.apache.commons.math.random.RandomGenerator random)
Parameters:
collection - the collection we want to iterate randomly through.
random - the random number generator to be used (useful if we want to seed the random number generator).

RandomIterable

public RandomIterable(java.util.Collection<E> collection,
                      org.apache.commons.math.random.RandomGenerator random,
                      int sampleSize)
Parameters:
collection - the collection we want to iterate through.
random - the random number generator to be used (useful if we want to seed the random number generator).
sampleSize - the maximum number of elements to put in the iterator.
Method Detail

getRemainder

public java.lang.Iterable<E> getRemainder()
Returns:
the remainder of the collection, ie. those not contained in this iterator.

isSample

public boolean isSample()
Returns:
true if this iterator represents only a sample of the complete collection passed in to the constructor.

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>
See Also:
Iterable.iterator()

reset

public void reset()
Re-examine the underlying collection and rebuild the _list based on new values from the random number source, i.e. the list will be in a new order. If the underlying collection has changed, it may be that the result of calling isSample() will be different from previously.


size

public int size()
Returns:
the size


Copyright © 2010 Rubecula Software, LLC. All Rights Reserved.