From 684e47e00c2be1196af16d3fabd538512a8decf5 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 9 Mar 2021 11:00:24 +0100 Subject: [PATCH] added command pattern for task handling. --- .../HumanTaskDistributor.java | 13 ++------- .../commands/Command.java | 6 ++++ .../commands/PrepareTasksList.java | 29 +++++++++++++++++++ 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/human_task_distributor/commands/Command.java create mode 100644 src/main/java/nl/andrewlalis/human_task_distributor/commands/PrepareTasksList.java diff --git a/src/main/java/nl/andrewlalis/human_task_distributor/HumanTaskDistributor.java b/src/main/java/nl/andrewlalis/human_task_distributor/HumanTaskDistributor.java index c9694a5..6a9242a 100644 --- a/src/main/java/nl/andrewlalis/human_task_distributor/HumanTaskDistributor.java +++ b/src/main/java/nl/andrewlalis/human_task_distributor/HumanTaskDistributor.java @@ -1,5 +1,6 @@ package nl.andrewlalis.human_task_distributor; +import nl.andrewlalis.human_task_distributor.commands.PrepareTasksList; import org.apache.commons.cli.*; import org.apache.commons.csv.CSVFormat; @@ -14,21 +15,13 @@ public class HumanTaskDistributor { final Options options = getOptions(); CommandLineParser cmdParser = new DefaultParser(); try { + FileParser fileParser = new FileParser(); FileWriter fileWriter = new FileWriter(); CommandLine cmd = cmdParser.parse(options, args); if (cmd.hasOption("ptl")) { String[] values = cmd.getOptionValues("ptl"); - if (values.length != 2) { - throw new IllegalArgumentException("Expected exactly 2 parameters for ptl arg."); - } - String filePath = values[0].trim(); - String regex = values[1].trim(); - Set tasks = fileParser.parseTaskList(filePath, regex); - System.out.println("Read " + tasks.size() + " tasks from file."); - String outFilePath = filePath.replaceFirst("\\..*", ".csv"); - fileWriter.write(tasks, outFilePath); - System.out.println("Wrote tasks to " + outFilePath); + new PrepareTasksList().execute(values); return; } diff --git a/src/main/java/nl/andrewlalis/human_task_distributor/commands/Command.java b/src/main/java/nl/andrewlalis/human_task_distributor/commands/Command.java new file mode 100644 index 0000000..dcaadd8 --- /dev/null +++ b/src/main/java/nl/andrewlalis/human_task_distributor/commands/Command.java @@ -0,0 +1,6 @@ +package nl.andrewlalis.human_task_distributor.commands; + +public interface Command { + + void execute(String[] args); +} diff --git a/src/main/java/nl/andrewlalis/human_task_distributor/commands/PrepareTasksList.java b/src/main/java/nl/andrewlalis/human_task_distributor/commands/PrepareTasksList.java new file mode 100644 index 0000000..6cdb940 --- /dev/null +++ b/src/main/java/nl/andrewlalis/human_task_distributor/commands/PrepareTasksList.java @@ -0,0 +1,29 @@ +package nl.andrewlalis.human_task_distributor.commands; + +import nl.andrewlalis.human_task_distributor.FileParser; +import nl.andrewlalis.human_task_distributor.FileWriter; +import nl.andrewlalis.human_task_distributor.Task; + +import java.io.IOException; +import java.util.Set; + +public class PrepareTasksList implements Command { + @Override + public void execute(String[] args) { + if (args.length != 2) { + throw new IllegalArgumentException("Expected exactly 2 parameters for ptl arg."); + } + String filePath = args[0].trim(); + String regex = args[1].trim(); + Set tasks = new FileParser().parseTaskList(filePath, regex); + System.out.println("Read " + tasks.size() + " tasks from file."); + String outFilePath = filePath.replaceFirst("\\..*", ".csv"); + try { + new FileWriter().write(tasks, outFilePath); + System.out.println("Wrote tasks to " + outFilePath); + } catch (IOException e) { + e.printStackTrace(); + System.err.println("Couldn't write output file."); + } + } +}