package com.github.dtschust.zork.repl; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.actions.*; import java.util.Arrays; import java.util.List; import java.util.Optional; public class ActionDispatcher { private static final List actionList = List.of( new AddAction(), new AttackAction(), new DeleteAction(), new DropItemAction(), new GameOverAction(), new InventoryAction(), new MoveAction(), new OpenAction(), new PutAction(), new ReadAction(), new StartGameAction(), new TakeAction(), new TurnOnAction(), new UpdateAction() ); private final ZorkGame game; public ActionDispatcher(ZorkGame game) { this.game = game; } private Optional findAction(final List arguments) { if (arguments.isEmpty()) return Optional.empty(); final int size = arguments.size(); return actionList.stream().filter( u -> size >= u.getMinimumArgCount() && size <= u.getMaximumArgCount() && u.matchesInput(arguments) ).findAny(); } public void dispatch(String input) { final List arguments = Arrays.asList(input.split(" ")); final Optional action = findAction(arguments); if (action.isEmpty() || !action.get().run(game, arguments)) { game.stream.println("Error"); } } }