From 17e79b751a53692b9226369ba53a2354db1728b4 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Sat, 29 Feb 2020 17:05:29 +0100 Subject: [PATCH] Checkstyle --- src/edu/kit/informatik/model/CardGame.java | 22 ++++++------- .../informatik/ui/InvalidInputException.java | 2 +- src/edu/kit/informatik/ui/command/Build.java | 22 +++++++++---- .../kit/informatik/ui/command/Buildable.java | 19 ++++++++--- .../kit/informatik/ui/command/Command.java | 7 ---- .../informatik/ui/command/CommandFactory.java | 31 +++++++++--------- src/edu/kit/informatik/ui/command/Draw.java | 32 +++++++++---------- .../informatik/ui/command/ListBuildings.java | 17 ++++++++-- .../informatik/ui/command/ListResources.java | 17 ++++++++-- src/edu/kit/informatik/ui/command/Reset.java | 17 ++++++++-- .../kit/informatik/ui/command/RollDice.java | 28 +++++++++++----- src/edu/kit/informatik/ui/command/Start.java | 27 +++++++++++----- 12 files changed, 155 insertions(+), 86 deletions(-) diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java index 6892485..5116c96 100644 --- a/src/edu/kit/informatik/model/CardGame.java +++ b/src/edu/kit/informatik/model/CardGame.java @@ -38,13 +38,13 @@ public class CardGame { private Integer minimumDiceRoll = null; /** - * - * @param cardStack order: 1st element = 1st to take + * Start a new game with the specified stack of cards. + * @param cardStack stack of cards to use, where the first element is the first to take * @throws InvalidInputException if card stack has wrong distribution of cards - * @return + * @return whether the game could be successfully started */ public boolean start(Deque cardStack) throws InvalidInputException { - if (this.cardStack == null) { + if (!gameStarted()) { if (Collections.frequency(cardStack, WOOD) != 16 || Collections.frequency(cardStack, PLASTIC) != 16 || Collections.frequency(cardStack, METAL) != 16 || Collections.frequency(cardStack, SPIDER) != 5 || Collections.frequency(cardStack, SNAKE) != 5 || Collections.frequency(cardStack, TIGER) != 5 @@ -65,7 +65,7 @@ public class CardGame { } else if (awaitedDiceSize != null) { throw new InvalidInputException("roll dice, please"); } - Card card = currentCardStack.removeFirst(); + final Card card = currentCardStack.removeFirst(); if (card.isResource()) { resources.add(card); } else if (card.needsDiceRoll()) { @@ -91,10 +91,10 @@ public class CardGame { throw new InvalidInputException("impossible roll"); } awaitedDiceSize = null; - int minimumNeeded = minimumDiceRoll; + final int minimumNeeded = minimumDiceRoll; minimumDiceRoll = null; if (fightingAnimal) { - int bonus = items.contains(AXE) ? 2 : items.contains(CLUB) ? 1 : 0; + final int bonus = items.contains(AXE) ? 2 : items.contains(CLUB) ? 1 : 0; if (roll + bonus >= minimumNeeded) { return "survived"; } else { @@ -113,7 +113,7 @@ public class CardGame { } public void clearResources() { - int keepLastResources = items.contains(SHACK) ? 5 : 0; + final int keepLastResources = items.contains(SHACK) ? 5 : 0; while (resources.size() > keepLastResources) { resources.removeFirst(); } @@ -128,7 +128,7 @@ public class CardGame { throw new InvalidInputException("already built"); } else if (canBuild(item)) { // remove used resources - for (Card resource : item.resourcesNeeded()) { + for (final Card resource : item.resourcesNeeded()) { resources.removeLastOccurrence(resource); } items.add(item); @@ -151,8 +151,8 @@ public class CardGame { } private boolean canBuild(Item item) { - Card[] resourcesNeeded = item.resourcesNeeded(); - for (Card resource : resources) { + final Card[] resourcesNeeded = item.resourcesNeeded(); + for (final Card resource : resources) { for (int j = 0; j < resourcesNeeded.length; j++) { if (resourcesNeeded[j] != null && resourcesNeeded[j] == resource) { resourcesNeeded[j] = null; diff --git a/src/edu/kit/informatik/ui/InvalidInputException.java b/src/edu/kit/informatik/ui/InvalidInputException.java index 8a3c82f..a9bc8e2 100644 --- a/src/edu/kit/informatik/ui/InvalidInputException.java +++ b/src/edu/kit/informatik/ui/InvalidInputException.java @@ -1,6 +1,6 @@ package edu.kit.informatik.ui; -public class InvalidInputException extends Exception { +public class InvalidInputException extends RuntimeException { public InvalidInputException(String message) { super(message); } diff --git a/src/edu/kit/informatik/ui/command/Build.java b/src/edu/kit/informatik/ui/command/Build.java index b3c0a51..b0816d9 100644 --- a/src/edu/kit/informatik/ui/command/Build.java +++ b/src/edu/kit/informatik/ui/command/Build.java @@ -10,10 +10,14 @@ import java.util.regex.Pattern; import static edu.kit.informatik.ui.command.CommandFactory.BUILD; -public class Build extends Command { +public final class Build extends Command { private static final Pattern BUILD_ARGUMENT = Pattern.compile(BUILD + "(\\w+)"); - private Item item; + private final Item item; + + private Build(Item item) { + this.item = item; + } @Override public void apply(CardGame game) throws InvalidInputException { @@ -24,15 +28,21 @@ public class Build extends Command { } } - @Override - public void parse(String input) throws InvalidInputException { + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @return fully initialized command + * @throws InvalidInputException if user input is in any way invalid + */ + public static Command parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(BUILD)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = BUILD_ARGUMENT.matcher(input); + final Matcher matcher = Build.BUILD_ARGUMENT.matcher(input); if (!matcher.matches()) { throw new InvalidInputException("invalid build command"); } - item = Item.parse(matcher.group(1)); + return new Build(Item.parse(matcher.group(1))); } } diff --git a/src/edu/kit/informatik/ui/command/Buildable.java b/src/edu/kit/informatik/ui/command/Buildable.java index 03878e1..1040f51 100644 --- a/src/edu/kit/informatik/ui/command/Buildable.java +++ b/src/edu/kit/informatik/ui/command/Buildable.java @@ -10,10 +10,14 @@ import java.util.Set; import static edu.kit.informatik.ui.command.CommandFactory.BUILDABLE; -public class Buildable extends Command { +public final class Buildable extends Command { + private Buildable() { + + } + @Override public void apply(CardGame game) throws InvalidInputException { - Set buildable = game.getBuildableItems(); + final Set buildable = game.getBuildableItems(); if (buildable.isEmpty()) { Terminal.printLine("EMPTY"); } else { @@ -21,13 +25,20 @@ public class Buildable extends Command { } } - @Override - public void parse(String input) throws InvalidInputException { + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @return fully initialized command + * @throws InvalidInputException if user input is in any way invalid + */ + public static Command parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(BUILDABLE)) { throw new InvalidInputException("unknown command"); } if (!input.equals(BUILDABLE)) { throw new InvalidInputException("invalid build? argument: none expected"); } + return new Buildable(); } } diff --git a/src/edu/kit/informatik/ui/command/Command.java b/src/edu/kit/informatik/ui/command/Command.java index f7e3834..c9ae2ce 100644 --- a/src/edu/kit/informatik/ui/command/Command.java +++ b/src/edu/kit/informatik/ui/command/Command.java @@ -17,11 +17,4 @@ public abstract class Command { * @throws InvalidInputException on invalid user input */ public abstract void apply(CardGame game) throws InvalidInputException; - - /** - * Parse user input into this command object. - * @param input one line of user input - * @throws InvalidInputException if user input is in any way invalid - */ - public abstract void parse(String input) throws InvalidInputException; } diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index 84c0a25..baa6b91 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -4,7 +4,7 @@ import edu.kit.informatik.ui.InvalidInputException; import java.util.HashMap; import java.util.Map; -import java.util.function.Supplier; +import java.util.function.Function; /** * Factory used to parse user input into commands. @@ -24,18 +24,18 @@ public final class CommandFactory { public static final String ROLL_DICE = "rollD"; public static final String RESET = "reset"; - private static final Map> COMMANDS = new HashMap<>(); + private static final Map> COMMANDS = new HashMap<>(); static { // initialize registered commands - COMMANDS.put(START, Start::new); - COMMANDS.put(DRAW, Draw::new); - COMMANDS.put(LIST_RESOURCES, ListResources::new); - COMMANDS.put(BUILD, Build::new); - COMMANDS.put(LIST_BUILDINGS, ListBuildings::new); - COMMANDS.put(BUILDABLE, Buildable::new); - COMMANDS.put(ROLL_DICE, RollDice::new); - COMMANDS.put(RESET, Reset::new); + COMMANDS.put(START, Start::parse); + COMMANDS.put(DRAW, Draw::parse); + COMMANDS.put(LIST_RESOURCES, ListResources::parse); + COMMANDS.put(BUILD, Build::parse); + COMMANDS.put(LIST_BUILDINGS, ListBuildings::parse); + COMMANDS.put(BUILDABLE, Buildable::parse); + COMMANDS.put(ROLL_DICE, RollDice::parse); + COMMANDS.put(RESET, Reset::parse); } /** @@ -52,12 +52,11 @@ public final class CommandFactory { * @throws InvalidInputException if user input is invalid */ public static Command getCommand(String input) throws InvalidInputException { - for (Map.Entry> entry : COMMANDS.entrySet()) { - if (input.startsWith(entry.getKey())) { - Command command = entry.getValue().get(); - command.parse(input); - return command; - } + Function parser = COMMANDS.entrySet().stream() + .filter(entry -> input.startsWith(entry.getKey())).map(Map.Entry::getValue) + .findFirst().orElse(null); + if (parser != null) { + return parser.apply(input); } throw new InvalidInputException("unknown command"); } diff --git a/src/edu/kit/informatik/ui/command/Draw.java b/src/edu/kit/informatik/ui/command/Draw.java index 2f03521..4629882 100644 --- a/src/edu/kit/informatik/ui/command/Draw.java +++ b/src/edu/kit/informatik/ui/command/Draw.java @@ -1,35 +1,35 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; -import edu.kit.informatik.model.Card; import edu.kit.informatik.model.CardGame; import edu.kit.informatik.ui.InvalidInputException; import static edu.kit.informatik.ui.command.CommandFactory.DRAW; -public class Draw extends Command { - @Override - public void apply(CardGame game) throws InvalidInputException { - Card card = game.draw(); - Terminal.printLine(card); +public final class Draw extends Command { + private Draw() { + } @Override - public void parse(String input) throws InvalidInputException { + public void apply(CardGame game) throws InvalidInputException { + Terminal.printLine(game.draw()); + } + + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @return fully initialized command + * @throws InvalidInputException if user input is in any way invalid + */ + public static Command parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(DRAW)) { throw new InvalidInputException("unknown command"); } if (!input.equals(DRAW)) { throw new InvalidInputException("invalid draw argument: none expected"); } + return new Draw(); } - /* - Es existieren folgende Fallunterscheidungen: - •Wenn sich keine Karte mehr auf dem Stapel befindet bzw. der Befehl in einer nicht für - den Befehl vorhergesehenen Spielphase durchgeführt wird, ist dieser Spielzug nicht zulässig. - Entsprechend wird eine Fehlermeldung ausgegeben. - •Zieht der Spieler eine Karte, so wird der Name der Ressource bzw. der englischsprachige - Bezeichner aus den o.g. Kategorien (i) Ressourcen, (ii) Tiere, (iii) Katastrophe ausgegeben. - Entsprechend der gezogenen Karte kann sich die Spielphase ändern. - */ } diff --git a/src/edu/kit/informatik/ui/command/ListBuildings.java b/src/edu/kit/informatik/ui/command/ListBuildings.java index 2d382d1..bcdeb7e 100644 --- a/src/edu/kit/informatik/ui/command/ListBuildings.java +++ b/src/edu/kit/informatik/ui/command/ListBuildings.java @@ -9,7 +9,11 @@ import java.util.List; import static edu.kit.informatik.ui.command.CommandFactory.LIST_BUILDINGS; -public class ListBuildings extends Command { +public final class ListBuildings extends Command { + private ListBuildings() { + + } + @Override public void apply(CardGame game) throws InvalidInputException { List items = game.getItems(); @@ -22,13 +26,20 @@ public class ListBuildings extends Command { } } - @Override - public void parse(String input) throws InvalidInputException { + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @return fully initialized command + * @throws InvalidInputException if user input is in any way invalid + */ + public static Command parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(LIST_BUILDINGS)) { throw new InvalidInputException("unknown command"); } if (!input.equals(LIST_BUILDINGS)) { throw new InvalidInputException("invalid list-buildings argument: none expected"); } + return new ListBuildings(); } } diff --git a/src/edu/kit/informatik/ui/command/ListResources.java b/src/edu/kit/informatik/ui/command/ListResources.java index d5d4d0e..b73bde8 100644 --- a/src/edu/kit/informatik/ui/command/ListResources.java +++ b/src/edu/kit/informatik/ui/command/ListResources.java @@ -9,7 +9,11 @@ import java.util.Deque; import static edu.kit.informatik.ui.command.CommandFactory.LIST_RESOURCES; -public class ListResources extends Command { +public final class ListResources extends Command { + private ListResources() { + + } + @Override public void apply(CardGame game) throws InvalidInputException { Deque resources = game.getResources(); @@ -20,13 +24,20 @@ public class ListResources extends Command { } } - @Override - public void parse(String input) throws InvalidInputException { + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @return fully initialized command + * @throws InvalidInputException if user input is in any way invalid + */ + public static Command parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(LIST_RESOURCES)) { throw new InvalidInputException("unknown command"); } if (!input.equals(LIST_RESOURCES)) { throw new InvalidInputException("invalid list-resources argument: none expected"); } + return new ListResources(); } } diff --git a/src/edu/kit/informatik/ui/command/Reset.java b/src/edu/kit/informatik/ui/command/Reset.java index 9c25bb6..a2c6bf4 100644 --- a/src/edu/kit/informatik/ui/command/Reset.java +++ b/src/edu/kit/informatik/ui/command/Reset.java @@ -6,20 +6,31 @@ import edu.kit.informatik.ui.InvalidInputException; import static edu.kit.informatik.ui.command.CommandFactory.RESET; -public class Reset extends Command { +public final class Reset extends Command { + private Reset() { + + } + @Override public void apply(CardGame game) { game.reset(); Terminal.printLine("OK"); } - @Override - public void parse(String input) throws InvalidInputException { + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @return fully initialized command + * @throws InvalidInputException if user input is in any way invalid + */ + public static Command parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(RESET)) { throw new InvalidInputException("unknown command"); } if (!input.equals(RESET)) { throw new InvalidInputException("invalid reset argument: none expected"); } + return new Reset(); } } diff --git a/src/edu/kit/informatik/ui/command/RollDice.java b/src/edu/kit/informatik/ui/command/RollDice.java index 0bcc092..f5b8739 100644 --- a/src/edu/kit/informatik/ui/command/RollDice.java +++ b/src/edu/kit/informatik/ui/command/RollDice.java @@ -9,28 +9,40 @@ import java.util.regex.Pattern; import static edu.kit.informatik.ui.command.CommandFactory.ROLL_DICE; -public class RollDice extends Command { +public final class RollDice extends Command { private static final Pattern ROLL_DICE_ARGUMENTS = Pattern.compile(ROLL_DICE + "(\\d) (\\d)"); - private int size; + private final int size; - private int rolledNumber; + private final int rolledNumber; + + private RollDice(int size, int rolledNumber) { + this.size = size; + this.rolledNumber = rolledNumber; + } @Override public void apply(CardGame game) throws InvalidInputException { Terminal.printLine(game.rollDice(size, rolledNumber)); } - @Override - public void parse(String input) throws InvalidInputException { + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @return fully initialized command + * @throws InvalidInputException if user input is in any way invalid + */ + public static Command parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(ROLL_DICE)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = ROLL_DICE_ARGUMENTS.matcher(input); + final Matcher matcher = ROLL_DICE_ARGUMENTS.matcher(input); if (!matcher.matches()) { throw new InvalidInputException("invalid roll dice syntax"); } - size = Integer.parseInt(matcher.group(1)); - rolledNumber = Integer.parseInt(matcher.group(2)); + final int size = Integer.parseInt(matcher.group(1)); + final int rolledNumber = Integer.parseInt(matcher.group(2)); + return new RollDice(size, rolledNumber); } } diff --git a/src/edu/kit/informatik/ui/command/Start.java b/src/edu/kit/informatik/ui/command/Start.java index 889ee19..8ff0058 100644 --- a/src/edu/kit/informatik/ui/command/Start.java +++ b/src/edu/kit/informatik/ui/command/Start.java @@ -10,10 +10,14 @@ import java.util.Deque; import java.util.regex.Matcher; import java.util.regex.Pattern; -public class Start extends Command { +public final class Start extends Command { private static final Pattern START_ARGUMENTS = Pattern.compile("start ((\\w+,){63}(\\w+))"); - private Deque cards; + private final Deque cards; + + private Start(Deque cards) { + this.cards = cards; + } @Override public void apply(CardGame game) throws InvalidInputException { @@ -24,23 +28,30 @@ public class Start extends Command { } } - @Override - public void parse(String input) throws InvalidInputException { + /** + * Parse user input into this command object. + * + * @param input one line of user input + * @return fully initialized command + * @throws InvalidInputException if user input is in any way invalid + */ + public static Command parse(String input) throws InvalidInputException { if (input == null || !input.startsWith(CommandFactory.START)) { throw new InvalidInputException("unknown command"); } - Matcher matcher = START_ARGUMENTS.matcher(input); + final Matcher matcher = START_ARGUMENTS.matcher(input); if (!matcher.matches()) { throw new InvalidInputException("invalid start arguments"); } - cards = new ArrayDeque<>(); - for (String s : matcher.group(1).split(",")) { - Card card = Card.parse(s); + final Deque cards = new ArrayDeque<>(); + for (final String s : matcher.group(1).split(",")) { + final Card card = Card.parse(s); if (card == null) { throw new InvalidInputException("invalid start argument value(s)"); } cards.add(card); } + return new Start(cards); } /* Dieser Befehl ermöglicht es dem Benutzer, ein neues Spiel zu starten. Dieser Befehl kann nur