
toxgene.util.cdata.xmark.Birthdays Maven / Gradle / Ivy
package toxgene.util.cdata.xmark;
import java.util.Random;
import toxgene.interfaces.ToXgeneCdataGenerator;
/**
* This class implements a simple CDATA generator that produces
* random birthdays in the format MM/DD/YYYY.
*
* This code is provided as part of ToXgene - (c) 2001 University of Toronto
* and IBM Corporation.
*
* @author Denilson Barbosa
* @version 1.0
*/
public class Birthdays implements ToXgeneCdataGenerator {
private static Random pick;
private static String result;
/**
* Specifies a seed for the random generator so that repeated
* executions always produce the same content, if the same seed is
* provided.
*
* @param seed random seed to initialize the random generator
*/
public void setRandomSeed(int seed){
pick = new Random(seed);
}
/**
* Generates random text whose length is determined by the parameter
* length. A value of -1 determines that a string of any length can
* be returned; a positive value means that a string must be
* truncated if necessary.
*
* @param length specifies the length of the CDATA value to be returned.
*/
public String getCdata(int length){
result = String.format("%02d", pick.nextInt(12) + 1) + "/" +
String.format("%02d", pick.nextInt(31) + 1) + "/" +
String.valueOf(pick.nextInt(120) + 1900);
//length == -1 means that the string should not be trimmed
if (length == -1){
return result;
}
if (result.length() > length)
return result.substring(0,length);
else
return result;
}
//my own testing purposes:
public static void main(String[] args) {
System.out.println(generateBirthday());
}
public static String generateBirthday() {
pick = new Random();
result = String.format("%02d", pick.nextInt(12) + 1) + "/" +
String.format("%02d", pick.nextInt(31) + 1) + "/" +
String.valueOf(pick.nextInt(120) + 1900);
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy