Skip to content

Commit b5ff5c2

Browse files
committed
chore: comments and formatting
1 parent 3275b53 commit b5ff5c2

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/Lesson9.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static void main(String[] args) {
2424
application.run(args);
2525
}
2626

27+
@Override
2728
public void run(String... args) throws Exception {
2829
if (args.length == 0) {
2930
return;
@@ -33,6 +34,8 @@ public void run(String... args) throws Exception {
3334
if (option == null) {
3435
throw new IllegalArgumentException("Provider name is required");
3536
}
37+
38+
// If the option is "--bulk", generate data files for all providers.
3639
if (option.equals("--bulk")) {
3740
for (DataProvider provider : providers) {
3841
String providerName = provider.getProviderName();
@@ -42,6 +45,7 @@ public void run(String... args) throws Exception {
4245
}
4346
return;
4447
}
48+
4549
String path = getDataPath();
4650
var fileGenerator = new SampleFileGenerator();
4751
fileGenerator.createTestFile(path, option);

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/generator/SampleFileGenerator.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private List<ValueGenerator> getShuffledGenerators() {
4444
return generators;
4545
}
4646

47+
// Mapping of classes to value generators
4748
private static final Map<Class<?>, ValueGenerator> TYPE_TO_GENERATOR =
4849
Map.of(
4950
Integer.class, new IntValueGenerator(),
@@ -54,25 +55,49 @@ Boolean.class, new BooleanValueGenerator(),
5455
String.class, new StringValueGenerator(),
5556
Double.class, new DoubleValueGenerator());
5657

58+
/**
59+
* Creates a JSON provider file with generated sample data.
60+
*
61+
* @param path the file path where the provider file will be written
62+
* @param providerName the name of the provider
63+
* @param provider the {DataProvider} that defines columns
64+
* @throws IllegalArgumentException if no generator is found for a column type
65+
*/
5766
public void createProviderFile(String path, String providerName, DataProvider provider) {
5867
var generators = getGenerators(provider);
5968
ArrayList<Map<String, String>> rows = createSampleData(generators);
69+
70+
// Writes the generated rows to a JSON file.
6071
saveToJsonFile(path, providerName, rows);
6172
}
6273

74+
/**
75+
* Retrieves value generators for each column in the given {@link DataProvider}.
76+
*
77+
* <p>The column names are expected to follow the format "column1", "column2", ..., "columnN".
78+
* Each column type must have a corresponding {@link ValueGenerator} in {@code TYPE_TO_GENERATOR}.
79+
* Ensures that the generators are in the same order as the columns in the DataProvider.
80+
*
81+
* @param provider the provider defining column types
82+
* @return a list of value generators, in column order
83+
* @throws IllegalArgumentException if no generator is mapped for a column type
84+
*/
6385
private List<ValueGenerator> getGenerators(DataProvider provider) {
6486
Map<String, Class> columnTypes = provider.getColumnTypeByName();
65-
6687
ArrayList<ValueGenerator> generators = new ArrayList<>();
88+
6789
for (int i = 1; i <= columnTypes.size(); i++) {
6890
String column = "column" + i;
6991
Class<?> type = columnTypes.get(column);
92+
93+
// Look up the appropriate generator for this column type.
7094
ValueGenerator generator = TYPE_TO_GENERATOR.get(type);
7195
if (generator == null) {
7296
throw new IllegalArgumentException("No generator found for type: " + type);
7397
}
7498
generators.add(generator);
7599
}
100+
76101
return generators;
77102
}
78103

0 commit comments

Comments
 (0)