aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/edu/brown/cs/student/term
diff options
context:
space:
mode:
authorJulia McCauley <skurvyj@gmail.com>2021-03-31 14:58:57 -0400
committerJulia McCauley <skurvyj@gmail.com>2021-03-31 14:58:57 -0400
commitcfd69d5a555bc4d5e7df21d266454a13ac617531 (patch)
treebf586cfbe47af9125b7371f07b9e0aeecdfe9722 /src/main/java/edu/brown/cs/student/term
parentc6d3379bc7b10fb429857c2ec475f541589b8e40 (diff)
set up basic repl/command structure for ease of testing backend
Diffstat (limited to 'src/main/java/edu/brown/cs/student/term')
-rw-r--r--src/main/java/edu/brown/cs/student/term/Main.java54
-rw-r--r--src/main/java/edu/brown/cs/student/term/repl/Command.java19
-rw-r--r--src/main/java/edu/brown/cs/student/term/repl/REPL.java59
3 files changed, 132 insertions, 0 deletions
diff --git a/src/main/java/edu/brown/cs/student/term/Main.java b/src/main/java/edu/brown/cs/student/term/Main.java
new file mode 100644
index 0000000..c436b2e
--- /dev/null
+++ b/src/main/java/edu/brown/cs/student/term/Main.java
@@ -0,0 +1,54 @@
+package edu.brown.cs.student.term;
+
+import edu.brown.cs.student.term.repl.Command;
+import edu.brown.cs.student.term.repl.REPL;
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+
+import java.util.HashMap;
+
+/**
+ * The Main class of our project. This is where execution begins.
+ *
+ */
+public final class Main {
+
+ private static final int DEFAULT_PORT = 4567;
+
+ /**
+ * The initial method called when execution begins.
+ *
+ * @param args
+ * An array of command line arguments
+ */
+ public static void main(String[] args) {
+ new Main(args).run();
+ }
+
+ private String[] args;
+
+ private Main(String[] args) {
+ this.args = args;
+ }
+
+ private void run() {
+ // Parse command line arguments
+ OptionParser parser = new OptionParser();
+ parser.accepts("gui");
+ parser.accepts("port").withRequiredArg().ofType(Integer.class)
+ .defaultsTo(DEFAULT_PORT);
+ OptionSet options = parser.parse(args);
+
+ if (options.has("gui")) {
+ //do a gui type thing
+ //runSparkServer((int) options.valueOf("port"));
+ }
+
+ HashMap<String, Command> commandHashMap = new HashMap<>();
+ /** add commands to map here! */
+ REPL repl = new REPL(commandHashMap);
+ repl.runREPL();
+
+ // TODO: Process commands in a REPL
+ }
+} \ No newline at end of file
diff --git a/src/main/java/edu/brown/cs/student/term/repl/Command.java b/src/main/java/edu/brown/cs/student/term/repl/Command.java
new file mode 100644
index 0000000..f8b0b04
--- /dev/null
+++ b/src/main/java/edu/brown/cs/student/term/repl/Command.java
@@ -0,0 +1,19 @@
+package edu.brown.cs.student.term.repl;
+
+/**
+ * Interface implemented by all five StarsCommand commands.
+ * Implemented by StarsCommand, NaiveNeighbors, NaiveRadiusCommand, RadiusCommand, NeighborsCommand
+ */
+public interface Command {
+ /**
+ * Main run method for every command.
+ * @param args arguments for the command
+ */
+ String run(String[] args);
+
+ /**
+ * Used to print command output to GUI.
+ * @return String representing neighbors found by NeighborsCommand and RadiusCommand commands
+ */
+ String toString();
+}
diff --git a/src/main/java/edu/brown/cs/student/term/repl/REPL.java b/src/main/java/edu/brown/cs/student/term/repl/REPL.java
new file mode 100644
index 0000000..0be7e3f
--- /dev/null
+++ b/src/main/java/edu/brown/cs/student/term/repl/REPL.java
@@ -0,0 +1,59 @@
+package edu.brown.cs.student.term.repl;
+import edu.brown.cs.student.term.repl.Command;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * REPL Class maps input to proper class and function for that command.
+ */
+public class REPL {
+
+ private Map<String, Command> commands;
+
+ /**
+ * Constructor for a REPL object.
+ */
+ public REPL(HashMap<String, Command> userCommands) {
+ commands = userCommands;
+ }
+
+ /**
+ * Gets the current map of commands the REPL supports.
+ * @return the command map
+ */
+ public Map<String, Command> getReplCommandMap() {
+ return commands; }
+
+ /**
+ * Reads user input, maps it to the correct command, and continues.
+ */
+ public void runREPL() {
+ BufferedReader commandHandler = new BufferedReader(new InputStreamReader(System.in));
+ try {
+ String command = commandHandler.readLine();
+ while (command != null) {
+ String[] commandPieces = command.split("\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
+
+ if (commands.containsKey(commandPieces[0])) {
+ Command desiredCommand = commands.get(commandPieces[0]);
+
+ String[] arguments = Arrays.copyOfRange(commandPieces, 1, commandPieces.length);
+
+ desiredCommand.run(arguments);
+ } else {
+ System.out.println("ERROR: Sorry, command not recognized! Please try again.");
+ }
+
+ command = commandHandler.readLine();
+ }
+ } catch (IOException e) {
+ //might wanna change this later
+ System.out.println("ERROR: Could not locate specified file!");
+ }
+ }
+}