diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/Main.java (renamed from src/main/java/edu/brown/cs/student/stars/Main.java) | 26 | ||||
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/repl/Command.java | 19 | ||||
-rw-r--r-- | src/main/java/edu/brown/cs/student/term/repl/REPL.java | 59 |
3 files changed, 87 insertions, 17 deletions
diff --git a/src/main/java/edu/brown/cs/student/stars/Main.java b/src/main/java/edu/brown/cs/student/term/Main.java index 61127b5..c436b2e 100644 --- a/src/main/java/edu/brown/cs/student/stars/Main.java +++ b/src/main/java/edu/brown/cs/student/term/Main.java @@ -1,24 +1,11 @@ -package edu.brown.cs.student.stars; - -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.Map; +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 spark.ExceptionHandler; -import spark.ModelAndView; -import spark.Request; -import spark.Response; -import spark.Spark; -import spark.TemplateViewRoute; -import spark.template.freemarker.FreeMarkerEngine; - -import com.google.common.collect.ImmutableMap; -import freemarker.template.Configuration; +import java.util.HashMap; /** * The Main class of our project. This is where execution begins. @@ -57,6 +44,11 @@ public final class Main { //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!"); + } + } +} |