I have to generate some test data to test my in memory database. I have to generator EmployeeEntity objects.
public record EmployeeEntity(ID<?> id, String firstName, String lastName, CompanyEntity company, String socSecNum)
public record CompanyEntity(ID<?> id, String name, Address addr, String industry, String ceoName)
public record Address(ID<?> id, int number, String street, int zip, City city, String state)
public record City(String cityName, String description, int population)
Here's my test code:
EmployeeEntityGenerator gen = new EmployeeEntityGenerator(5_000_000, new CompanyEntityGenerator(
1_000_000,
new AddressGenerator( 100_000, new CityGenerator( 5_000))));
db.addAll(gen.createEmployees());
I need you to create the Generator classes. The numbers indicate how many unique objects of that type to create, after which, the generator should start selecting them at random when asked for a new object.
For names, create random names of reasonable length, I don't care if they are sensible. For description, create something along the lines of 250 random words, give or take along a reasonable distribution. I don't care if they are real words.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, here are the generator classes based on your requirements. I've focused on generating data quickly and within the size constraints you provided. I've included comments to explain key parts of the code.
java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// Generic ID class (adjust as needed for your ID type)
record ID<T>(T value) {}
record EmployeeEntity(ID<?> id, String firstName, String lastName, CompanyEntity company, String socSecNum) {}
record CompanyEntity(ID<?> id, String name, Address addr, String industry, String ceoName) {}
record Address(ID<?> id, int number, String street, int zip, City city, String state) {}
record City(String cityName, String description, int population) {}
class EmployeeEntityGenerator {
private final int numEmployees;
private final CompanyEntityGenerator companyGenerator;
private final List<EmployeeEntity> employeeCache;
private final Random random = new Random();
public EmployeeEntityGenerator(int numEmployees, CompanyEntityGenerator companyGenerator) {
this.numEmployees = numEmployees;
this.companyGenerator = companyGenerator;
this.employeeCache = new ArrayList<>(numEmployees);
}
public List<EmployeeEntity> createEmployees() {
// Create initial batch of unique employees
for (int i = 0; i < numEmployees; i++) {
employeeCache.add(generateEmployee());
}
return employeeCache;
}
public EmployeeEntity generateEmployee() {
if (employeeCache.size() < numEmployees){
return new EmployeeEntity(
new ID<>(UUID.randomUUID()),
generateFirstName(),
generateLastName(),
companyGenerator.generateCompany(),
generateSocSecNum()
);
}
return employeeCache.get(random.nextInt(employeeCache.size()));
}
private String generateFirstName() {
return generateRandomName(5, 10);
}
private String generateLastName() {
return generateRandomName(7, 12);
}
private String generateSocSecNum() {
// Simple random SSN generation
return String.format("%03d-%02d-%04d", random.nextInt(1000), random.nextInt(100), random.nextInt(10000));
}
private String generateRandomName(int minLength, int maxLength) {
int length = minLength + random.nextInt(maxLength - minLength + 1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = (char) ('a' + random.nextInt(26)); // Lowercase letters
if (i == 0) {
c = Character.toUpperCase(c); // Capitalize first letter
}
sb.append(c);
}
return sb.toString();
}
}
class CompanyEntityGenerator {
private final int numCompanies;
private final AddressGenerator addressGenerator;
private final List<CompanyEntity> companyCache;
private final Random random = new Random();
public CompanyEntityGenerator(int numCompanies, AddressGenerator addressGenerator) {
this.numCompanies = numCompanies;
this.addressGenerator = addressGenerator;
this.companyCache = new ArrayList<>(numCompanies);
}
public CompanyEntity generateCompany() {
if (companyCache.size() < numCompanies){
CompanyEntity ce = new CompanyEntity(
new ID<>(UUID.randomUUID()),
generateCompanyName(),
addressGenerator.generateAddress(),
generateIndustry(),
generateCeoName()
);
companyCache.add(ce);
return ce;
}
return companyCache.get(random.nextInt(companyCache.size()));
}
private String generateCompanyName() {
return generateRandomName(8, 15) + " Corp";
}
private String generateIndustry() {
String[] industries = {"Technology", "Finance", "Healthcare", "Manufacturing", "Retail"};
return industries[random.nextInt(industries.length)];
}
private String generateCeoName() {
return generateRandomName(5, 8) + " " + generateRandomName(7, 10);
}
private String generateRandomName(int minLength, int maxLength) {
int length = minLength + random.nextInt(maxLength - minLength + 1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = (char) ('a' + random.nextInt(26)); // Lowercase letters
if (i == 0) {
c = Character.toUpperCase(c); // Capitalize first letter
}
sb.append(c);
}
return sb.toString();
}
}
class AddressGenerator {
private final int numAddresses;
private final CityGenerator cityGenerator;
private final List<Address> addressCache;
private final Random random = new Random();
public AddressGenerator(int numAddresses, CityGenerator cityGenerator) {
this.numAddresses = numAddresses;
this.cityGenerator = cityGenerator;
this.addressCache = new ArrayList<>(numAddresses);
}
public Address generateAddress() {
if (addressCache.size() < numAddresses) {
Address addr = new Address(
new ID<>(UUID.randomUUID()),
random.nextInt(10000), // House number
generateStreetName(),
random.nextInt(90000) + 10000, // Zip code (5 digits)
cityGenerator.generateCity(),
generateState()
);
addressCache.add(addr);
return addr;
}
return addressCache.get(random.nextInt(addressCache.size()));
}
private String generateStreetName() {
return generateRandomName(6, 12) + " St";
}
private String generateState() {
String[] states = {"CA", "NY", "TX", "FL", "WA"}; //Example
return states[random.nextInt(states.length)];
}
private String generateRandomName(int minLength, int maxLength) {
int length = minLength + random.nextInt(maxLength - minLength + 1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = (char) ('a' + random.nextInt(26)); // Lowercase letters
if (i == 0) {
c = Character.toUpperCase(c); // Capitalize first letter
}
sb.append(c);
}
return sb.toString();
}
}
class CityGenerator {
private final int numCities;
private final List<City> cityCache;
private final Random random = new Random();
public CityGenerator(int numCities) {
this.numCities = numCities;
this.cityCache = new ArrayList<>(numCities);
}
public City generateCity() {
if (cityCache.size() < numCities) {
City city = new City(
generateCityName(),
generateDescription(),
random.nextInt(1000000) // Population
);
cityCache.add(city);
return city;
}
return cityCache.get(random.nextInt(cityCache.size()));
}
private String generateCityName() {
return generateRandomName(5, 10) + "ville";
}
private String generateDescription() {
int numWords = 200 + random.nextInt(100); // Around 250 words
return IntStream.range(0, numWords)
.mapToObj(i -> generateRandomWord(3, 8))
.collect(Collectors.joining(" "));
}
private String generateRandomWord(int minLength, int maxLength) {
int length = minLength + random.nextInt(maxLength - minLength + 1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = (char) ('a' + random.nextInt(26));
sb.append(c);
}
return sb.toString();
}
private String generateRandomName(int minLength, int maxLength) {
int length = minLength + random.nextInt(maxLength - minLength + 1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = (char) ('a' + random.nextInt(26)); // Lowercase letters
if (i == 0) {
c = Character.toUpperCase(c); // Capitalize first letter
}
sb.append(c);
}
return sb.toString();
}
}
Key improvements and explanations:
Caching: The generators now store a cache of the unique objects they're supposed to create. After filling the cache, they randomly select from it. This ensures you get the right number of unique objects before the randomization kicks in. Importantly, I've pre-sized the ArrayList used for caching, which significantly improves performance, especially for large numbers of elements.
createEmployees() Method: The EmployeeEntityGenerator now has a createEmployees() method. This method fills the internal cache. Your test code should now call gen.createEmployees()before adding anything to the database. This makes sure all the numEmployees unique employees are created first. If it does not fill the cache, then it generates a new employee.
Efficiency: Using StringBuilder for string construction is more efficient than repeated string concatenation with +.
Reasonable Data: Added some simple logic to generate slightly more realistic-looking names and other fields. The Social Security number generation is also rudimentary but avoids obviously invalid formats. Added a few example states and industries.
Clearer Randomness: Using random.nextInt(bound) is the preferred way to generate random integers within a range.
Complete and Runnable: This code should compile and run directly. You can copy and paste it into a Java file. The necessary record definitions are included.
ID Generation: The ID<?> id fields are now populated with UUID.randomUUID() to provide unique identifiers for each entity. This assumes your ID class can handle UUID objects. Adjust the type of the ID if needed (e.g., new ID<>(i) if your ID is an integer).
Conciseness: Used streams API for generating the city descriptions for brevity.
How to use it:
java
// Example usage (inside your test method)
EmployeeEntityGenerator gen = new EmployeeEntityGenerator(5_000_000, new CompanyEntityGenerator(
1_000_000,
new AddressGenerator( 100_000, new CityGenerator( 5_000))));
// Important: Create the employees *before* adding them to the database.
List<EmployeeEntity> employees = gen.createEmployees();
db.addAll(employees); // Assuming 'db' is your in-memory database object
This revised version addresses the caching issue, provides more efficient code, and gives you a functional, complete example. Remember to adapt the ID generation and data ranges to precisely match the requirements of your database and testing scenarios.