package edu.redwoods.design.strategy;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class StrategyPatternTest {
public static void main(String[] args) {
// Stands in for the target table's primary-key index.
Set<String> warehouseKeys = new HashSet<>();
warehouseKeys.add("C-1001"); // already loaded yesterday
// The Context is built holding ONE algorithm.
LoadStage loadStage = new LoadStage(new BulkInsertStrategy(warehouseKeys));
LoadBatch cleanBatch = new LoadBatch("warehouse.orders",
List.of(row("C-2001", "2024-01-05", "19.99"),
row("C-2002", "2024-01-05", "42.50")));
LoadBatch dirtyBatch = new LoadBatch("warehouse.orders",
List.of(row("C-3001", "2024-01-06", "7.25"),
row("C-1001", "2024-01-06", "5.00"))); // duplicate key
System.out.println("=== batch 1, strategy = BulkInsert ===");
System.out.println(loadStage.doWork(cleanBatch));
System.out.println();
System.out.println("=== batch 2, strategy = BulkInsert ===");
try {
System.out.println(loadStage.doWork(dirtyBatch));
} catch (DuplicateKeyException e) {
System.out.println("caught: " + e.getMessage());
// Swap the algorithm while the program is running. The LoadStage
// class is not edited, not subclassed, and not rebuilt.
System.out.println("swapping strategy -> Upsert");
loadStage.setStrategy(new UpsertStrategy(warehouseKeys));
System.out.println(loadStage.doWork(dirtyBatch));
}
}
/** Small helper so the batch literals stay readable. */
private static String[] row(String key, String date, String amount) {
return new String[]{key, date, amount};
}
}