Created
February 11, 2011 16:29
-
-
Save xsalefter/822592 to your computer and use it in GitHub Desktop.
Simple, quick and dirty way to generate a random "IN" clause in SQL language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author xsalefter ([email protected]) | |
*/ | |
public class RandomGenerator { | |
private int sizeOfQuestion; // Mean how much question want to display per user. | |
private Random random; | |
private int dataSize; // This is total amount of data in the database. | |
private StringBuffer generatedRandomNumber; | |
public RandomGenerator() { | |
this.random = new Random(); | |
} | |
public RandomGenerator(int sizeOfQuestion) { | |
this(); | |
this.sizeOfQuestion = sizeOfQuestion; | |
} | |
public RandomGenerator(int sizeOfQuestion, int dataSize) { | |
this(sizeOfQuestion); | |
this.dataSize = dataSize; | |
} | |
public RandomGenerator setDataSize(int dataSize) { | |
this.dataSize = dataSize; | |
return this; | |
} | |
public RandomGenerator setSizeOfQuestion(int question) { | |
this.sizeOfQuestion = question; | |
return this; | |
} | |
public RandomGenerator parse() { | |
this.generatedRandomNumber = new StringBuffer(); | |
for (int i = 1; i <= this.sizeOfQuestion; i ++) { | |
if (i == this.sizeOfQuestion) { | |
this.generatedRandomNumber. | |
append((this.random.nextInt(dataSize) + 1)); | |
} else { | |
this.generatedRandomNumber. | |
append((this.random.nextInt(dataSize) + 1)). | |
append(", "); | |
} | |
} | |
return this; | |
} | |
public String generate() { | |
return new StringBuffer(). | |
append("('"). | |
append(this.generatedRandomNumber.toString()). | |
append("')"). | |
toString(); | |
} | |
public String generate(String prefix, String postfix) { | |
return new StringBuffer().append(prefix). | |
append(this.generatedRandomNumber.toString()). | |
append(postfix). | |
toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public class RandomGeneratorTest {
}