From c40e635cb8b157005ae3b642ff5e35714a3b3ac6 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Wed, 19 Feb 2020 23:18:14 +0100 Subject: [PATCH] Checkstyle --- src/edu/kit/informatik/model/Rail.java | 4 +++- src/edu/kit/informatik/model/Track.java | 5 ----- src/edu/kit/informatik/ui/CoachType.java | 14 -------------- src/edu/kit/informatik/ui/command/AddSwitch.java | 3 +++ src/edu/kit/informatik/ui/command/AddTrack.java | 3 +++ src/edu/kit/informatik/ui/command/AddTrain.java | 3 +++ src/edu/kit/informatik/ui/command/Command.java | 7 ++++--- .../kit/informatik/ui/command/CommandFactory.java | 3 ++- src/edu/kit/informatik/ui/command/CreateCoach.java | 3 +++ .../kit/informatik/ui/command/CreateEngine.java | 3 +++ .../kit/informatik/ui/command/CreateTrainSet.java | 3 +++ .../informatik/ui/command/DeleteRollingStock.java | 3 +++ src/edu/kit/informatik/ui/command/PutTrain.java | 3 +++ src/edu/kit/informatik/ui/command/SetSwitch.java | 3 +++ 14 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/edu/kit/informatik/model/Rail.java b/src/edu/kit/informatik/model/Rail.java index 5c10338..1415362 100644 --- a/src/edu/kit/informatik/model/Rail.java +++ b/src/edu/kit/informatik/model/Rail.java @@ -50,7 +50,9 @@ public abstract class Rail { * @param position point to connect to * @return whether rail could be successfully set */ - public abstract boolean switchTo(Vector2D position); + public boolean switchTo(Vector2D position) { + return false; + } /** * @return whether the rail is ready for trains running on it diff --git a/src/edu/kit/informatik/model/Track.java b/src/edu/kit/informatik/model/Track.java index 1a7d14f..b9924b7 100644 --- a/src/edu/kit/informatik/model/Track.java +++ b/src/edu/kit/informatik/model/Track.java @@ -51,11 +51,6 @@ public final class Track extends Rail { return rail != null && (rail.canConnectTo(start) || rail.canConnectTo(end)); } - @Override - public boolean switchTo(Vector2D position) { - return false; - } - @Override public boolean isReadyForTrains() { return true; diff --git a/src/edu/kit/informatik/ui/CoachType.java b/src/edu/kit/informatik/ui/CoachType.java index 513372b..17e846a 100644 --- a/src/edu/kit/informatik/ui/CoachType.java +++ b/src/edu/kit/informatik/ui/CoachType.java @@ -37,18 +37,4 @@ public enum CoachType { return null; } } - - @Override - public String toString() { - switch (this) { - case PASSENGER: - return "p"; - case FREIGHT: - return "f"; - case SPECIAL: - return "s"; - default: - throw new IllegalStateException(); - } - } } diff --git a/src/edu/kit/informatik/ui/command/AddSwitch.java b/src/edu/kit/informatik/ui/command/AddSwitch.java index 9c47dc0..e601023 100644 --- a/src/edu/kit/informatik/ui/command/AddSwitch.java +++ b/src/edu/kit/informatik/ui/command/AddSwitch.java @@ -36,6 +36,9 @@ public class AddSwitch extends Command { @Override public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + if (start == null) { + throw new IllegalStateException("command not initialized"); + } int id = simulation.addSwitch(start, end1, end2); if (id == -1) { Terminal.printError("switch not connected to existing rails"); diff --git a/src/edu/kit/informatik/ui/command/AddTrack.java b/src/edu/kit/informatik/ui/command/AddTrack.java index 0a1cf89..8ea0a88 100644 --- a/src/edu/kit/informatik/ui/command/AddTrack.java +++ b/src/edu/kit/informatik/ui/command/AddTrack.java @@ -32,6 +32,9 @@ public class AddTrack extends Command { @Override public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + if (start == null) { + throw new IllegalStateException("command not initialized"); + } int id = simulation.addTrack(start, end); if (id == -1) { Terminal.printError("id space exhausted"); diff --git a/src/edu/kit/informatik/ui/command/AddTrain.java b/src/edu/kit/informatik/ui/command/AddTrain.java index 43762ce..74f3759 100644 --- a/src/edu/kit/informatik/ui/command/AddTrain.java +++ b/src/edu/kit/informatik/ui/command/AddTrain.java @@ -33,6 +33,9 @@ public class AddTrain extends Command { @Override public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + if (rollingStockId == null) { + throw new IllegalStateException("command not initialized"); + } simulation.addTrain(trainId, rollingStockId); RollingStock rollingStock = simulation.getRollingStock(rollingStockId); Terminal.printLine(String.format("%s %s added to train %d", diff --git a/src/edu/kit/informatik/ui/command/Command.java b/src/edu/kit/informatik/ui/command/Command.java index 3adf33f..8346593 100644 --- a/src/edu/kit/informatik/ui/command/Command.java +++ b/src/edu/kit/informatik/ui/command/Command.java @@ -5,7 +5,7 @@ import edu.kit.informatik.ui.InvalidInputException; /** * Command that can be applied to a simulation. - * Commands are implemented as separate classes (to avoid a god enum). + * Commands are implemented as separate classes for easy modification and expansion. * * @author Arne Keller * @version 1.0 @@ -15,11 +15,12 @@ public abstract class Command { * Apply this command to a model railway simulation. * @param simulation simulation to use * @throws InvalidInputException on invalid user input + * @throws IllegalStateException if the command is not yet initialized */ - public abstract void apply(ModelRailwaySimulation simulation) throws InvalidInputException; + public abstract void apply(ModelRailwaySimulation simulation) throws InvalidInputException, IllegalStateException; /** - * Parse user input into the command object. + * Parse user input into this command object. * @param input one line of user input * @throws InvalidInputException if user input is in any way invalid */ diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index 293b944..1969b33 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -103,10 +103,11 @@ public final class CommandFactory { * Name of the step command. */ public static final String STEP = "step"; - + private static final Map> COMMANDS = new HashMap<>(); static { + // initialize registered commands COMMANDS.put(ADD_TRACK, AddTrack::new); COMMANDS.put(ADD_SWITCH, AddSwitch::new); COMMANDS.put(DELETE_TRACK, DeleteTrack::new); diff --git a/src/edu/kit/informatik/ui/command/CreateCoach.java b/src/edu/kit/informatik/ui/command/CreateCoach.java index 66714e7..6e7ea29 100644 --- a/src/edu/kit/informatik/ui/command/CreateCoach.java +++ b/src/edu/kit/informatik/ui/command/CreateCoach.java @@ -40,6 +40,9 @@ public class CreateCoach extends Command { @Override public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + if (type == null) { + throw new IllegalStateException("command not initialized"); + } int id = simulation.createCoach(type, length, couplingFront, couplingBack); if (id == -1) { Terminal.printError("id space exhausted"); diff --git a/src/edu/kit/informatik/ui/command/CreateEngine.java b/src/edu/kit/informatik/ui/command/CreateEngine.java index a9848d8..5ab2b47 100644 --- a/src/edu/kit/informatik/ui/command/CreateEngine.java +++ b/src/edu/kit/informatik/ui/command/CreateEngine.java @@ -54,6 +54,9 @@ public class CreateEngine extends Command { @Override public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + if (type == null) { + throw new IllegalStateException("command not initialized"); + } Engine engine; switch (type) { case ELECTRICAL: diff --git a/src/edu/kit/informatik/ui/command/CreateTrainSet.java b/src/edu/kit/informatik/ui/command/CreateTrainSet.java index 7104064..e5d53f6 100644 --- a/src/edu/kit/informatik/ui/command/CreateTrainSet.java +++ b/src/edu/kit/informatik/ui/command/CreateTrainSet.java @@ -46,6 +46,9 @@ public class CreateTrainSet extends Command { @Override public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + if (name == null) { + throw new IllegalStateException("command not initialized"); + } TrainSet trainSet = new TrainSet(series, name, length, couplingFront, couplingBack); simulation.createTrainSet(trainSet); Terminal.printLine(trainSet.getIdentifier()); diff --git a/src/edu/kit/informatik/ui/command/DeleteRollingStock.java b/src/edu/kit/informatik/ui/command/DeleteRollingStock.java index f75f613..c3259ef 100644 --- a/src/edu/kit/informatik/ui/command/DeleteRollingStock.java +++ b/src/edu/kit/informatik/ui/command/DeleteRollingStock.java @@ -27,6 +27,9 @@ public class DeleteRollingStock extends Command { @Override public void apply(final ModelRailwaySimulation simulation) { + if (id == null) { + throw new IllegalStateException("command not initialized"); + } if (simulation.deleteRollingStock(id)) { Terminal.printLine("OK"); } else { diff --git a/src/edu/kit/informatik/ui/command/PutTrain.java b/src/edu/kit/informatik/ui/command/PutTrain.java index 352d4e7..21ea7e9 100644 --- a/src/edu/kit/informatik/ui/command/PutTrain.java +++ b/src/edu/kit/informatik/ui/command/PutTrain.java @@ -37,6 +37,9 @@ public class PutTrain extends Command { @Override public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException { + if (point == null) { + throw new IllegalStateException("command not initialized"); + } if (simulation.putTrain(id, point, direction)) { Terminal.printLine("OK"); } else { diff --git a/src/edu/kit/informatik/ui/command/SetSwitch.java b/src/edu/kit/informatik/ui/command/SetSwitch.java index d6910f9..dc6eb82 100644 --- a/src/edu/kit/informatik/ui/command/SetSwitch.java +++ b/src/edu/kit/informatik/ui/command/SetSwitch.java @@ -33,6 +33,9 @@ public class SetSwitch extends Command { @Override public void apply(final ModelRailwaySimulation simulation) { + if (point == null) { + throw new IllegalStateException("command not initialized"); + } if (simulation.setSwitch(id, point)) { Terminal.printLine("OK"); } else {