Store available commands in map

This commit is contained in:
Arne Keller 2020-02-19 23:09:43 +01:00
parent 8803aab42a
commit 931ff805a9

View File

@ -2,6 +2,10 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
/**
* Factory used to parse user input into commands.
*
@ -100,6 +104,29 @@ public final class CommandFactory {
*/
public static final String STEP = "step";
private static final Map<String, Supplier<Command>> COMMANDS = new HashMap<>();
static {
COMMANDS.put(ADD_TRACK, AddTrack::new);
COMMANDS.put(ADD_SWITCH, AddSwitch::new);
COMMANDS.put(DELETE_TRACK, DeleteTrack::new);
COMMANDS.put(LIST_TRACKS, ListTracks::new);
COMMANDS.put(SET_SWITCH, SetSwitch::new);
COMMANDS.put(CREATE_ENGINE, CreateEngine::new);
COMMANDS.put(LIST_ENGINES, ListEngines::new);
COMMANDS.put(CREATE_COACH, CreateCoach::new);
COMMANDS.put(LIST_COACHES, ListCoaches::new);
COMMANDS.put(CREATE_TRAIN_SET, CreateTrainSet::new);
COMMANDS.put(LIST_TRAIN_SETS, ListTrainSets::new);
COMMANDS.put(DELETE_ROLLING_STOCK, DeleteRollingStock::new);
COMMANDS.put(ADD_TRAIN, AddTrain::new);
COMMANDS.put(DELETE_TRAIN, DeleteTrain::new);
COMMANDS.put(LIST_TRAINS, ListTrains::new);
COMMANDS.put(SHOW_TRAIN, ShowTrain::new);
COMMANDS.put(PUT_TRAIN, PutTrain::new);
COMMANDS.put(STEP, Step::new);
}
/**
* Utility class -> private constructor.
*/
@ -109,52 +136,18 @@ public final class CommandFactory {
/**
* Parse a single line of user input into one command.
* @param command user input line
* @param input user input line
* @return a fully specified command object
* @throws InvalidInputException if user input is invalid
*/
public static Command getCommand(final String command) throws InvalidInputException {
Command commandObject;
if (command.startsWith(ADD_TRACK)) {
commandObject = new AddTrack();
} else if (command.startsWith(ADD_SWITCH)) {
commandObject = new AddSwitch();
} else if (command.startsWith(DELETE_TRACK)) {
commandObject = new DeleteTrack();
} else if (command.startsWith(LIST_TRACKS)) {
commandObject = new ListTracks();
} else if (command.startsWith(SET_SWITCH)) {
commandObject = new SetSwitch();
} else if (command.startsWith(CREATE_ENGINE)) {
commandObject = new CreateEngine();
} else if (command.startsWith(LIST_ENGINES)) {
commandObject = new ListEngines();
} else if (command.startsWith(CREATE_COACH)) {
commandObject = new CreateCoach();
} else if (command.startsWith(LIST_COACHES)) {
commandObject = new ListCoaches();
} else if (command.startsWith(CREATE_TRAIN_SET)) {
commandObject = new CreateTrainSet();
} else if (command.startsWith(LIST_TRAIN_SETS)) {
commandObject = new ListTrainSets();
} else if (command.startsWith(DELETE_ROLLING_STOCK)) {
commandObject = new DeleteRollingStock();
} else if (command.startsWith(ADD_TRAIN)) {
commandObject = new AddTrain();
} else if (command.startsWith(DELETE_TRAIN)) {
commandObject = new DeleteTrain();
} else if (command.startsWith(LIST_TRAINS)) {
commandObject = new ListTrains();
} else if (command.startsWith(SHOW_TRAIN)) {
commandObject = new ShowTrain();
} else if (command.startsWith(PUT_TRAIN)) {
commandObject = new PutTrain();
} else if (command.startsWith(STEP)) {
commandObject = new Step();
} else {
throw new InvalidInputException("unknown command");
public static Command getCommand(String input) throws InvalidInputException {
for (Map.Entry<String, Supplier<Command>> entry : COMMANDS.entrySet()) {
if (input.startsWith(entry.getKey())) {
Command command = entry.getValue().get();
command.parse(input);
return command;
}
}
commandObject.parse(command);
return commandObject;
throw new InvalidInputException("unknown command");
}
}