diff --git a/src/edu/kit/informatik/Main.java b/src/edu/kit/informatik/Main.java index 4e63f0c..d3b16c5 100644 --- a/src/edu/kit/informatik/Main.java +++ b/src/edu/kit/informatik/Main.java @@ -8,7 +8,14 @@ import edu.kit.informatik.ui.CommandLine; * @author Arne Keller * @version 1.0 */ -public class Main { +public final class Main { + /** + * Utility class -> private constructor. + */ + private Main() { + + } + /** * Program entry point. * @param args command-line arguments diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index d9fb30f..6638e65 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -402,8 +402,8 @@ public class ModelRailwaySimulation { * Get collisions of trains currently placed. * @return list of collisions (never null, sometimes empty) */ - private ArrayList> getStaticCollisions() { - ArrayList> collisions = new ArrayList<>(); + private List> getStaticCollisions() { + List> collisions = new ArrayList<>(); int maxId = trains.keySet().stream().max(Integer::compareTo).orElse(0); train: for (int id1 = 1; id1 <= maxId; id1++) { Train train1 = trains.get(id1); @@ -421,7 +421,7 @@ public class ModelRailwaySimulation { collision.add(train2); } } - if (collision.size() >= 1) { + if (!collision.isEmpty()) { // check for existing collision for (HashSet otherCollision : collisions) { if (otherCollision.stream().anyMatch(collision::contains)) { @@ -441,8 +441,8 @@ public class ModelRailwaySimulation { * Get collisions of moving the trains one step forward. * @return list of collisions (never null, sometimes empty) */ - private ArrayList> getCollisionsOfOneStep() { - ArrayList> collisions = new ArrayList<>(); + private List> getCollisionsOfOneStep() { + List> collisions = new ArrayList<>(); Map> occupiedRails = new HashMap<>(); for (Train train : trains.values()) { if (train.isPlaced()) { @@ -456,8 +456,7 @@ public class ModelRailwaySimulation { Vector2D direction = train.getDirection(); Vector2D nextPosition = railNetwork.move(position, direction); if (nextPosition == null - || (train.isOnPosition(nextPosition) - && !train.getRearPosition().equals(train.getFrontPosition()))) { + || train.isOnPosition(nextPosition) && !train.getRearPosition().equals(train.getFrontPosition())) { collisions.add(new HashSet<>(Arrays.asList(train))); train.removeFromRails(); nextOccupiedRails.put(train, occupiedRails.get(train)); @@ -512,8 +511,8 @@ public class ModelRailwaySimulation { * Get collisions of moving the trains one step backward. * @return list of collisions (never null, sometimes empty) */ - private ArrayList> getCollisionsOfOneReverseStep() { - ArrayList> collisions = new ArrayList<>(); + private List> getCollisionsOfOneReverseStep() { + List> collisions = new ArrayList<>(); Map> occupiedRails = new HashMap<>(); for (Train train : trains.values()) { if (train.isPlaced()) { @@ -531,7 +530,7 @@ public class ModelRailwaySimulation { Vector2D direction = train.getRearDirection(); Vector2D nextPosition = railNetwork.move(position, direction); if (nextPosition == null - || (train.isOnPosition(nextPosition) && !train.getFrontPosition().equals(nextPosition))) { + || train.isOnPosition(nextPosition) && !train.getFrontPosition().equals(nextPosition)) { collisions.add(new HashSet<>(Arrays.asList(train))); train.removeFromRails(); nextOccupiedRails.put(train, occupiedRails.get(train)); diff --git a/src/edu/kit/informatik/model/RailwayNetwork.java b/src/edu/kit/informatik/model/RailwayNetwork.java index 623e01e..aca5062 100644 --- a/src/edu/kit/informatik/model/RailwayNetwork.java +++ b/src/edu/kit/informatik/model/RailwayNetwork.java @@ -103,7 +103,7 @@ public class RailwayNetwork { * @return whether the rail could be successfully removed */ public boolean removeRail(final int id) { - if (rails.size() == 0) { + if (rails.isEmpty()) { return false; } Rail toRemove = rails.get(id); @@ -223,8 +223,8 @@ public class RailwayNetwork { return nextPosition; } Vector2D previousPosition = position.add(direction.negated()); - Rail nextRail = (touchingRails[0].contains(previousPosition) - || touchingRails[0].canConnectTo(previousPosition)) ? touchingRails[1] : touchingRails[0]; + Rail nextRail = touchingRails[0].contains(previousPosition) + || touchingRails[0].canConnectTo(previousPosition) ? touchingRails[1] : touchingRails[0]; Vector2D nextDirection = nextRail.getDirectionFrom(position); if (nextDirection != null) { direction.setX(nextDirection.getX()); diff --git a/src/edu/kit/informatik/model/Track.java b/src/edu/kit/informatik/model/Track.java index 2c949d1..e784b1d 100644 --- a/src/edu/kit/informatik/model/Track.java +++ b/src/edu/kit/informatik/model/Track.java @@ -67,12 +67,12 @@ public final class Track extends Rail { int startY = start.getY(); int endY = end.getY(); int positionY = position.getY(); - return (startY < positionY && positionY < endY) || (startY > positionY && positionY > endY); + return startY < positionY && positionY < endY || startY > positionY && positionY > endY; } else if (start.getY() == end.getY() && position.getY() == start.getY()) { int startX = start.getX(); int endX = end.getX(); int positionX = position.getX(); - return (startX < positionX && positionX < endX) || (startX > positionX && positionX > endX); + return startX < positionX && positionX < endX || startX > positionX && positionX > endX; } return false; } diff --git a/src/edu/kit/informatik/model/Train.java b/src/edu/kit/informatik/model/Train.java index bde42ed..128436d 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -25,7 +25,8 @@ public final class Train { */ private final List rollingStocks = new ArrayList<>(); /** - * List of positions this train touches. + * List of positions this train touches. This is a linked list because we only have to update the first and last + * positions when moving the train. */ private LinkedList position; /** @@ -161,7 +162,7 @@ public final class Train { for (int i = 1; i <= length; i++) { rollingStockPosition = railNetwork.move(rollingStockPosition, positioningDirection); if (rollingStockPosition == null - || (positions.contains(rollingStockPosition) && !positions.get(0).equals(rollingStockPosition))) { + || positions.contains(rollingStockPosition) && !positions.get(0).equals(rollingStockPosition)) { return false; } positions.addLast(rollingStockPosition); @@ -209,8 +210,7 @@ public final class Train { // the first or last rolling stock HAVE TO BE an engine OR a train-set! // therefore, no other rolling stock types should be allowed at all. // TODO: consider using first.canBeAtFront() ??? - return (first instanceof Engine || first instanceof TrainSet) - || last instanceof Engine; + return first instanceof Engine || first instanceof TrainSet || last instanceof Engine; } /** @@ -381,6 +381,6 @@ public final class Train { * @return total length of this train */ public long getLength() { - return rollingStocks.stream().mapToLong((RollingStock::getLength)).sum(); + return rollingStocks.stream().mapToLong(RollingStock::getLength).sum(); } } diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java index dea367c..8b580f3 100644 --- a/src/edu/kit/informatik/ui/CommandLine.java +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -11,12 +11,19 @@ import edu.kit.informatik.ui.command.CommandFactory; * @author Arne Keller * @version 1.0 */ -public class CommandLine { +public final class CommandLine { /** * Command used to exit the simulation and terminate the program. */ private static final String EXIT = "exit"; + /** + * Utility class -> private constructor. + */ + private CommandLine() { + + } + /** * Start a new interactive user session. Returns when standard in is fully processed or user exits. */ diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index 7cb674c..cf3d687 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -17,7 +17,7 @@ import java.util.regex.Pattern; public final class CommandFactory { private static final String NUMBER = "[+-]?\\d+"; private static final String VECTOR = NUMBER + "," + NUMBER; - private static final String ALPHANUMERIC_WORD = "[\\p{L}\\p{N}]+"; + private static final String ALPHANUMERIC_WORD = "[\\p{L}\\d]+"; private static final String ROLLING_STOCK_IDENTIFIER = "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|W" + NUMBER; @@ -58,6 +58,13 @@ public final class CommandFactory { = Pattern.compile(" (" + NUMBER + ") at \\((" + VECTOR + ")\\) in direction (" + VECTOR + ")"); private static final String STEP = "step"; + /** + * Utility class -> private constructor. + */ + private CommandFactory() { + + } + /** * Parse a single line of user input into one command. * @param command user input line @@ -113,7 +120,7 @@ public final class CommandFactory { } EngineType type = EngineType.parse(matcher.group(1)); String series = matcher.group(2); - if (series.equals("W")) { + if ("W".equals(series)) { throw new InvalidInputException("invalid engine class/series"); } String name = matcher.group(3); @@ -149,7 +156,7 @@ public final class CommandFactory { throw new InvalidInputException("invalid create train-set arguments"); } String series = matcher.group(1); - if (series.equals("W")) { + if ("W".equals(series)) { throw new InvalidInputException("invalid train-set class/series"); } String name = matcher.group(2);