//////////////////////////////////////////////////////////////////////////////
// 
//               Copyright 2010, Starting Block Technologies
//                        www.startingblocktech.com
//                           All Rights Reserved
//
//////////////////////////////////////////////////////////////////////////////

package com.startingblocktech.tcases.generator;

import org.apache.commons.collections15.IteratorUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * Returns a random permutation of a sequence.
 *
 * @version $Revision$, $Date$
 */
public class RandSeq
  {
  /**
   * Creates a new RandSeq object.
   */
  public RandSeq( Random generator)
    {
    generator_ = generator;
    }
  
  /**
   * Creates a new RandSeq object.
   */
  public RandSeq()
    {
    this( new Random());
    }
  
  /**
   * Creates a new RandSeq object.
   */
  public RandSeq( long seed)
    {
    this( new Random( seed));
    }

  /**
   * Returns the given list with its elements rearranged in a random order.
   */
  public <T> List<T> reorder( List<T> sequence)
    {
    int elementCount = sequence.size();
    if( elementCount > 1)
      {
      List<T> elements = new ArrayList<T>( sequence);
      sequence.clear();

      for( ; elementCount > 0; elementCount--)
        {
        sequence.add( elements.remove( generator_.nextInt( elementCount)));
        }
      }

    return sequence;
    }

  /**
   * Returns an iterator that visits the elements of the given sequence in a random order.
   */
  public <T> Iterator<T> reorder( Iterator<T> sequence)
    {
    return reorder( IteratorUtils.toList( sequence)).iterator();
    }

  /**
   * Applies the given <code>randSeq</code> to rearrange the given sequence in a random order.
   * If <code>randSeq</code> is <code>null</code>, returns the sequence unchanged.
   */
  public static <T> List<T> reorderIf( RandSeq randSeq, List<T> sequence)
    {
    return randSeq==null? sequence : randSeq.reorder( sequence);
    }

  /**
   * Applies the given <code>randSeq</code> to rearrange the given sequence in a random order.
   * If <code>randSeq</code> is <code>null</code>, returns the sequence unchanged.
   */
  public static <T> Iterator<T> reorderIf( RandSeq randSeq, Iterator<T> sequence)
    {
    return randSeq==null? sequence : randSeq.reorder( sequence);
    }

  private Random generator_;
  }


@@ -450,24 +450,53 @@
 .ArrayList;%0A
+import java.util.Collection;%0A
 import java.
@@ -2070,32 +2070,470 @@
 quence);%0A    %7D%0A%0A
+  /**%0A   * Applies the given %3Ccode%3ErandSeq%3C/code%3E to rearrange the given collection in a random order.%0A   * If %3Ccode%3ErandSeq%3C/code%3E is %3Ccode%3Enull%3C/code%3E, returns the collection in normal order.%0A   */%0A  public static %3CT%3E List%3CT%3E order( RandSeq randSeq, Collection%3CT%3E collection)%0A    %7B%0A    ArrayList%3CT%3E sequence = randSeq==null? null : new ArrayList%3CT%3E( collection);%0A    return sequence==null? sequence : randSeq.reorder( sequence);%0A    %7D%0A%0A
   /**%0A   * Appli

Add support for converting unordered collections into random sequence