/* * Created on Aug 4, 2004 */ package hashing; import java.util.Iterator; import java.util.Random; /** Generate "random" (but repeatable) strings of upper-case letters. * This class is an Iterator, so to use it just use the usual pattern of * hasNext() and next(): *
*
 Iterator sIter = new RandomAlphaStringsGenerator(maxDesired);
 while (sIter.hasNext()) {
    String aStr = (String) sIter.next();
    //do something with aStr
 }

* @author dickey */ public class RandomAlphaStringsGenerator implements Iterator { private int remainingToGenerate; private int stringMinLength = 5; private int stringMaxLength = 20; private Random rgen; /** Construct a generator that will generate exactly the number of * strings given by the parameter. For a given parameter value, the * sequence of "random" strings will be the same each time. The strings * generated will have lengths which are random within a default range * of lengths. To get each string in turn, use the next() method. * @param numberToGenerate the number of strings to generate; after the * last one has been delivered, hasNext() will begin returning false. */ public RandomAlphaStringsGenerator(int numberToGenerate) { if (numberToGenerate < 0) { throw new IllegalArgumentException("number of strings to generate " + "must be >= 0"); } this.remainingToGenerate = numberToGenerate; this.rgen = new Random(numberToGenerate); //same random seed each time } /** not supported. * @see java.util.Iterator#remove() */ public void remove() { throw new UnsupportedOperationException("remove not supported for " + this.getClass().getName()); } /** See if there are any strings left to get. * @see java.util.Iterator#hasNext() */ public boolean hasNext() { return this.remainingToGenerate > 0; } /** Get the next random string in the sequence. * @see java.util.Iterator#next() * @return a random String. */ public Object next() { this.remainingToGenerate--; return randString(); } /** Set the minimum length of each string generated (from this point on) * to the given value. * * @param newMin desired min length, > 0. */ public void setStringMinLength(int newMin) { if (newMin <= 0) { throw new IllegalArgumentException("length must be > 0"); } this.stringMinLength = newMin; } /** Set the maximum length of each string generated (from this point on) * to the given value. * * @param newvalue desired max length, > 0. */ public void setStringMaxLength(int newvalue) { if (newvalue <= 0) { throw new IllegalArgumentException("length must be > 0"); } this.stringMaxLength = newvalue; } private String randString() { if (this.stringMaxLength < this.stringMinLength) { throw new IllegalArgumentException("String min length " + this.stringMinLength + " must not be greater than " + "string max length " + this.stringMaxLength); } int slen = this.stringMinLength + this.rgen.nextInt(1 + this.stringMaxLength - this.stringMinLength); assert slen >= this.stringMinLength && slen <= this.stringMaxLength; StringBuffer buf = new StringBuffer(slen); for (int c = 0; c < slen; c++) { char nextChar = randChar(); buf.append(nextChar); } return buf.toString(); } private char randChar() { char nextChar = (char) ('A' + this.rgen.nextInt(26)); assert nextChar >= 'A' && nextChar <= 'Z'; return nextChar; } }