import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class CollectionGuard {
private final Set<String> guardedCollections = new HashSet<>();
private boolean dryRunMode = false;
public CollectionGuard() {
// Add collections to be guarded. Can be dynamically updated.
}
public void addCollection(String collectionName) {
guardedCollections.add(collectionName);
}
public void setDryRunMode(boolean dryRunMode) {
this.dryRunMode = dryRunMode;
}
public void executeTask(String collectionName, Runnable task) {
if (dryRunMode) {
System.out.println("Dry run: Would execute task on collection: " + collectionName);
return;
}
if (!guardedCollections.contains(collectionName)) {
System.out.println("Collection '" + collectionName + "' is not guarded. Skipping.");
return;
}
try {
task.run(); // Execute the task on the collection
} catch (Exception e) {
System.err.println("Error executing task on collection '" + collectionName + "': " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
CollectionGuard guard = new CollectionGuard();
guard.addCollection("myCollection");
// Example task
Runnable myTask = () -> {
System.out.println("Executing task on myCollection");
};
guard.setDryRunMode(false);
guard.executeTask("myCollection", myTask);
guard.setDryRunMode(true);
guard.executeTask("myCollection", myTask);
guard.executeTask("anotherCollection", myTask); // should skip
}
}
Add your comment