00001 /* 00002 * UniqueRandomIntFactory.java 00003 * 00004 * Created on May 21, 2005, 5:14 PM 00005 * Created by Detro - 566/2145 00006 */ 00007 00008 package org.jaebi.server.util; 00009 00010 import java.util.HashSet; 00011 import java.util.Random; 00018 public class UniqueRandomIntFactory { 00024 private final HashSet prevValues; 00026 private final Random randomGenerator; 00027 00028 public UniqueRandomIntFactory() { 00029 prevValues = new HashSet(); 00030 randomGenerator = new Random(); 00031 } 00032 00033 public int createUniqueRandomInt() { 00034 int newValue = randomGenerator.nextInt(); 00035 Integer newValueObj = new Integer(newValue); 00036 00037 while ( prevValues.contains(newValueObj) ) { 00038 newValue = randomGenerator.nextInt(); 00039 newValueObj = new Integer(newValue); 00040 } 00041 00042 prevValues.add(newValueObj); 00043 return newValue; 00044 } 00045 }