Checkstyle

This commit is contained in:
Arne Keller 2020-02-19 18:13:22 +01:00
parent 4a175ee581
commit b17e8db22f
7 changed files with 45 additions and 25 deletions

View File

@ -8,7 +8,14 @@ import edu.kit.informatik.ui.CommandLine;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class Main { public final class Main {
/**
* Utility class -> private constructor.
*/
private Main() {
}
/** /**
* Program entry point. * Program entry point.
* @param args command-line arguments * @param args command-line arguments

View File

@ -402,8 +402,8 @@ public class ModelRailwaySimulation {
* Get collisions of trains currently placed. * Get collisions of trains currently placed.
* @return list of collisions (never null, sometimes empty) * @return list of collisions (never null, sometimes empty)
*/ */
private ArrayList<HashSet<Train>> getStaticCollisions() { private List<HashSet<Train>> getStaticCollisions() {
ArrayList<HashSet<Train>> collisions = new ArrayList<>(); List<HashSet<Train>> collisions = new ArrayList<>();
int maxId = trains.keySet().stream().max(Integer::compareTo).orElse(0); int maxId = trains.keySet().stream().max(Integer::compareTo).orElse(0);
train: for (int id1 = 1; id1 <= maxId; id1++) { train: for (int id1 = 1; id1 <= maxId; id1++) {
Train train1 = trains.get(id1); Train train1 = trains.get(id1);
@ -421,7 +421,7 @@ public class ModelRailwaySimulation {
collision.add(train2); collision.add(train2);
} }
} }
if (collision.size() >= 1) { if (!collision.isEmpty()) {
// check for existing collision // check for existing collision
for (HashSet<Train> otherCollision : collisions) { for (HashSet<Train> otherCollision : collisions) {
if (otherCollision.stream().anyMatch(collision::contains)) { if (otherCollision.stream().anyMatch(collision::contains)) {
@ -441,8 +441,8 @@ public class ModelRailwaySimulation {
* Get collisions of moving the trains one step forward. * Get collisions of moving the trains one step forward.
* @return list of collisions (never null, sometimes empty) * @return list of collisions (never null, sometimes empty)
*/ */
private ArrayList<Set<Train>> getCollisionsOfOneStep() { private List<Set<Train>> getCollisionsOfOneStep() {
ArrayList<Set<Train>> collisions = new ArrayList<>(); List<Set<Train>> collisions = new ArrayList<>();
Map<Train, Set<Rail>> occupiedRails = new HashMap<>(); Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
for (Train train : trains.values()) { for (Train train : trains.values()) {
if (train.isPlaced()) { if (train.isPlaced()) {
@ -456,8 +456,7 @@ public class ModelRailwaySimulation {
Vector2D direction = train.getDirection(); Vector2D direction = train.getDirection();
Vector2D nextPosition = railNetwork.move(position, direction); Vector2D nextPosition = railNetwork.move(position, direction);
if (nextPosition == null if (nextPosition == null
|| (train.isOnPosition(nextPosition) || train.isOnPosition(nextPosition) && !train.getRearPosition().equals(train.getFrontPosition())) {
&& !train.getRearPosition().equals(train.getFrontPosition()))) {
collisions.add(new HashSet<>(Arrays.asList(train))); collisions.add(new HashSet<>(Arrays.asList(train)));
train.removeFromRails(); train.removeFromRails();
nextOccupiedRails.put(train, occupiedRails.get(train)); nextOccupiedRails.put(train, occupiedRails.get(train));
@ -512,8 +511,8 @@ public class ModelRailwaySimulation {
* Get collisions of moving the trains one step backward. * Get collisions of moving the trains one step backward.
* @return list of collisions (never null, sometimes empty) * @return list of collisions (never null, sometimes empty)
*/ */
private ArrayList<Set<Train>> getCollisionsOfOneReverseStep() { private List<Set<Train>> getCollisionsOfOneReverseStep() {
ArrayList<Set<Train>> collisions = new ArrayList<>(); List<Set<Train>> collisions = new ArrayList<>();
Map<Train, Set<Rail>> occupiedRails = new HashMap<>(); Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
for (Train train : trains.values()) { for (Train train : trains.values()) {
if (train.isPlaced()) { if (train.isPlaced()) {
@ -531,7 +530,7 @@ public class ModelRailwaySimulation {
Vector2D direction = train.getRearDirection(); Vector2D direction = train.getRearDirection();
Vector2D nextPosition = railNetwork.move(position, direction); Vector2D nextPosition = railNetwork.move(position, direction);
if (nextPosition == null if (nextPosition == null
|| (train.isOnPosition(nextPosition) && !train.getFrontPosition().equals(nextPosition))) { || train.isOnPosition(nextPosition) && !train.getFrontPosition().equals(nextPosition)) {
collisions.add(new HashSet<>(Arrays.asList(train))); collisions.add(new HashSet<>(Arrays.asList(train)));
train.removeFromRails(); train.removeFromRails();
nextOccupiedRails.put(train, occupiedRails.get(train)); nextOccupiedRails.put(train, occupiedRails.get(train));

View File

@ -103,7 +103,7 @@ public class RailwayNetwork {
* @return whether the rail could be successfully removed * @return whether the rail could be successfully removed
*/ */
public boolean removeRail(final int id) { public boolean removeRail(final int id) {
if (rails.size() == 0) { if (rails.isEmpty()) {
return false; return false;
} }
Rail toRemove = rails.get(id); Rail toRemove = rails.get(id);
@ -223,8 +223,8 @@ public class RailwayNetwork {
return nextPosition; return nextPosition;
} }
Vector2D previousPosition = position.add(direction.negated()); Vector2D previousPosition = position.add(direction.negated());
Rail nextRail = (touchingRails[0].contains(previousPosition) Rail nextRail = touchingRails[0].contains(previousPosition)
|| touchingRails[0].canConnectTo(previousPosition)) ? touchingRails[1] : touchingRails[0]; || touchingRails[0].canConnectTo(previousPosition) ? touchingRails[1] : touchingRails[0];
Vector2D nextDirection = nextRail.getDirectionFrom(position); Vector2D nextDirection = nextRail.getDirectionFrom(position);
if (nextDirection != null) { if (nextDirection != null) {
direction.setX(nextDirection.getX()); direction.setX(nextDirection.getX());

View File

@ -67,12 +67,12 @@ public final class Track extends Rail {
int startY = start.getY(); int startY = start.getY();
int endY = end.getY(); int endY = end.getY();
int positionY = position.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()) { } else if (start.getY() == end.getY() && position.getY() == start.getY()) {
int startX = start.getX(); int startX = start.getX();
int endX = end.getX(); int endX = end.getX();
int positionX = position.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; return false;
} }

View File

@ -25,7 +25,8 @@ public final class Train {
*/ */
private final List<RollingStock> rollingStocks = new ArrayList<>(); private final List<RollingStock> 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<Vector2D> position; private LinkedList<Vector2D> position;
/** /**
@ -161,7 +162,7 @@ public final class Train {
for (int i = 1; i <= length; i++) { for (int i = 1; i <= length; i++) {
rollingStockPosition = railNetwork.move(rollingStockPosition, positioningDirection); rollingStockPosition = railNetwork.move(rollingStockPosition, positioningDirection);
if (rollingStockPosition == null if (rollingStockPosition == null
|| (positions.contains(rollingStockPosition) && !positions.get(0).equals(rollingStockPosition))) { || positions.contains(rollingStockPosition) && !positions.get(0).equals(rollingStockPosition)) {
return false; return false;
} }
positions.addLast(rollingStockPosition); 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! // 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. // therefore, no other rolling stock types should be allowed at all.
// TODO: consider using first.canBeAtFront() ??? // TODO: consider using first.canBeAtFront() ???
return (first instanceof Engine || first instanceof TrainSet) return first instanceof Engine || first instanceof TrainSet || last instanceof Engine;
|| last instanceof Engine;
} }
/** /**
@ -381,6 +381,6 @@ public final class Train {
* @return total length of this train * @return total length of this train
*/ */
public long getLength() { public long getLength() {
return rollingStocks.stream().mapToLong((RollingStock::getLength)).sum(); return rollingStocks.stream().mapToLong(RollingStock::getLength).sum();
} }
} }

View File

@ -11,12 +11,19 @@ import edu.kit.informatik.ui.command.CommandFactory;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class CommandLine { public final class CommandLine {
/** /**
* Command used to exit the simulation and terminate the program. * Command used to exit the simulation and terminate the program.
*/ */
private static final String EXIT = "exit"; 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. * Start a new interactive user session. Returns when standard in is fully processed or user exits.
*/ */

View File

@ -17,7 +17,7 @@ import java.util.regex.Pattern;
public final class CommandFactory { public final class CommandFactory {
private static final String NUMBER = "[+-]?\\d+"; private static final String NUMBER = "[+-]?\\d+";
private static final String VECTOR = NUMBER + "," + NUMBER; 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 private static final String ROLLING_STOCK_IDENTIFIER
= "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|W" + NUMBER; = "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|W" + NUMBER;
@ -58,6 +58,13 @@ public final class CommandFactory {
= Pattern.compile(" (" + NUMBER + ") at \\((" + VECTOR + ")\\) in direction (" + VECTOR + ")"); = Pattern.compile(" (" + NUMBER + ") at \\((" + VECTOR + ")\\) in direction (" + VECTOR + ")");
private static final String STEP = "step"; private static final String STEP = "step";
/**
* Utility class -> private constructor.
*/
private CommandFactory() {
}
/** /**
* Parse a single line of user input into one command. * Parse a single line of user input into one command.
* @param command user input line * @param command user input line
@ -113,7 +120,7 @@ public final class CommandFactory {
} }
EngineType type = EngineType.parse(matcher.group(1)); EngineType type = EngineType.parse(matcher.group(1));
String series = matcher.group(2); String series = matcher.group(2);
if (series.equals("W")) { if ("W".equals(series)) {
throw new InvalidInputException("invalid engine class/series"); throw new InvalidInputException("invalid engine class/series");
} }
String name = matcher.group(3); String name = matcher.group(3);
@ -149,7 +156,7 @@ public final class CommandFactory {
throw new InvalidInputException("invalid create train-set arguments"); throw new InvalidInputException("invalid create train-set arguments");
} }
String series = matcher.group(1); String series = matcher.group(1);
if (series.equals("W")) { if ("W".equals(series)) {
throw new InvalidInputException("invalid train-set class/series"); throw new InvalidInputException("invalid train-set class/series");
} }
String name = matcher.group(2); String name = matcher.group(2);