View Javadoc

1   /**
2    * Copyright 2008 Bryan Ray
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0 
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package uk.org.bryanray.testtoys.generator.sequencegenerators;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Random;
21  
22  
23  /**
24   * <DL>
25   *   <DT><B>Project:</B></DT>
26   *   <DD>TestDataLoader</DD>
27   *   <DT><B>Filename:</B></DT>
28   *   <DD>RandomEnumerationGenerator.java</DD>
29   *   <DT><B>Creation Date:</B></DT>
30   *   <DD>29 May 2008</DD>
31   * </DL>
32   * 
33   * A sequence generator that serves up a list of values in a random order.
34   * 
35   * @author Bryan Ray
36   */
37  public class RandomEnumerationGenerator implements SequenceGenerator {
38  
39  	private String currentValue;
40  	private Random random = new Random();
41  	private List<String> list;
42  	
43  	/**
44  	 * @param enumeration A list of values to use as the enumeration.
45  	 * */
46  	public RandomEnumerationGenerator(List<String> enumeration) {
47  		if(0 < enumeration.size()) {
48  			list = new ArrayList<String>(enumeration);
49  			next();
50  		}
51  		else {
52  			throw new IllegalArgumentException("At least one enumeration value must be supplied.");
53  		}
54  	}
55  	
56  	/**
57  	 * {@inheritDoc}
58  	 */
59  	public void next() {
60  		int index = random.nextInt(list.size());
61  		currentValue = list.get(index);
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	public String value() {
68  		return currentValue;
69  	}
70  
71  }