1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. public class TaskQueueImporter {
  9. /**
  10. * Imports task queue data from a file. Supports multiple formats (e.g., simple text, CSV).
  11. *
  12. * @param filePath The path to the task queue data file.
  13. * @return A map where keys are queue names and values are lists of tasks. Returns an empty map on error.
  14. */
  15. public static Map<String, List<String>> importTaskQueueData(String filePath) {
  16. Map<String, List<String>> taskQueues = new HashMap<>();
  17. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  18. String line;
  19. while ((line = reader.readLine()) != null) {
  20. line = line.trim(); // Remove leading/trailing whitespace
  21. if (line.isEmpty()) {
  22. continue; // Skip empty lines
  23. }
  24. // Attempt to parse the line based on common formats. Adjust regex as needed.
  25. // Format 1: "QueueName: task1,task2,task3"
  26. Pattern pattern1 = Pattern.compile("^(.*?): (.*)$");
  27. Matcher matcher1 = pattern1.matcher(line);
  28. if (matcher1.matches()) {
  29. String queueName = matcher1.group(1).trim();
  30. String tasksString = matcher1.group(2).trim();
  31. List<String> tasks = tasksString.split(",").stream().map(String::trim).toList();
  32. taskQueues.put(queueName, tasks);
  33. continue;
  34. }
  35. // Format 2: "QueueName - task1;task2;task3"
  36. Pattern pattern2 = Pattern.compile("^(.*?)( - )(.*)$");
  37. Matcher matcher2 = pattern2.matcher(line);
  38. if (matcher2.matches()) {
  39. String queueName = matcher2.group(1).trim();
  40. String tasksString = matcher2.group(3).trim();
  41. List<String> tasks = tasksString.split(";").stream().map(String::trim).toList();
  42. taskQueues.put(queueName, tasks);
  43. continue;
  44. }
  45. //Format 3: "QueueName task1 task2 task3" (space separated)
  46. Pattern pattern3 = Pattern.compile("^(.*?)\\s+(.*)$");
  47. Matcher matcher3 = pattern3.matcher(line);
  48. if(matcher3.matches()){
  49. String queueName = matcher3.group(1).trim();
  50. String tasksString = matcher3.group(2).trim();
  51. List<String> tasks = tasksString.split("\\s+").stream().map(String::trim).toList();
  52. taskQueues.put(queueName, tasks);
  53. continue;
  54. }
  55. // If no known format is matched, log a warning. Consider adding more parsing logic
  56. System.err.println("Warning: Unrecognized line format: " + line);
  57. }
  58. } catch (IOException e) {
  59. System.err.println("Error reading file: " + e.getMessage());
  60. return new HashMap<>(); // Return empty map on error.
  61. }
  62. return taskQueues;
  63. }
  64. public static void main(String[] args) {
  65. // Example usage
  66. String filePath = "task_queue_data.txt";
  67. Map<String, List<String>> taskQueues = importTaskQueueData(filePath);
  68. for (Map.Entry<String, List<String>> entry : taskQueues.entrySet()) {
  69. System.out.println("Queue: " + entry.getKey());
  70. System.out.println("Tasks: " + entry.getValue());
  71. }
  72. }
  73. }

Add your comment