To implement the generator classes for your test data, we need to ensure that each generator creates a specified number of unique objects and then randomly selects from those objects when asked for more. Below is the complete implementation of the generator classes, including random name generation, description creation, and other necessary logic.
Implementation
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Generators {
// Base ID class
public static class ID<T> {
private final T value;
public ID(T value) {
this.value = value;
}
@Override
public String toString() {
return value.toString();
}
}
// CityGenerator
public static class CityGenerator {
private final List<City> cities = new ArrayList<>();
private final Random random = ThreadLocalRandom.current();
public CityGenerator(int uniqueCount) {
for (int i = 0; i < uniqueCount; i++) {
cities.add(new City(
randomString(5, 15), // cityName
randomDescription(200, 300), // description
random.nextInt(10_000, 10_000_000) // population
));
}
}
public City generate() {
return cities.get(random.nextInt(cities.size()));
}
}
// AddressGenerator
public static class AddressGenerator {
private final List<Address> addresses = new ArrayList<>();
private final CityGenerator cityGenerator;
private final Random random = ThreadLocalRandom.current();
public AddressGenerator(int uniqueCount, CityGenerator cityGenerator) {
this.cityGenerator = cityGenerator;
for (int i = 0; i < uniqueCount; i++) {
addresses.add(new Address(
new ID<>(i + 1),
random.nextInt(1, 1000), // number
randomString(5, 20), // street
random.nextInt(10000, 99999), // zip
cityGenerator.generate(), // city
randomString(2, 2).toUpperCase() // state
));
}
}
public Address generate() {
return addresses.get(random.nextInt(addresses.size()));
}
}
// CompanyEntityGenerator
public static class CompanyEntityGenerator {
private final List<CompanyEntity> companies = new ArrayList<>();
private final AddressGenerator addressGenerator;
private final Random random = ThreadLocalRandom.current();
public CompanyEntityGenerator(int uniqueCount, AddressGenerator addressGenerator) {
this.addressGenerator = addressGenerator;
for (int i = 0; i < uniqueCount; i++) {
companies.add(new CompanyEntity(
new ID<>(i + 1),
randomString(5, 20), // name
addressGenerator.generate(), // address
randomString(5, 15), // industry
randomString(5, 15) // ceoName
));
}
}
public CompanyEntity generate() {
return companies.get(random.nextInt(companies.size()));
}
}
// EmployeeEntityGenerator
public static class EmployeeEntityGenerator {
private final List<EmployeeEntity> employees = new ArrayList<>();
private final CompanyEntityGenerator companyGenerator;
private final Random random = ThreadLocalRandom.current();
public EmployeeEntityGenerator(int uniqueCount, CompanyEntityGenerator companyGenerator) {
this.companyGenerator = companyGenerator;
for (int i = 0; i < uniqueCount; i++) {
employees.add(new EmployeeEntity(
new ID<>(i + 1),
randomString(5, 15), // firstName
randomString(5, 15), // lastName
companyGenerator.generate(), // company
randomString(9, 9) // socSecNum
));
}
}
public List<EmployeeEntity> createEmployees(int count) {
return IntStream.range(0, count)
.mapToObj(i -> employees.get(random.nextInt(employees.size())))
.collect(Collectors.toList());
}
}
// Helper methods
private static String randomString(int minLength, int maxLength) {
int length = ThreadLocalRandom.current().nextInt(minLength, maxLength + 1);
return ThreadLocalRandom.current()
.ints(length, 'a', 'z' + 1)
.mapToObj(i -> String.valueOf((char) i))
.collect(Collectors.joining());
}
private static String randomDescription(int minLength, int maxLength) {
int length = ThreadLocalRandom.current().nextInt(minLength, maxLength + 1);
return ThreadLocalRandom.current()
.ints(length, 'a', 'z' + 1)
.mapToObj(i -> String.valueOf((char) i))
.collect(Collectors.joining(" "));
}
public static void main(String[] args) {
CityGenerator cityGenerator = new CityGenerator(5_000);
AddressGenerator addressGenerator = new AddressGenerator(100_000, cityGenerator);
CompanyEntityGenerator companyGenerator = new CompanyEntityGenerator(1_000_000, addressGenerator);
EmployeeEntityGenerator employeeGenerator = new EmployeeEntityGenerator(5_000_000, companyGenerator);
// Generate 10 employees as an example
List<EmployeeEntity> employees = employeeGenerator.createEmployees(10);
employees.forEach(System.out::println);
}
}
Explanation
-
Generators:
- Each generator (
CityGenerator, AddressGenerator, CompanyEntityGenerator, EmployeeEntityGenerator) creates a fixed number of unique objects during initialization.
- After initialization, the
generate method randomly selects from the pre-generated objects.
-
Random Data Generation:
randomString: Generates random strings of a given length range.
randomDescription: Generates a random "description" of words with a specified length range.
-
Usage:
- The
main method demonstrates how to use the generators to create test data. It generates 10 EmployeeEntity objects as an example.
-
Scalability:
- The generators are designed to handle large numbers of unique objects efficiently by storing them in memory and selecting randomly when needed.
This implementation ensures that the generated data adheres to the constraints provided while being scalable and reusable.