import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TaskQueueImporter {
/**
* Imports task queue data from a file. Supports multiple formats (e.g., simple text, CSV).
*
* @param filePath The path to the task queue data file.
* @return A map where keys are queue names and values are lists of tasks. Returns an empty map on error.
*/
public static Map<String, List<String>> importTaskQueueData(String filePath) {
Map<String, List<String>> taskQueues = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim(); // Remove leading/trailing whitespace
if (line.isEmpty()) {
continue; // Skip empty lines
}
// Attempt to parse the line based on common formats. Adjust regex as needed.
// Format 1: "QueueName: task1,task2,task3"
Pattern pattern1 = Pattern.compile("^(.*?): (.*)$");
Matcher matcher1 = pattern1.matcher(line);
if (matcher1.matches()) {
String queueName = matcher1.group(1).trim();
String tasksString = matcher1.group(2).trim();
List<String> tasks = tasksString.split(",").stream().map(String::trim).toList();
taskQueues.put(queueName, tasks);
continue;
}
// Format 2: "QueueName - task1;task2;task3"
Pattern pattern2 = Pattern.compile("^(.*?)( - )(.*)$");
Matcher matcher2 = pattern2.matcher(line);
if (matcher2.matches()) {
String queueName = matcher2.group(1).trim();
String tasksString = matcher2.group(3).trim();
List<String> tasks = tasksString.split(";").stream().map(String::trim).toList();
taskQueues.put(queueName, tasks);
continue;
}
//Format 3: "QueueName task1 task2 task3" (space separated)
Pattern pattern3 = Pattern.compile("^(.*?)\\s+(.*)$");
Matcher matcher3 = pattern3.matcher(line);
if(matcher3.matches()){
String queueName = matcher3.group(1).trim();
String tasksString = matcher3.group(2).trim();
List<String> tasks = tasksString.split("\\s+").stream().map(String::trim).toList();
taskQueues.put(queueName, tasks);
continue;
}
// If no known format is matched, log a warning. Consider adding more parsing logic
System.err.println("Warning: Unrecognized line format: " + line);
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
return new HashMap<>(); // Return empty map on error.
}
return taskQueues;
}
public static void main(String[] args) {
// Example usage
String filePath = "task_queue_data.txt";
Map<String, List<String>> taskQueues = importTaskQueueData(filePath);
for (Map.Entry<String, List<String>> entry : taskQueues.entrySet()) {
System.out.println("Queue: " + entry.getKey());
System.out.println("Tasks: " + entry.getValue());
}
}
}
Add your comment