Checkstyle

This commit is contained in:
Arne Keller 2020-02-17 11:00:15 +01:00
parent cdc71c8253
commit 3f1594263e
18 changed files with 173 additions and 26 deletions

View File

@ -253,7 +253,7 @@ public class ModelRailwaySimulation {
* @param id identifier of the rolling stock to find * @param id identifier of the rolling stock to find
* @return the specified rolling stock, or null if not found * @return the specified rolling stock, or null if not found
*/ */
private RollingStock getRollingStock(final String id) { public RollingStock getRollingStock(final String id) {
if (id.matches("W\\+?\\d+")) { if (id.matches("W\\+?\\d+")) {
int coachId = Integer.parseInt(id.substring(1)); int coachId = Integer.parseInt(id.substring(1));
for (Coach coach : coaches) { for (Coach coach : coaches) {

View File

@ -16,6 +16,9 @@ import java.util.Set;
* @version 1.0 * @version 1.0
*/ */
public class RailwayNetwork { public class RailwayNetwork {
/**
* Rail in this railway network. Usually contains tracks and switches.
*/
private List<Rail> rails = new ArrayList<>(); private List<Rail> rails = new ArrayList<>();
/** /**

View File

@ -20,25 +20,51 @@ public abstract class RollingStock {
*/ */
protected boolean couplingBack; protected boolean couplingBack;
/**
* @return identifier of this rolling stock
*/
public abstract String getIdentifier(); public abstract String getIdentifier();
/**
* @return length of this rolling stock
*/
public int getLength() { public int getLength() {
return length; return length;
} }
/**
* @return whether this rolling stock has a front coupling
*/
public boolean hasCouplingFront() { public boolean hasCouplingFront() {
return couplingFront; return couplingFront;
} }
/**
* @return whether this rolling stock has a back coupling
*/
public boolean hasCouplingBack() { public boolean hasCouplingBack() {
return couplingBack; return couplingBack;
} }
/**
* @param rollingStock other rolling stock
* @return whether this rolling stock can couple to the back of the other rolling stock
*/
public abstract boolean canCoupleFrontTo(RollingStock rollingStock); public abstract boolean canCoupleFrontTo(RollingStock rollingStock);
/**
* @param series series/class of train set
* @return whether this rolling stock can couple to train sets of that series
*/
public abstract boolean canCoupleToTrainSetSeries(String series); public abstract boolean canCoupleToTrainSetSeries(String series);
/**
* @return ASCII art representation of this rolling stock
*/
public abstract String[] textRepresentation(); public abstract String[] textRepresentation();
/**
* @return textual description of this rolling stock
*/
public abstract String description(); public abstract String description();
} }

View File

@ -7,6 +7,9 @@ package edu.kit.informatik.model;
* @version 1.0 * @version 1.0
*/ */
public class SteamEngine extends Engine { public class SteamEngine extends Engine {
/**
* ASCII art representation of a steam engine.
*/
private static final String[] STEAM_ENGINE_TEXT = new String[] { private static final String[] STEAM_ENGINE_TEXT = new String[] {
" ++ +------", " ++ +------",
" || |+-+ | ", " || |+-+ | ",

View File

@ -34,15 +34,17 @@ public final class Switch extends Rail {
* @param id identifier of this switch * @param id identifier of this switch
* @throws IllegalArgumentException if the switch is not composed of straight lines * @throws IllegalArgumentException if the switch is not composed of straight lines
*/ */
public Switch(Vector2D start, Vector2D end1, Vector2D end2, int id) throws IllegalArgumentException { public Switch(final Vector2D start, final Vector2D end1, final Vector2D end2, final int id)
throws IllegalArgumentException {
super(id); super(id);
if (start.getX() != end1.getX() && start.getY() != end1.getY() if (start.getX() != end1.getX() && start.getY() != end1.getY()
|| start.getX() != end2.getX() && start.getY() != end2.getY()) { || start.getX() != end2.getX() && start.getY() != end2.getY()) {
throw new IllegalArgumentException("start has to be connected in straight lines to end positions!"); throw new IllegalArgumentException("start has to be connected in straight lines to end positions!");
} }
this.start = start; // make sure caller can not modify internal state after calling
this.end1 = end1; this.start = new Vector2D(start);
this.end2 = end2; this.end1 = new Vector2D(end1);
this.end2 = new Vector2D(end2);
} }
@Override @Override

View File

@ -15,10 +15,25 @@ import java.util.Set;
* @version 1.0 * @version 1.0
*/ */
public final class Train { public final class Train {
/**
* Numerical identifer of this train.
*/
private final int identifier; private final int identifier;
/**
* List of rolling stocks in this train.
*/
private final List<RollingStock> rollingStocks = new ArrayList<>(); private final List<RollingStock> rollingStocks = new ArrayList<>();
/**
* List of positions this train touches.
*/
private List<Vector2D> position; private List<Vector2D> position;
/**
* Direction of the train head.
*/
private Vector2D direction; private Vector2D direction;
/**
* Set of rails this train occupies.
*/
private Set<Rail> occupiedRails; private Set<Rail> occupiedRails;
/** /**
@ -215,7 +230,8 @@ public final class Train {
* @return the direction of this train * @return the direction of this train
*/ */
public Vector2D getDirection() { public Vector2D getDirection() {
return direction; // make sure caller can not modify internal state
return new Vector2D(direction);
} }
/** /**
@ -243,7 +259,8 @@ public final class Train {
* @param nextPosition position to move train to * @param nextPosition position to move train to
*/ */
public void moveTo(final RailwayNetwork railNetwork, final Vector2D nextPosition) { public void moveTo(final RailwayNetwork railNetwork, final Vector2D nextPosition) {
position.add(0, nextPosition); // make sure caller can not modiy internal state
position.add(0, new Vector2D(nextPosition));
Vector2D last = position.remove(position.size() - 1); Vector2D last = position.remove(position.size() - 1);
updateOccupiedRails(railNetwork, position.get(0), last, position.get(position.size() - 1)); updateOccupiedRails(railNetwork, position.get(0), last, position.get(position.size() - 1));
} }
@ -255,8 +272,8 @@ public final class Train {
*/ */
public void moveBackTo(final RailwayNetwork railNetwork, final Vector2D rearPosition) { public void moveBackTo(final RailwayNetwork railNetwork, final Vector2D rearPosition) {
Vector2D last = position.remove(0); Vector2D last = position.remove(0);
position.add(rearPosition); position.add(new Vector2D(rearPosition));
updateOccupiedRails(railNetwork, rearPosition, last, position.get(0)); updateOccupiedRails(railNetwork, position.get(position.size() - 1), last, position.get(0));
} }
/** /**
@ -334,7 +351,7 @@ public final class Train {
* @param other other train * @param other other train
* @return whether this train touches the other train * @return whether this train touches the other train
*/ */
public boolean touches(Train other) { public boolean touches(final Train other) {
return other.isOnAnyPosition(position); return other.isOnAnyPosition(position);
} }
@ -342,7 +359,7 @@ public final class Train {
* @param point position to check * @param point position to check
* @return whether this train is on the specified position * @return whether this train is on the specified position
*/ */
public boolean isOnPosition(Vector2D point) { public boolean isOnPosition(final Vector2D point) {
return position.contains(point); return position.contains(point);
} }
@ -350,7 +367,7 @@ public final class Train {
* @param positions list of positions to check * @param positions list of positions to check
* @return whether this train is on any of the specified positions * @return whether this train is on any of the specified positions
*/ */
public boolean isOnAnyPosition(List<Vector2D> positions) { public boolean isOnAnyPosition(final List<Vector2D> positions) {
return position.stream().anyMatch(positions::contains); return position.stream().anyMatch(positions::contains);
} }

View File

@ -7,6 +7,9 @@ package edu.kit.informatik.model;
* @version 1.0 * @version 1.0
*/ */
public class TrainSet extends RollingStock { public class TrainSet extends RollingStock {
/**
* ASCII art representation of a train set.
*/
private static final String[] TRAIN_SET_TEXT = new String[] { private static final String[] TRAIN_SET_TEXT = new String[] {
" ++ ", " ++ ",
" || ", " || ",
@ -18,7 +21,13 @@ public class TrainSet extends RollingStock {
" (O) (O) " " (O) (O) "
}; };
/**
* Series (class) of this train set.
*/
private String series; private String series;
/**
* Name of this train set. TODO: create NamedRollingStock class
*/
private String name; private String name;
/** /**

View File

@ -23,7 +23,7 @@ public class Vector2D {
* @param x first component * @param x first component
* @param y second component * @param y second component
*/ */
public Vector2D(int x, int y) { public Vector2D(final int x, final int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
@ -89,7 +89,7 @@ public class Vector2D {
* @param other another vector * @param other another vector
* @return the difference of this vector and the other vector * @return the difference of this vector and the other vector
*/ */
public Vector2D subtract(Vector2D other) { public Vector2D subtract(final Vector2D other) {
return new Vector2D(x - other.x, y - other.y); return new Vector2D(x - other.x, y - other.y);
} }
@ -119,7 +119,7 @@ public class Vector2D {
* Change the value of the first component of the vector * Change the value of the first component of the vector
* @param x value to change to * @param x value to change to
*/ */
public void setX(int x) { public void setX(final int x) {
this.x = x; this.x = x;
} }
@ -127,7 +127,7 @@ public class Vector2D {
* Change the value of the second component of the vector * Change the value of the second component of the vector
* @param y value to change to * @param y value to change to
*/ */
public void setY(int y) { public void setY(final int y) {
this.y = y; this.y = y;
} }

View File

@ -11,8 +11,17 @@ import edu.kit.informatik.Terminal;
* @version 1.0 * @version 1.0
*/ */
public class AddSwitch implements Command { public class AddSwitch implements Command {
/**
* Start position of the switch to add.
*/
private final Vector2D start; private final Vector2D start;
/**
* End position 1 of the switch to add.
*/
private final Vector2D end1; private final Vector2D end1;
/**
* End position 2 of the switch to add.
*/
private final Vector2D end2; private final Vector2D end2;
/** /**
@ -22,12 +31,13 @@ public class AddSwitch implements Command {
* @param end2 end position 2 * @param end2 end position 2
*/ */
public AddSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) { public AddSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) {
this.start = start; // make sure caller can not modify internal state
this.end1 = end1; this.start = new Vector2D(start);
this.end2 = end2; this.end1 = new Vector2D(end1);
this.end2 = new Vector2D(end2);
} }
public void apply(ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
int id = simulation.addSwitch(start, end1, end2); int id = simulation.addSwitch(start, end1, end2);
if (id == -1) { if (id == -1) {
Terminal.printError("switch not connected to existing rails"); Terminal.printError("switch not connected to existing rails");

View File

@ -11,7 +11,13 @@ import edu.kit.informatik.Terminal;
* @version 1.0 * @version 1.0
*/ */
public class AddTrack implements Command { public class AddTrack implements Command {
/**
* Start position of the new track.
*/
private Vector2D start; private Vector2D start;
/**
* End position of the new track.
*/
private Vector2D end; private Vector2D end;
/** /**
@ -20,8 +26,9 @@ public class AddTrack implements Command {
* @param end position of the end of the track * @param end position of the end of the track
*/ */
public AddTrack(final Vector2D start, final Vector2D end) { public AddTrack(final Vector2D start, final Vector2D end) {
this.start = start; // make sure caller can not modify internal state
this.end = end; this.start = new Vector2D(start);
this.end = new Vector2D(end);
} }
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {

View File

@ -11,20 +11,26 @@ import edu.kit.informatik.Terminal;
* @version 1.0 * @version 1.0
*/ */
public class AddTrain implements Command { public class AddTrain implements Command {
/**
* Identifier of the train to add or modify.
*/
private final int trainId; private final int trainId;
/**
* Identifier of the rolling stock to add.
*/
private final String rollingStockId; private final String rollingStockId;
/** /**
* Construct a new 'add train' command. * Construct a new 'add train' command.
* @param trainId identifier of the train to be added/modified * @param trainId identifier of the train to be added/modified
* @param rollingStockId identifer of the rolling to be added to the train * @param rollingStockId identifier of the rolling to be added to the train
*/ */
public AddTrain(final int trainId, final String rollingStockId) { public AddTrain(final int trainId, final String rollingStockId) {
this.trainId = trainId; this.trainId = trainId;
this.rollingStockId = rollingStockId; this.rollingStockId = rollingStockId;
} }
public void apply(ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
if (simulation.addTrain(trainId, rollingStockId)) { if (simulation.addTrain(trainId, rollingStockId)) {
RollingStock rollingStock = simulation.getRollingStock(rollingStockId); RollingStock rollingStock = simulation.getRollingStock(rollingStockId);
Terminal.printLine(String.format("%s %s added to train %d", Terminal.printLine(String.format("%s %s added to train %d",

View File

@ -10,5 +10,9 @@ import edu.kit.informatik.model.ModelRailwaySimulation;
* @version 1.0 * @version 1.0
*/ */
public interface Command { public interface Command {
/**
* Apply this command to a model railway simulation.
* @param simulation simulation to use
*/
void apply(ModelRailwaySimulation simulation); void apply(ModelRailwaySimulation simulation);
} }

View File

@ -11,9 +11,21 @@ import edu.kit.informatik.Terminal;
* @version 1.0 * @version 1.0
*/ */
public class CreateCoach implements Command { public class CreateCoach implements Command {
/**
* Type of the new coach.
*/
private final CoachType type; private final CoachType type;
/**
* Length of the new coach.
*/
private final int length; private final int length;
/**
* Whether the new coach should have a front coupling.
*/
private final boolean couplingFront; private final boolean couplingFront;
/**
* Whether the new coach should have a back coupling.
*/
private final boolean couplingBack; private final boolean couplingBack;
/** /**
@ -25,13 +37,16 @@ public class CreateCoach implements Command {
*/ */
public CreateCoach(final CoachType type, final int length, public CreateCoach(final CoachType type, final int length,
final boolean couplingFront, final boolean couplingBack) { final boolean couplingFront, final boolean couplingBack) {
if (type == null) {
throw new IllegalArgumentException("coach type is null");
}
this.type = type; this.type = type;
this.length = length; this.length = length;
this.couplingFront = couplingFront; this.couplingFront = couplingFront;
this.couplingBack = couplingBack; this.couplingBack = couplingBack;
} }
public void apply(ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
int id = simulation.createCoach(type, length, couplingFront, couplingBack); int id = simulation.createCoach(type, length, couplingFront, couplingBack);
if (id == -1) { if (id == -1) {
Terminal.printError("could not add coach"); Terminal.printError("could not add coach");

View File

@ -15,11 +15,29 @@ import edu.kit.informatik.ui.EngineType;
* @version 1.0 * @version 1.0
*/ */
public class CreateEngine implements Command { public class CreateEngine implements Command {
/**
* Type of the new engine.
*/
private final EngineType type; private final EngineType type;
/**
* Series (class) of the new engine.
*/
private final String series; private final String series;
/**
* Name of the new engine.
*/
private final String name; private final String name;
/**
* Length of the new engine.
*/
private final int length; private final int length;
/**
* Whether the new engine should have a front coupling.
*/
private final boolean couplingFront; private final boolean couplingFront;
/**
* Whether the new engine should have a back coupling.
*/
private final boolean couplingBack; private final boolean couplingBack;
/** /**
@ -33,6 +51,9 @@ public class CreateEngine implements Command {
*/ */
public CreateEngine(final EngineType type, final String series, final String name, final int length, public CreateEngine(final EngineType type, final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) { final boolean couplingFront, final boolean couplingBack) {
if (type == null) {
throw new IllegalArgumentException("engine type is null!");
}
this.type = type; this.type = type;
this.series = series; this.series = series;
this.name = name; this.name = name;
@ -42,7 +63,7 @@ public class CreateEngine implements Command {
} }
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) {
final Engine engine; Engine engine;
switch (type) { switch (type) {
case ELECTRICAL: case ELECTRICAL:
engine = new ElectricalEngine(series, name, length, couplingFront, couplingBack); engine = new ElectricalEngine(series, name, length, couplingFront, couplingBack);

View File

@ -11,10 +11,25 @@ import edu.kit.informatik.model.TrainSet;
* @version Arne Keller * @version Arne Keller
*/ */
public class CreateTrainSet implements Command { public class CreateTrainSet implements Command {
/**
* Series (class) of the new train set.
*/
private final String series; private final String series;
/**
* Name of the new train set.
*/
private final String name; private final String name;
/**
* Length of the new train set.
*/
private final int length; private final int length;
/**
* Whether the new train set should have front coupling.
*/
private final boolean couplingFront; private final boolean couplingFront;
/**
* Whether the new train set should have a back coupling.
*/
private final boolean couplingBack; private final boolean couplingBack;
/** /**

View File

@ -10,6 +10,9 @@ import edu.kit.informatik.Terminal;
* @version 1.0 * @version 1.0
*/ */
public class DeleteRollingStock implements Command { public class DeleteRollingStock implements Command {
/**
* Identifier of the rolling stock to delete.
*/
private final String id; private final String id;
/** /**

View File

@ -10,6 +10,9 @@ import edu.kit.informatik.Terminal;
* @version 1.0 * @version 1.0
*/ */
public class DeleteTrack implements Command { public class DeleteTrack implements Command {
/**
* Identifier of the rail to delete.
*/
private final int id; private final int id;
/** /**

View File

@ -10,6 +10,9 @@ import edu.kit.informatik.Terminal;
* @version 1.0 * @version 1.0
*/ */
public class DeleteTrain implements Command { public class DeleteTrain implements Command {
/**
* Identifer of the train to delete.
*/
private final int id; private final int id;
/** /**