au.gov.tas.dpiwe.mr.util
Class MrRandomList

java.lang.Object
  extended by java.util.AbstractCollection
      extended by java.util.AbstractList
          extended by au.gov.tas.dpiwe.mr.util.MrAbstractList
              extended by au.gov.tas.dpiwe.mr.util.MrRandomList
All Implemented Interfaces:
IterableCollection, Iterable, Collection, List

public class MrRandomList
extends MrAbstractList

An concrete implementation of an AbstractList which can be shuffled and unshuffled. The shuffling only affects the order of iteration, which is guaranteed to be stable every time this list is of the same length. i.e. adding or removing elements will destabilize the iteration order, but restoring the length will result in the same slots being chosen in the same order once more.

Since:
Mr Architecture 3.2
Version:
1.0.0.0
Author:
Kade Hansson, A Warberg

Field Summary
protected  Random r
           
protected  boolean shuffled
           
 
Fields inherited from class au.gov.tas.dpiwe.mr.util.MrAbstractList
l
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
MrRandomList(List elements)
          Creates a random list with a particular shuffled order.
MrRandomList(List elements, Random instance)
          Creates a random list with a particular shuffled order resulting from the Random instance given.
 
Method Summary
 Object clone()
           
 Iterator iterator()
          Returns the elements in a random order (if in shuffled state) or in natural order (if not in shuffled state).
 ListIterator listIterator()
          Returns the elements in a random order (if in shuffled state) or in natural order (if not in shuffled state).
static void main(String[] argv)
          Run a quick little unit test.
 void reshuffle(long seed)
          Reshuffles the list.
 void shuffle()
          Puts the list in shuffled state (the default).
 void unshuffle()
          Puts the list in unshuffled state.
 
Methods inherited from class au.gov.tas.dpiwe.mr.util.MrAbstractList
add, get, remove, remove, set, size
 
Methods inherited from class java.util.AbstractList
add, addAll, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray
 

Field Detail

r

protected Random r

shuffled

protected boolean shuffled
Constructor Detail

MrRandomList

public MrRandomList(List elements)
Creates a random list with a particular shuffled order.


MrRandomList

public MrRandomList(List elements,
                    Random instance)
Creates a random list with a particular shuffled order resulting from the Random instance given.

Parameters:
elements - the list elements.
instance - a source of randomness.
Method Detail

iterator

public Iterator iterator()
Returns the elements in a random order (if in shuffled state) or in natural order (if not in shuffled state).

Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface List
Overrides:
iterator in class AbstractList
Returns:
the appropriately ordered iterator.

listIterator

public ListIterator listIterator()
Returns the elements in a random order (if in shuffled state) or in natural order (if not in shuffled state).

Specified by:
listIterator in interface List
Overrides:
listIterator in class AbstractList
Returns:
the appropriately ordered iterator.

shuffle

public void shuffle()
Puts the list in shuffled state (the default). The shuffled order is unchanged.


reshuffle

public void reshuffle(long seed)
Reshuffles the list. The list is now in shuffled state. System.currentTimeMillis() is suggested as a seed value.

Parameters:
seed - the new seed to shuffle with.

unshuffle

public void unshuffle()
Puts the list in unshuffled state. The shuffled order is retained, and can be restored by calling shuffle(). To get a new shuffled order, call reshuffle().


clone

public Object clone()
             throws CloneNotSupportedException
Overrides:
clone in class MrAbstractList
Throws:
CloneNotSupportedException

main

public static void main(String[] argv)
Run a quick little unit test. Imagine we have a full suit of cards from a standard deck...

Parameters:
argv - no params needed.