/* * Created on Aug 5, 2004 */ package hashing; import java.util.Iterator; import java.util.Random; /** Producer of a series of random strings with the format of IP addresses, * xxx.xxx.xxx.xxx. This class is an Iterator, so it can be used with the * familiar pattern: * *
*
 Iterator sIter = new RandomAlphaStringsGenerator(maxDesired);
 while (sIter.hasNext()) {
    String aStr = (String) sIter.next();
    //do something with aStr
 }

* @author dickey */ public class RandomIPAddresses implements Iterator { private int limit; private Random rgen; public RandomIPAddresses(int limit) { if (limit <= 0) { throw new IllegalArgumentException("limit must be >0"); } this.limit = limit; rgen = new Random(this.limit); //seed so that the same series is generated } /* * @see java.util.Iterator#remove() */ public void remove() { throw new UnsupportedOperationException("remove is not supported"); } /* * @see java.util.Iterator#hasNext() */ public boolean hasNext() { assert limit >= 0; return limit > 0; } /* * @see java.util.Iterator#next() */ public Object next() { this.limit--; assert limit >= 0; StringBuffer ipStr = new StringBuffer(16); for (int i = 1; i <=4; i++) { ipStr.append(rgen.nextInt(256)); if (i < 4) { ipStr.append('.'); } } //System.out.println(ipStr); return ipStr.toString(); } }