From 6df81726d163da836d9f9684c60298975cd61e66 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 22 Nov 2022 18:36:17 +0100 Subject: [PATCH] Minor cleanup --- REFACTORS.md | 8 ------- .../com/github/dtschust/zork/ZorkTrigger.java | 1 - .../dtschust/zork/objects/ZorkObject.java | 4 ---- .../zork/objects/ZorkObjectTypes.java | 21 ------------------- .../dtschust/zork/objects/ZorkRoom.java | 2 -- .../dtschust/zork/parser/ParserIOC.java | 2 +- .../dtschust/zork/parser/ZorkXMLParser.java | 2 +- .../ZorkContainerParseStrategy.java | 2 +- .../strategies/ZorkCreatureParseStrategy.java | 2 +- .../strategies/ZorkItemParseStrategy.java | 2 +- .../strategies/ZorkRoomParseStrategy.java | 2 +- .../zork/repl/actions/DeleteAction.java | 9 +------- .../dtschust/zork/repl/actions/PutAction.java | 14 ++++++------- .../zork/repl/actions/ReadAction.java | 3 --- .../zork/repl/actions/StartGameAction.java | 4 ++-- .../zork/repl/actions/TakeAction.java | 2 -- .../dtschust/zork/types/ObjectCollector.java | 4 +--- .../com/github/dtschust/zork/ZorkTest.java | 8 +------ 18 files changed, 18 insertions(+), 74 deletions(-) delete mode 100644 REFACTORS.md delete mode 100644 src/main/java/com/github/dtschust/zork/objects/ZorkObjectTypes.java diff --git a/REFACTORS.md b/REFACTORS.md deleted file mode 100644 index 41f6703..0000000 --- a/REFACTORS.md +++ /dev/null @@ -1,8 +0,0 @@ -# Refactors done - -- chore: move to package `com.github.dtschust.zork` - - default package is bad -- remove 5 redundant `if` statements from `ZorkCommand`, `ZorkCommandHas`, `ZorkCondtionStatus' - - e.g. `if (blah) return true; else return false;` can be simplified to `return blah;` -- replaced 2 `for` with foreach loops in `ZorkCreature` and `ZorkTrigger` -- 23 use of diamond operator in `Zork`, `ZorkContainer`, `ZorkCreature`, `ZorkItem`, `ZorkObject`, `ZorkRoom`, `ZorkTrigger` \ No newline at end of file diff --git a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java index ca3ef7d..f7ec787 100644 --- a/src/main/java/com/github/dtschust/zork/ZorkTrigger.java +++ b/src/main/java/com/github/dtschust/zork/ZorkTrigger.java @@ -2,7 +2,6 @@ package com.github.dtschust.zork; import com.github.dtschust.zork.types.HasPrintsAndActions; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Optional; diff --git a/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java b/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java index dfc2cc7..9af4fc5 100644 --- a/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkObject.java @@ -13,10 +13,6 @@ public abstract class ZorkObject { private final List trigger; private String status; - protected ZorkObject(String name, String description) { - this(name, description, null, Collections.emptyList()); - } - protected ZorkObject(final String name, final String description, final String status, diff --git a/src/main/java/com/github/dtschust/zork/objects/ZorkObjectTypes.java b/src/main/java/com/github/dtschust/zork/objects/ZorkObjectTypes.java deleted file mode 100644 index 612e200..0000000 --- a/src/main/java/com/github/dtschust/zork/objects/ZorkObjectTypes.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.github.dtschust.zork.objects; - -import java.util.EnumSet; -import java.util.Optional; - -public enum ZorkObjectTypes { - ROOM("room"), - ITEM("item"), - CONTAINER("container"), - CREATURE("creature"); - - private final String propertyName; - - ZorkObjectTypes(final String propertyName) { - this.propertyName = propertyName; - } - - public static Optional fromPropertyName(final String propertyName) { - return EnumSet.allOf(ZorkObjectTypes.class).stream().filter(e -> e.propertyName.equals(propertyName)).findFirst(); - } -} diff --git a/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java b/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java index 965b46e..2d5e858 100644 --- a/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java +++ b/src/main/java/com/github/dtschust/zork/objects/ZorkRoom.java @@ -8,8 +8,6 @@ import com.github.dtschust.zork.types.ZorkDirection; import java.util.*; -import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; - /* Room*/ public class ZorkRoom extends ZorkObject implements ObjectCollector { private final String type; diff --git a/src/main/java/com/github/dtschust/zork/parser/ParserIOC.java b/src/main/java/com/github/dtschust/zork/parser/ParserIOC.java index 0d09315..19b809f 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ParserIOC.java +++ b/src/main/java/com/github/dtschust/zork/parser/ParserIOC.java @@ -1,11 +1,11 @@ package com.github.dtschust.zork.parser; import com.github.dtschust.zork.ZorkCondition; -import com.github.dtschust.zork.parser.strategies.*; import com.github.dtschust.zork.objects.ZorkContainer; import com.github.dtschust.zork.objects.ZorkCreature; import com.github.dtschust.zork.objects.ZorkItem; import com.github.dtschust.zork.objects.ZorkRoom; +import com.github.dtschust.zork.parser.strategies.*; /** * Inversion of control for Zork parse strategies diff --git a/src/main/java/com/github/dtschust/zork/parser/ZorkXMLParser.java b/src/main/java/com/github/dtschust/zork/parser/ZorkXMLParser.java index f40e03e..a03f7cf 100644 --- a/src/main/java/com/github/dtschust/zork/parser/ZorkXMLParser.java +++ b/src/main/java/com/github/dtschust/zork/parser/ZorkXMLParser.java @@ -1,10 +1,10 @@ package com.github.dtschust.zork.parser; -import com.github.dtschust.zork.parser.dom.DOMElement; import com.github.dtschust.zork.objects.ZorkContainer; import com.github.dtschust.zork.objects.ZorkCreature; import com.github.dtschust.zork.objects.ZorkItem; import com.github.dtschust.zork.objects.ZorkRoom; +import com.github.dtschust.zork.parser.dom.DOMElement; import org.w3c.dom.Element; import javax.xml.parsers.DocumentBuilderFactory; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkContainerParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkContainerParseStrategy.java index b0bc41e..1e79618 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkContainerParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkContainerParseStrategy.java @@ -1,10 +1,10 @@ package com.github.dtschust.zork.parser.strategies; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.objects.ZorkContainer; import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; -import com.github.dtschust.zork.objects.ZorkContainer; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkCreatureParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkCreatureParseStrategy.java index a2ba01c..82d137d 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkCreatureParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkCreatureParseStrategy.java @@ -2,10 +2,10 @@ package com.github.dtschust.zork.parser.strategies; import com.github.dtschust.zork.ZorkCondition; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.objects.ZorkCreature; import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; -import com.github.dtschust.zork.objects.ZorkCreature; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkItemParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkItemParseStrategy.java index 7dfe825..c652197 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkItemParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkItemParseStrategy.java @@ -1,10 +1,10 @@ package com.github.dtschust.zork.parser.strategies; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.objects.ZorkItem; import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; -import com.github.dtschust.zork.objects.ZorkItem; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkRoomParseStrategy.java b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkRoomParseStrategy.java index 2b564f2..ad9f5c1 100644 --- a/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkRoomParseStrategy.java +++ b/src/main/java/com/github/dtschust/zork/parser/strategies/ZorkRoomParseStrategy.java @@ -1,11 +1,11 @@ package com.github.dtschust.zork.parser.strategies; import com.github.dtschust.zork.ZorkTrigger; +import com.github.dtschust.zork.objects.ZorkRoom; import com.github.dtschust.zork.parser.Property; import com.github.dtschust.zork.parser.PropertyParseStrategy; import com.github.dtschust.zork.parser.TriggerPropertyParseStrategy; import com.github.dtschust.zork.types.ZorkDirection; -import com.github.dtschust.zork.objects.ZorkRoom; import java.util.List; import java.util.Map; diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java index 98c7fa7..f725d5b 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/DeleteAction.java @@ -1,17 +1,10 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; -import com.github.dtschust.zork.objects.ZorkItem; -import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.types.ObjectCollector; -import com.github.dtschust.zork.objects.ZorkObjectTypes; -import com.github.dtschust.zork.objects.ZorkObject; import com.github.dtschust.zork.objects.ZorkRoom; +import com.github.dtschust.zork.repl.Action; import java.util.List; -import java.util.Set; - -import static com.github.dtschust.zork.objects.ZorkObjectTypes.*; /** * Delete: figure out what object it is and delete it accordingly. Rooms are especially tricky diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java index 6b2c665..bccd80c 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/PutAction.java @@ -20,12 +20,12 @@ public class PutAction implements Action { public boolean run(ZorkGame game, List arguments) { final String what = arguments.get(1); return game.getContainer(arguments.get(3)) - .filter(c -> c.isOpen() && game.inventory.contains(what)) - .map(tempContainer -> { - tempContainer.addItem(what); - game.inventory.remove(what); - game.stream.println("Item " + what + " added to " + tempContainer.getName() + "."); - return true; - }).orElse(false); + .filter(c -> c.isOpen() && game.inventory.contains(what)) + .map(tempContainer -> { + tempContainer.addItem(what); + game.inventory.remove(what); + game.stream.println("Item " + what + " added to " + tempContainer.getName() + "."); + return true; + }).orElse(false); } } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java index 9643ade..87f8f45 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/ReadAction.java @@ -2,12 +2,9 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.repl.Action; -import com.github.dtschust.zork.objects.ZorkItem; import java.util.List; -import static com.github.dtschust.zork.objects.ZorkObjectTypes.ITEM; - public class ReadAction implements Action { @Override public boolean matchesInput(List arguments) { diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java index ed5f8ee..bc9494e 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/StartGameAction.java @@ -24,8 +24,8 @@ public class StartGameAction implements Action { public boolean run(ZorkGame game, List arguments) { final String room = arguments.get(2); - if(!game.isRunning()){ - if(game.changeRoom(room)){ + if (!game.isRunning()) { + if (game.changeRoom(room)) { game.stream.println(game.getCurrentRoom().getDescription()); return true; } diff --git a/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java b/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java index eacf0b1..f9ef7f1 100644 --- a/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java +++ b/src/main/java/com/github/dtschust/zork/repl/actions/TakeAction.java @@ -2,11 +2,9 @@ package com.github.dtschust.zork.repl.actions; import com.github.dtschust.zork.ZorkGame; import com.github.dtschust.zork.objects.ZorkContainer; -import com.github.dtschust.zork.objects.ZorkItem; import com.github.dtschust.zork.repl.Action; import java.util.List; -import java.util.Optional; public class TakeAction implements Action { @Override diff --git a/src/main/java/com/github/dtschust/zork/types/ObjectCollector.java b/src/main/java/com/github/dtschust/zork/types/ObjectCollector.java index d12336b..2b1a493 100644 --- a/src/main/java/com/github/dtschust/zork/types/ObjectCollector.java +++ b/src/main/java/com/github/dtschust/zork/types/ObjectCollector.java @@ -1,11 +1,9 @@ package com.github.dtschust.zork.types; import com.github.dtschust.zork.objects.ZorkObject; -import com.github.dtschust.zork.objects.ZorkObjectTypes; - -import java.util.Set; public interface ObjectCollector { void addObject(final ZorkObject object); + void removeObject(final ZorkObject object); } diff --git a/src/test/java/com/github/dtschust/zork/ZorkTest.java b/src/test/java/com/github/dtschust/zork/ZorkTest.java index 4eeab52..923f179 100644 --- a/src/test/java/com/github/dtschust/zork/ZorkTest.java +++ b/src/test/java/com/github/dtschust/zork/ZorkTest.java @@ -4,8 +4,6 @@ import com.github.dtschust.zork.utils.CommandReader; import com.github.dtschust.zork.utils.IOWrapper; import org.junit.jupiter.api.Test; -import java.io.PrintStream; - import static com.github.stefanbirkner.systemlambda.SystemLambda.catchSystemExit; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -19,16 +17,12 @@ class ZorkTest { String gameConfig = "sampleGame.xml"; String gameExecution = "RunThroughResults.txt"; - PrintStream out = System.out; - CommandReader run = new CommandReader(gameExecution); IOWrapper io = new IOWrapper(true); new Thread(() -> { try { catchSystemExit(() -> new Zork(gameConfig)); - } catch (Exception ignored) { - ignored.printStackTrace(out); - } + } catch (Exception ignored) {} }).start(); while(true){ switch(run.getInstructionType()) {