From f39fdee4c9e9cdbdf682c71a35e1d587e5e32af2 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Wed, 11 Mar 2020 11:19:34 +0100 Subject: [PATCH] Correctly handle common prefixes in command names --- generic_waf2_test_output.txt | 10 +++++----- .../kit/informatik/cardgame/ui/command/Build.java | 4 ++-- .../cardgame/ui/command/CommandFactory.java | 14 ++++++++------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/generic_waf2_test_output.txt b/generic_waf2_test_output.txt index c903259..b284487 100644 --- a/generic_waf2_test_output.txt +++ b/generic_waf2_test_output.txt @@ -1,7 +1,7 @@ Error, no card to draw exists Error, can not get resources: game not started Error, could not build item -Error, unknown command +Error, invalid build command Error, can not get buildings: game not started Error, can not get buildable items: game not started Error, not expecting dice roll @@ -14,7 +14,7 @@ EMPTY wood wood Error, could not build item -Error, unknown command +Error, invalid build command EMPTY EMPTY Error, not expecting dice roll @@ -29,7 +29,7 @@ spider Error, roll dice, please wood Error, awaiting dice roll, can not build -Error, unknown command +Error, invalid build command EMPTY Error, can not get buildable items: awaiting dice roll Error, unexpected dice size @@ -39,7 +39,7 @@ snake Error, roll dice, please wood Error, awaiting dice roll, can not build -Error, unknown command +Error, invalid build command EMPTY Error, can not get buildable items: awaiting dice roll Error, unexpected dice size @@ -49,7 +49,7 @@ tiger Error, roll dice, please wood Error, awaiting dice roll, can not build -Error, unknown command +Error, invalid build command EMPTY Error, can not get buildable items: awaiting dice roll Error, unexpected dice size diff --git a/src/edu/kit/informatik/cardgame/ui/command/Build.java b/src/edu/kit/informatik/cardgame/ui/command/Build.java index b2c7c38..09d52c7 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/Build.java +++ b/src/edu/kit/informatik/cardgame/ui/command/Build.java @@ -12,7 +12,7 @@ import java.util.regex.Pattern; import static edu.kit.informatik.cardgame.ui.command.CommandFactory.BUILD; public final class Build extends Command { - private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)"); + private static final Pattern BUILD_ARGUMENT = Pattern.compile(" (\\w+)"); private Item item; @@ -31,7 +31,7 @@ public final class Build extends Command { if (input == null || !input.startsWith(BUILD)) { throw new InvalidInputException("unknown command"); } - final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input); + final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input.substring(BUILD.length())); if (!matcher.matches()) { throw new InvalidInputException("invalid build command"); } diff --git a/src/edu/kit/informatik/cardgame/ui/command/CommandFactory.java b/src/edu/kit/informatik/cardgame/ui/command/CommandFactory.java index 8fc0cdd..d542d52 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/cardgame/ui/command/CommandFactory.java @@ -2,27 +2,27 @@ package edu.kit.informatik.cardgame.ui.command; import edu.kit.informatik.cardgame.ui.InvalidInputException; -import java.util.HashMap; import java.util.Map; +import java.util.TreeMap; import java.util.function.Supplier; /** * Factory used to parse user input into commands. * * @author Arne Keller - * @version 1.0 + * @version 1.1 */ public final class CommandFactory { public static final String START = "start"; public static final String DRAW = "draw"; public static final String LIST_RESOURCES = "list-resources"; - public static final String BUILD = "build "; + public static final String BUILD = "build"; public static final String LIST_BUILDINGS = "list-buildings"; public static final String BUILDABLE = "build?"; public static final String ROLL_DICE = "rollD"; public static final String RESET = "reset"; - private static final Map> COMMANDS = new HashMap<>(); + private static final TreeMap> COMMANDS = new TreeMap<>(); static { // initialize registered commands @@ -50,8 +50,10 @@ public final class CommandFactory { * @throws InvalidInputException if user input is invalid */ public static Command getCommand(String input) throws InvalidInputException { - Supplier commandSupplier = COMMANDS.entrySet().stream() - .filter(entry -> input.startsWith(entry.getKey())).map(Map.Entry::getValue) + Supplier commandSupplier = COMMANDS + .descendingKeySet().stream() + .filter(input::startsWith) + .map(COMMANDS::get) .findFirst().orElse(null); if (commandSupplier != null) { Command command = commandSupplier.get();