Checkstyle

This commit is contained in:
Arne Keller 2020-03-07 13:20:29 +01:00
parent 036847553d
commit 4fd72ebe43
27 changed files with 170 additions and 88 deletions

View File

@ -4,7 +4,7 @@ import edu.kit.informatik.ui.CommandLine;
/** /**
* The main class. * The main class.
* *
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
@ -13,7 +13,7 @@ public final class Main {
* Utility class -> private constructor. * Utility class -> private constructor.
*/ */
private Main() { private Main() {
} }
/** /**

View File

@ -4,6 +4,8 @@ import edu.kit.informatik.ui.InvalidInputException;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static edu.kit.informatik.ui.command.CommandFactory.NUMBER;
/** /**
* A coach. * A coach.
* *
@ -16,21 +18,22 @@ public abstract class Coach extends RollingStock {
*/ */
public static final String IDENTIFIER_PREFIX = "W"; public static final String IDENTIFIER_PREFIX = "W";
/** /**
* Regex pattern to match coach identifiers. * Pattern to match a single coach identifier.
*/ */
public static final Pattern IDENTIFIER_PATTERN = Pattern.compile(IDENTIFIER_PREFIX + "\\+?\\d+"); public static final Pattern IDENTIFIER_PATTERN = Pattern.compile(IDENTIFIER_PREFIX + NUMBER);
/** /**
* Regex pattern to match coach type (passenger, freight, or special). * Pattern to match coach type (passenger, freight, or special).
*/ */
public static final String COACH_TYPE = "passenger|freight|special"; public static final String COACH_TYPE = "passenger|freight|special";
/** /**
* The unique identifier of this coach. * The (unique) identifier of this coach.
*/ */
private final int identifier; private final int identifier;
/** /**
* Construct a new coach. * Construct a new coach.
*
* @param identifier identifier to use * @param identifier identifier to use
* @param length length of coach * @param length length of coach
* @param couplingFront whether the coach should have a front coupling * @param couplingFront whether the coach should have a front coupling
@ -42,6 +45,11 @@ public abstract class Coach extends RollingStock {
this.identifier = identifier; this.identifier = identifier;
} }
/**
* Get the textual identifier of this coach in this format: [PREFIX][NUMERICAL_IDENTIFIER].
*
* @return the textual identifier of this coach
*/
@Override @Override
public String getIdentifier() { public String getIdentifier() {
return String.format("%s%d", IDENTIFIER_PREFIX, identifier); return String.format("%s%d", IDENTIFIER_PREFIX, identifier);
@ -70,6 +78,8 @@ public abstract class Coach extends RollingStock {
} }
/** /**
* Get the type of this coach ("p" for passenger, "f" for freight, "s" for special).
*
* @return abbreviation of the type of this coach * @return abbreviation of the type of this coach
*/ */
public abstract String getType(); public abstract String getType();

View File

@ -12,7 +12,7 @@ public class DieselEngine extends Engine {
/** /**
* ASCII art representation of a diesel engine. * ASCII art representation of a diesel engine.
*/ */
private static final String[] DIESEL_ENGINE_TEXT = new String[] { private static final String[] DIESEL_ENGINE_TEXT = new String[]{
" _____________|____ ", " _____________|____ ",
" /_| ____________ |_\\ ", " /_| ____________ |_\\ ",
"/ |____________| \\", "/ |____________| \\",
@ -23,6 +23,7 @@ public class DieselEngine extends Engine {
/** /**
* Construct a new diesel engine. * Construct a new diesel engine.
*
* @param series series/class of engine * @param series series/class of engine
* @param name name of engine * @param name name of engine
* @param length length of engine * @param length length of engine
@ -36,9 +37,8 @@ public class DieselEngine extends Engine {
} }
@Override @Override
public String toString() { public String getType() {
return String.format("d %s %s %d %b %b", getSeries(), getName(), getLength(), return "d";
hasCouplingFront(), hasCouplingBack());
} }
@Override @Override

View File

@ -12,7 +12,7 @@ public class ElectricalEngine extends Engine {
/** /**
* ASCII art of an electrical engine. * ASCII art of an electrical engine.
*/ */
private static final String[] ELECTRICAL_ENGINE_TEXT = new String[] { private static final String[] ELECTRICAL_ENGINE_TEXT = new String[]{
" ___ ", " ___ ",
" \\ ", " \\ ",
" _______________/__ ", " _______________/__ ",
@ -25,6 +25,7 @@ public class ElectricalEngine extends Engine {
/** /**
* Construct a new electrical engine. * Construct a new electrical engine.
*
* @param series series/class of engine * @param series series/class of engine
* @param name name of engine * @param name name of engine
* @param length length of engine * @param length length of engine
@ -32,15 +33,14 @@ public class ElectricalEngine extends Engine {
* @param couplingBack whether the engine should have a back coupling * @param couplingBack whether the engine should have a back coupling
* @throws InvalidInputException on invalid user input (e.g. zero-sized engine) * @throws InvalidInputException on invalid user input (e.g. zero-sized engine)
*/ */
public ElectricalEngine(String series, String name, int length, public ElectricalEngine(String series, String name, int length, boolean couplingFront, boolean couplingBack)
boolean couplingFront, boolean couplingBack) throws InvalidInputException { throws InvalidInputException {
super(series, name, length, couplingFront, couplingBack); super(series, name, length, couplingFront, couplingBack);
} }
@Override @Override
public String toString() { public String getType() {
return String.format("e %s %s %d %b %b", getSeries(), getName(), getLength(), return "e";
hasCouplingFront(), hasCouplingBack());
} }
@Override @Override

View File

@ -20,6 +20,7 @@ public abstract class Engine extends RollingStock {
/** /**
* Initialize an engine. * Initialize an engine.
*
* @param series series/class of this engine * @param series series/class of this engine
* @param name name of this engine * @param name name of this engine
* @param length length of this engine * @param length length of this engine
@ -27,7 +28,7 @@ public abstract class Engine extends RollingStock {
* @param couplingBack whether this engine should have a back coupling * @param couplingBack whether this engine should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach) * @throws InvalidInputException on invalid user input (zero-sized coach)
*/ */
protected Engine(String series, String name, int length, protected Engine(String series, String name, int length,
boolean couplingFront, boolean couplingBack) throws InvalidInputException { boolean couplingFront, boolean couplingBack) throws InvalidInputException {
super(length, couplingFront, couplingBack); super(length, couplingFront, couplingBack);
this.series = series; this.series = series;
@ -67,4 +68,23 @@ public abstract class Engine extends RollingStock {
public boolean usefulAtBackOfTrain() { public boolean usefulAtBackOfTrain() {
return true; return true;
} }
/**
* Get the type of this engine ("s" for steam, "d" for diesel, "e" for electrical).
*
* @return abbreviation of the type of this engine
*/
public abstract String getType();
/**
* Get the textual representation of this engine in the format:
* [type] [series] [name] [length] [couplingFront] [couplingBack]
*
* @return textual representation of the engine
*/
@Override
public String toString() {
return String.format("%s %s %s %d %b %b", getType(), getSeries(), getName(), getLength(),
hasCouplingFront(), hasCouplingBack());
}
} }

View File

@ -4,7 +4,7 @@ import edu.kit.informatik.ui.InvalidInputException;
/** /**
* A freight coach. * A freight coach.
* *
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
@ -12,7 +12,7 @@ public class FreightCoach extends Coach {
/** /**
* ASCII art representation of a freight coach. * ASCII art representation of a freight coach.
*/ */
private static final String[] FREIGHT_TEXT = new String[] { private static final String[] FREIGHT_TEXT = new String[]{
"| |", "| |",
"| |", "| |",
"| |", "| |",
@ -22,11 +22,12 @@ public class FreightCoach extends Coach {
/** /**
* Construct a new freight coach. * Construct a new freight coach.
* @param identifier identifier to use *
* @param length length of coach * @param identifier identifier to use
* @param length length of coach
* @param couplingFront whether the coach should have a front coupling * @param couplingFront whether the coach should have a front coupling
* @param couplingBack whether the coach should have a back coupling * @param couplingBack whether the coach should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach) * @throws InvalidInputException on invalid user input (e.g. zero-sized coach)
*/ */
public FreightCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) public FreightCoach(int identifier, int length, boolean couplingFront, boolean couplingBack)
throws InvalidInputException { throws InvalidInputException {

View File

@ -45,6 +45,7 @@ public class ModelRailwaySimulation {
/** /**
* Add a new track to the rail network of this simulation. * Add a new track to the rail network of this simulation.
*
* @param start start position of the new track * @param start start position of the new track
* @param end end position of the new track * @param end end position of the new track
* @return the positive identifier of the new track if successful, -1 if none available * @return the positive identifier of the new track if successful, -1 if none available
@ -56,6 +57,7 @@ public class ModelRailwaySimulation {
/** /**
* Add a switch to the rail network of this simulation. * Add a switch to the rail network of this simulation.
*
* @param start start point of the switch * @param start start point of the switch
* @param end1 end position 1 of the switch * @param end1 end position 1 of the switch
* @param end2 end position 2 of the switch * @param end2 end position 2 of the switch
@ -90,6 +92,7 @@ public class ModelRailwaySimulation {
/** /**
* Change the setting of a switch in the rail network. * Change the setting of a switch in the rail network.
*
* @param id identifier of the switch * @param id identifier of the switch
* @param position position to set the switch to * @param position position to set the switch to
* @return whether the switch could be set * @return whether the switch could be set
@ -132,6 +135,7 @@ public class ModelRailwaySimulation {
/** /**
* Add a new coach to the simulation. * Add a new coach to the simulation.
*
* @param coachType type of the coach * @param coachType type of the coach
* @param length length of the coach * @param length length of the coach
* @param couplingFront whether the coach should have a front coupling * @param couplingFront whether the coach should have a front coupling
@ -163,6 +167,7 @@ public class ModelRailwaySimulation {
/** /**
* Calculate the next coach identifier. * Calculate the next coach identifier.
*
* @return the next coach identifier, or -1 if none available * @return the next coach identifier, or -1 if none available
*/ */
private int getNextCoachIdentifier() { private int getNextCoachIdentifier() {
@ -172,7 +177,7 @@ public class ModelRailwaySimulation {
/** /**
* Get a list of coaches (their text representation) added to the simulation. * Get a list of coaches (their text representation) added to the simulation.
* *
* @return list of coaches (never null, sometimes empty) * @return list of coaches (never null, sometimes empty)
*/ */
public List<String> listCoaches() { public List<String> listCoaches() {
@ -189,6 +194,7 @@ public class ModelRailwaySimulation {
/** /**
* Add a new train set to the simulation. * Add a new train set to the simulation.
*
* @param newTrainSet the train set to add * @param newTrainSet the train set to add
* @throws InvalidInputException if the identifier is already used * @throws InvalidInputException if the identifier is already used
*/ */
@ -218,6 +224,7 @@ public class ModelRailwaySimulation {
/** /**
* Delete a rolling stock. * Delete a rolling stock.
*
* @param id identifier of the rolling stock to remove * @param id identifier of the rolling stock to remove
* @return whether the rolling was successfully removed * @return whether the rolling was successfully removed
*/ */
@ -270,6 +277,7 @@ public class ModelRailwaySimulation {
/** /**
* Delete a train. * Delete a train.
*
* @param id identifier of the train * @param id identifier of the train
* @return whether the train could be deleted * @return whether the train could be deleted
*/ */
@ -298,6 +306,7 @@ public class ModelRailwaySimulation {
/** /**
* Get the ASCII art representation of a train. * Get the ASCII art representation of a train.
*
* @param id identifier of the train to show * @param id identifier of the train to show
* @return rows of ASCII art representing this train * @return rows of ASCII art representing this train
* @throws InvalidInputException if train not found * @throws InvalidInputException if train not found
@ -308,6 +317,7 @@ public class ModelRailwaySimulation {
/** /**
* Put a train on the rail network. * Put a train on the rail network.
*
* @param trainId identifier of the train to place * @param trainId identifier of the train to place
* @param position where to place the train * @param position where to place the train
* @param direction direction in which the train should initially go * @param direction direction in which the train should initially go

View File

@ -4,7 +4,7 @@ import edu.kit.informatik.ui.InvalidInputException;
/** /**
* A passenger coach. * A passenger coach.
* *
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
@ -12,7 +12,7 @@ public class PassengerCoach extends Coach {
/** /**
* ASCII art representation of a passenger coach. * ASCII art representation of a passenger coach.
*/ */
private static final String[] PASSENGER_TEXT = new String[] { private static final String[] PASSENGER_TEXT = new String[]{
"____________________", "____________________",
"| ___ ___ ___ ___ |", "| ___ ___ ___ ___ |",
"| |_| |_| |_| |_| |", "| |_| |_| |_| |_| |",
@ -23,11 +23,12 @@ public class PassengerCoach extends Coach {
/** /**
* Construct a new passenger coach. * Construct a new passenger coach.
* @param identifier identifier to use *
* @param length length of coach * @param identifier identifier to use
* @param length length of coach
* @param couplingFront whether the coach should have a front coupling * @param couplingFront whether the coach should have a front coupling
* @param couplingBack whether the coach should have a back coupling * @param couplingBack whether the coach should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach) * @throws InvalidInputException on invalid user input (e.g. zero-sized coach)
*/ */
public PassengerCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) public PassengerCoach(int identifier, int length, boolean couplingFront, boolean couplingBack)
throws InvalidInputException { throws InvalidInputException {

View File

@ -14,6 +14,7 @@ public abstract class Rail {
/** /**
* Initialize a new rail with the specified identifier. * Initialize a new rail with the specified identifier.
*
* @param id the identifier of this rail * @param id the identifier of this rail
*/ */
protected Rail(int id) { protected Rail(int id) {
@ -47,7 +48,7 @@ public abstract class Rail {
public abstract boolean canConnectTo(Vector2D point); public abstract boolean canConnectTo(Vector2D point);
/** /**
* @param rail rail to check for connection * @param rail rail to check for possible connection
* @return whether this rail can connect to the specified rail * @return whether this rail can connect to the specified rail
*/ */
public abstract boolean canConnectToRail(Rail rail); public abstract boolean canConnectToRail(Rail rail);
@ -60,6 +61,7 @@ public abstract class Rail {
/** /**
* Try to set the rail to connect to this position. Obviously only makes sense for switches and similar rails. * Try to set the rail to connect to this position. Obviously only makes sense for switches and similar rails.
*
* @param position point to connect to * @param position point to connect to
* @return whether rail could be successfully set * @return whether rail could be successfully set
*/ */

View File

@ -27,6 +27,7 @@ public class RailwayNetwork {
/** /**
* Add a new track to the rail network. * Add a new track to the rail network.
*
* @param start start position of the new track * @param start start position of the new track
* @param end end position of the new track * @param end end position of the new track
* @return the positive identifier of the new track if successful, -1 if none available * @return the positive identifier of the new track if successful, -1 if none available
@ -63,6 +64,7 @@ public class RailwayNetwork {
/** /**
* Add a switch to the rail network. * Add a switch to the rail network.
*
* @param start start point of the switch * @param start start point of the switch
* @param end1 end position 1 of the switch * @param end1 end position 1 of the switch
* @param end2 end position 2 of the switch * @param end2 end position 2 of the switch
@ -101,6 +103,7 @@ public class RailwayNetwork {
/** /**
* Get the rail with the specified identifier. * Get the rail with the specified identifier.
*
* @param id rail identifier to get * @param id rail identifier to get
* @return the rail * @return the rail
*/ */
@ -110,6 +113,7 @@ public class RailwayNetwork {
/** /**
* Remove the specified rail from the rail network. * Remove the specified rail from the rail network.
*
* @param id identifier of the rail to remove * @param id identifier of the rail to remove
* @return whether the rail could be successfully removed * @return whether the rail could be successfully removed
*/ */
@ -162,6 +166,7 @@ public class RailwayNetwork {
/** /**
* Change the setting of a switch in the rail network. * Change the setting of a switch in the rail network.
*
* @param id identifier of the switch * @param id identifier of the switch
* @param position position to set the switch to * @param position position to set the switch to
* @return whether the switch could be set * @return whether the switch could be set
@ -215,13 +220,13 @@ public class RailwayNetwork {
final Vector2D onRailOne = touchingRails[0].move(position, new Vector2D(direction), steps); final Vector2D onRailOne = touchingRails[0].move(position, new Vector2D(direction), steps);
final Vector2D onRailTwo = touchingRails[1].move(position, new Vector2D(direction), steps); final Vector2D onRailTwo = touchingRails[1].move(position, new Vector2D(direction), steps);
if (position.equals(onRailOne) || onRailOne == null if (position.equals(onRailOne) || onRailOne == null
|| onRailTwo != null && onRailTwo.subtract(position).directionEquals(direction)) { || onRailTwo != null && onRailTwo.subtract(position).directionEquals(direction)) {
// we are moving on rail two // we are moving on rail two
final Vector2D newDirection = touchingRails[1].getDirectionFrom(position); final Vector2D newDirection = touchingRails[1].getDirectionFrom(position);
direction.copyFrom(newDirection); direction.copyFrom(newDirection);
return onRailTwo; return onRailTwo;
} else if (position.equals(onRailTwo) || onRailTwo == null } else if (position.equals(onRailTwo) || onRailTwo == null
|| onRailOne.subtract(position).directionEquals(direction)) { || onRailOne.subtract(position).directionEquals(direction)) {
final Vector2D newDirection = touchingRails[0].getDirectionFrom(position); final Vector2D newDirection = touchingRails[0].getDirectionFrom(position);
direction.copyFrom(newDirection); direction.copyFrom(newDirection);
return onRailOne; return onRailOne;
@ -245,16 +250,16 @@ public class RailwayNetwork {
final Rail[] touchingRails = findTouchingRails(position); final Rail[] touchingRails = findTouchingRails(position);
if (touchingRails.length == 0) { if (touchingRails.length == 0) {
return findContainingRail(position) return findContainingRail(position)
.map(rail -> rail.allowsPlacement(position, direction)) .map(rail -> rail.allowsPlacement(position, direction))
.orElse(false); // containing rail is orthogonal to the requested direction .orElse(false); // containing rail is orthogonal to the requested direction
} else if (touchingRails.length == 1) { } else if (touchingRails.length == 1) {
// rail should not be orthogonal to the requested direction // rail should not be orthogonal to the requested direction
return touchingRails[0].allowsPlacement(position, direction) return touchingRails[0].allowsPlacement(position, direction)
|| touchingRails[0].allowsMovement(position, direction); || touchingRails[0].allowsMovement(position, direction);
} else if (!touchingRails[0].allowsPlacement(position, direction) } else if (!touchingRails[0].allowsPlacement(position, direction)
&& !touchingRails[1].allowsPlacement(position, direction) && !touchingRails[1].allowsPlacement(position, direction)
&& !(touchingRails[0].allowsMovement(position, direction) && !(touchingRails[0].allowsMovement(position, direction)
|| touchingRails[1].allowsMovement(position, direction))) { || touchingRails[1].allowsMovement(position, direction))) {
// rail network does not allow this direction according to // rail network does not allow this direction according to
// https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378 // https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378
return false; return false;
@ -288,6 +293,7 @@ public class RailwayNetwork {
/** /**
* Find all rails that *touch* (not contain) this position. * Find all rails that *touch* (not contain) this position.
*
* @param position the position to check * @param position the position to check
* @return the rail(s) that touch this position * @return the rail(s) that touch this position
*/ */
@ -297,6 +303,7 @@ public class RailwayNetwork {
/** /**
* Check whether the rail network is ready for trains (all switches set, etc.). * Check whether the rail network is ready for trains (all switches set, etc.).
*
* @return whether the rail network is ready * @return whether the rail network is ready
*/ */
public boolean isReadyForTrains() { public boolean isReadyForTrains() {

View File

@ -4,7 +4,7 @@ import edu.kit.informatik.ui.InvalidInputException;
/** /**
* A rolling stock with a specific integer length and couplings. Is usually an engine, train set or coach. * A rolling stock with a specific integer length and couplings. Is usually an engine, train set or coach.
* *
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
@ -24,7 +24,7 @@ public abstract class RollingStock {
/** /**
* Initialize a rolling stock. * Initialize a rolling stock.
* *
* @param length length of this rolling stock * @param length length of this rolling stock
* @param couplingFront whether this rolling stock should have a front coupling * @param couplingFront whether this rolling stock should have a front coupling
* @param couplingBack whether this rolling stock should have a back coupling * @param couplingBack whether this rolling stock should have a back coupling
@ -43,8 +43,6 @@ public abstract class RollingStock {
} }
/** /**
* Get the identifier of this rolling stock.
*
* @return identifier of this rolling stock * @return identifier of this rolling stock
*/ */
public abstract String getIdentifier(); public abstract String getIdentifier();
@ -71,18 +69,21 @@ public abstract class RollingStock {
} }
/** /**
* Check whether this rolling stock can theoretically couple to another rolling stock. This method will not check
* whether the other rolling stock has special coupling requirements.
*
* @param rollingStock other rolling stock * @param rollingStock other rolling stock
* @return whether this rolling stock can couple to the other rolling stock * @return whether this rolling stock can couple to the other rolling stock
*/ */
public abstract boolean canCoupleTo(RollingStock rollingStock); public abstract boolean canCoupleTo(RollingStock rollingStock);
/** /**
* @return whether this rolling stock is useful at the front end of a train * @return whether this rolling stock is useful at the front of a train
*/ */
public abstract boolean usefulAtFrontOfTrain(); public abstract boolean usefulAtFrontOfTrain();
/** /**
* @return whether this rolling stock is useful at the rear end of a train * @return whether this rolling stock is useful at the back of a train
*/ */
public abstract boolean usefulAtBackOfTrain(); public abstract boolean usefulAtBackOfTrain();

View File

@ -4,7 +4,7 @@ import edu.kit.informatik.ui.InvalidInputException;
/** /**
* A special coach, used for e.g. firefighting. * A special coach, used for e.g. firefighting.
* *
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
@ -12,7 +12,7 @@ public class SpecialCoach extends Coach {
/** /**
* ASCII art representation of a special coach. * ASCII art representation of a special coach.
*/ */
private static final String[] SPECIAL_TEXT = new String[] { private static final String[] SPECIAL_TEXT = new String[]{
" ____", " ____",
"/--------------| |", "/--------------| |",
"\\--------------| |", "\\--------------| |",
@ -24,11 +24,12 @@ public class SpecialCoach extends Coach {
/** /**
* Construct a new special coach. * Construct a new special coach.
* @param identifier identifier to use *
* @param length length of coach * @param identifier identifier to use
* @param length length of coach
* @param couplingFront whether the coach should have a front coupling * @param couplingFront whether the coach should have a front coupling
* @param couplingBack whether the coach should have a back coupling * @param couplingBack whether the coach should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach) * @throws InvalidInputException on invalid user input (e.g. zero-sized coach)
*/ */
public SpecialCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) public SpecialCoach(int identifier, int length, boolean couplingFront, boolean couplingBack)
throws InvalidInputException { throws InvalidInputException {

View File

@ -12,7 +12,7 @@ public class SteamEngine extends Engine {
/** /**
* ASCII art representation of a steam 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[]{
" ++ +------", " ++ +------",
" || |+-+ | ", " || |+-+ | ",
" /---------|| | | ", " /---------|| | | ",
@ -23,6 +23,7 @@ public class SteamEngine extends Engine {
/** /**
* Construct a new steam engine. * Construct a new steam engine.
*
* @param series series/class of engine * @param series series/class of engine
* @param name name of engine * @param name name of engine
* @param length length of engine * @param length length of engine
@ -36,9 +37,8 @@ public class SteamEngine extends Engine {
} }
@Override @Override
public String toString() { public String getType() {
return String.format("s %s %s %d %b %b", getSeries(), getName(), getLength(), return "s";
hasCouplingFront(), hasCouplingBack());
} }
@Override @Override

View File

@ -20,7 +20,7 @@ public final class Switch extends Rail {
*/ */
private final Track positionTwo; private final Track positionTwo;
/** /**
* Currently selected position (either one or two). * Currently selected position (either {@link #positionOne} or {@link #positionTwo}).
*/ */
private Track selection; private Track selection;
@ -44,11 +44,19 @@ public final class Switch extends Rail {
return positionOne.canConnectTo(point) || positionTwo.canConnectTo(point); return positionOne.canConnectTo(point) || positionTwo.canConnectTo(point);
} }
/**
* @param point point to check for connection
* @return whether the currently active configuration connects to the position
*/
@Override @Override
public boolean connectsTo(Vector2D point) { public boolean connectsTo(Vector2D point) {
return selection != null && selection.canConnectTo(point); return selection != null && selection.canConnectTo(point);
} }
/**
* @param rail rail to check for possible connection
* @return whether any of the configurations can connect to the position
*/
@Override @Override
public boolean canConnectToRail(Rail rail) { public boolean canConnectToRail(Rail rail) {
return rail.canConnectToRail(positionOne) || rail.canConnectToRail(positionTwo); return rail.canConnectToRail(positionOne) || rail.canConnectToRail(positionTwo);
@ -66,11 +74,18 @@ public final class Switch extends Rail {
return false; return false;
} }
/**
* @return whether this switch is set
*/
@Override @Override
public boolean isReadyForTrains() { public boolean isReadyForTrains() {
return selection != null; return selection != null;
} }
/**
* @param position the point to check
* @return whether the active switch configuration contains that position
*/
@Override @Override
public boolean contains(Vector2D position) { public boolean contains(Vector2D position) {
return selection != null && selection.contains(position); return selection != null && selection.contains(position);
@ -101,6 +116,9 @@ public final class Switch extends Rail {
|| positionTwo.allowsMovement(position, direction); || positionTwo.allowsMovement(position, direction);
} }
/**
* @return length of the current configuration
*/
@Override @Override
public long getLength() { public long getLength() {
return selection.getLength(); return selection.getLength();

View File

@ -33,7 +33,7 @@ public final class Train implements Comparable<Train> {
/** /**
* List of positions this train occupies. More specifically, it only contains: * List of positions this train occupies. More specifically, it only contains:
* the position of the front of the train, any positions on rail connections, the position of the back of the train * the position of the front of the train, any positions on rail connections, the position of the back of the train
* * <p>
* This is a linked list because we only have to update * This is a linked list because we only have to update
* the positions at the start and end of the list when moving the train. * the positions at the start and end of the list when moving the train.
*/ */
@ -71,6 +71,7 @@ public final class Train implements Comparable<Train> {
/** /**
* Add a rolling stock to this train. * Add a rolling stock to this train.
*
* @param rollingStock the rolling stack to add * @param rollingStock the rolling stack to add
* @throws InvalidInputException if the rolling stock could not be added to the train * @throws InvalidInputException if the rolling stock could not be added to the train
*/ */
@ -92,7 +93,7 @@ public final class Train implements Comparable<Train> {
*/ */
public boolean isOnRail(Rail rail) { public boolean isOnRail(Rail rail) {
return isPlaced() return isPlaced()
&& (occupiedRails.stream().anyMatch(blockedRail -> blockedRail == rail) && (occupiedRails.stream().anyMatch(blockedRail -> blockedRail == rail)
|| positions.stream().anyMatch(rail::canConnectTo)); || positions.stream().anyMatch(rail::canConnectTo));
} }
@ -228,7 +229,7 @@ public final class Train implements Comparable<Train> {
positions.getLast().subtractInPlace(getRearDirection()); positions.getLast().subtractInPlace(getRearDirection());
if (positions.getLast().equals(positions.get(positions.size() - 2))) { if (positions.getLast().equals(positions.get(positions.size() - 2))) {
if (!railNetwork.findContainingRail(nextPosition).equals(railUnderBackOfTrain) if (!railNetwork.findContainingRail(nextPosition).equals(railUnderBackOfTrain)
|| nextPosition == null) { || nextPosition == null) {
occupiedRails.remove(railUnderBackOfTrain.orElse(null)); occupiedRails.remove(railUnderBackOfTrain.orElse(null));
} }
positions.removeLast(); positions.removeLast();
@ -254,7 +255,7 @@ public final class Train implements Comparable<Train> {
positions.getFirst().subtractInPlace(getDirection()); positions.getFirst().subtractInPlace(getDirection());
if (positions.getFirst().equals(positions.get(1))) { if (positions.getFirst().equals(positions.get(1))) {
if (!railNetwork.findContainingRail(backPosition).equals(railUnderFrontOfTrain) if (!railNetwork.findContainingRail(backPosition).equals(railUnderFrontOfTrain)
|| backPosition == null) { || backPosition == null) {
occupiedRails.remove(railUnderFrontOfTrain.orElse(null)); occupiedRails.remove(railUnderFrontOfTrain.orElse(null));
} }
positions.removeFirst(); positions.removeFirst();
@ -274,16 +275,16 @@ public final class Train implements Comparable<Train> {
*/ */
private void simplifyPositions(RailwayNetwork railNetwork) { private void simplifyPositions(RailwayNetwork railNetwork) {
if (positions.size() >= 3 if (positions.size() >= 3
&& positions.getFirst().subtract(positions.get(1)).normalized() && positions.getFirst().subtract(positions.get(1)).normalized()
.equals(positions.get(1).subtract(positions.get(2)).normalized()) .equals(positions.get(1).subtract(positions.get(2)).normalized())
&& railNetwork.findTouchingRails(positions.get(1)).length == 0) { && railNetwork.findTouchingRails(positions.get(1)).length == 0) {
positions.remove(1); positions.remove(1);
} }
if (positions.size() >= 3 if (positions.size() >= 3
&& positions.getLast().subtract(positions.get(positions.size() - 2)).normalized() && positions.getLast().subtract(positions.get(positions.size() - 2)).normalized()
.equals(positions.get(positions.size() - 2) .equals(positions.get(positions.size() - 2)
.subtract(positions.get(positions.size() - 3)).normalized()) .subtract(positions.get(positions.size() - 3)).normalized())
&& railNetwork.findTouchingRails(positions.get(positions.size() - 2)).length == 0) { && railNetwork.findTouchingRails(positions.get(positions.size() - 2)).length == 0) {
positions.remove(positions.size() - 2); positions.remove(positions.size() - 2);
} }
} }

View File

@ -21,7 +21,7 @@ import java.util.stream.Stream;
/** /**
* Train manager, used for processing train placements and movement on a rail network. * Train manager, used for processing train placements and movement on a rail network.
* *
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
@ -108,7 +108,7 @@ public final class TrainManager {
*/ */
private int getNextTrainIdentifier() { private int getNextTrainIdentifier() {
return IntStream.rangeClosed(1, Integer.MAX_VALUE) return IntStream.rangeClosed(1, Integer.MAX_VALUE)
.filter(id -> !trains.containsKey(id)).findFirst().orElse(-1); .filter(id -> !trains.containsKey(id)).findFirst().orElse(-1);
} }
/** /**
@ -236,7 +236,7 @@ public final class TrainManager {
occupiedByTrain2.retainAll(occupiedByTrain1); occupiedByTrain2.retainAll(occupiedByTrain1);
if (!occupiedByTrain2.isEmpty()) { if (!occupiedByTrain2.isEmpty()) {
final SortedSet<Train> collision final SortedSet<Train> collision
= Stream.of(train1, train2).collect(Collectors.toCollection(TreeSet::new)); = Stream.of(train1, train2).collect(Collectors.toCollection(TreeSet::new));
addToSetOrAddNew(collisions, collision); addToSetOrAddNew(collisions, collision);
} }
})); }));
@ -348,9 +348,9 @@ public final class TrainManager {
.mapToObj(step -> speed >= 0 ? getCollisionsOfOneStep() : getCollisionsOfOneReverseStep()) .mapToObj(step -> speed >= 0 ? getCollisionsOfOneStep() : getCollisionsOfOneReverseStep())
// replace train references with identifiers to prevent modification by caller // replace train references with identifiers to prevent modification by caller
.flatMap(collisions -> collisions.stream() .flatMap(collisions -> collisions.stream()
.map(collision -> collision.stream().map(Train::getIdentifier) .map(collision -> collision.stream().map(Train::getIdentifier)
.collect(Collectors.toCollection(TreeSet::new)))) .collect(Collectors.toCollection(TreeSet::new))))
.sorted(Comparator.comparing(TreeSet::first)) .sorted(Comparator.comparing(TreeSet::first))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
} }

View File

@ -12,7 +12,7 @@ public class TrainSet extends RollingStock {
/** /**
* ASCII art representation of a train set. * 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[]{
" ++ ", " ++ ",
" || ", " || ",
"_________||_________", "_________||_________",
@ -34,6 +34,7 @@ public class TrainSet extends RollingStock {
/** /**
* Construct a new train set. * Construct a new train set.
*
* @param series series/class of train set * @param series series/class of train set
* @param name name of train set * @param name name of train set
* @param length length of train set * @param length length of train set

View File

@ -20,6 +20,7 @@ public class Vector2D {
/** /**
* Construct a new vector. * Construct a new vector.
*
* @param x first component * @param x first component
* @param y second component * @param y second component
*/ */
@ -30,6 +31,7 @@ public class Vector2D {
/** /**
* Copy a vector, creating a new object. * Copy a vector, creating a new object.
*
* @param other a vector * @param other a vector
*/ */
public Vector2D(Vector2D other) { public Vector2D(Vector2D other) {
@ -93,7 +95,7 @@ public class Vector2D {
*/ */
public boolean directionEquals(Vector2D other) { public boolean directionEquals(Vector2D other) {
return x == 0 && x == other.x && Long.signum(y) == Long.signum(other.y) return x == 0 && x == other.x && Long.signum(y) == Long.signum(other.y)
|| y == 0 && y == other.y && Long.signum(x) == Long.signum(other.x); || y == 0 && y == other.y && Long.signum(x) == Long.signum(other.x);
} }
@ -115,7 +117,7 @@ public class Vector2D {
} }
/** /**
* @return (-1) * this vector * @return (- 1) * this vector
*/ */
public Vector2D negated() { public Vector2D negated() {
return new Vector2D(-x, -y); return new Vector2D(-x, -y);
@ -123,6 +125,7 @@ public class Vector2D {
/** /**
* Subtract another vector from this one. * Subtract another vector from this one.
*
* @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
*/ */

View File

@ -22,6 +22,7 @@ public enum CoachType {
/** /**
* Parse the textual representation of a coach type into the correct enum value. * Parse the textual representation of a coach type into the correct enum value.
*
* @param value coach type as text * @param value coach type as text
* @return coach type as enum, or null if invalid * @return coach type as enum, or null if invalid
*/ */

View File

@ -25,7 +25,7 @@ public final class CommandLine {
* Utility class -> private constructor. * Utility class -> private constructor.
*/ */
private CommandLine() { private CommandLine() {
} }
/** /**

View File

@ -22,6 +22,7 @@ public enum EngineType {
/** /**
* Parse the textual representation of a engine type into the correct enum value. * Parse the textual representation of a engine type into the correct enum value.
*
* @param value engine type as text * @param value engine type as text
* @return engine type as enum, or null if invalid * @return engine type as enum, or null if invalid
*/ */

View File

@ -9,6 +9,7 @@ package edu.kit.informatik.ui;
public class InvalidInputException extends Exception { public class InvalidInputException extends Exception {
/** /**
* Construct a new invalid input exception with a specific message. * Construct a new invalid input exception with a specific message.
*
* @param message the error message * @param message the error message
*/ */
public InvalidInputException(String message) { public InvalidInputException(String message) {

View File

@ -13,6 +13,7 @@ import edu.kit.informatik.ui.InvalidInputException;
public abstract class Command { public abstract class Command {
/** /**
* Apply this command to a model railway simulation. * Apply this command to a model railway simulation.
*
* @param simulation simulation to use * @param simulation simulation to use
* @throws InvalidInputException on invalid user input * @throws InvalidInputException on invalid user input
*/ */
@ -20,6 +21,7 @@ public abstract class Command {
/** /**
* Parse user input into this command object. * Parse user input into this command object.
*
* @param input one line of user input * @param input one line of user input
* @throws InvalidInputException if user input is in any way invalid * @throws InvalidInputException if user input is in any way invalid
*/ */

View File

@ -14,6 +14,10 @@ import java.util.function.Supplier;
* @version 1.0 * @version 1.0
*/ */
public final class CommandFactory { public final class CommandFactory {
/**
* Regex to accept one boolean (true or false).
*/
public static final String BOOL = "true|false";
/** /**
* Regex to match a number, accepting leading plus sign and zeros. * Regex to match a number, accepting leading plus sign and zeros.
*/ */
@ -31,10 +35,6 @@ public final class CommandFactory {
*/ */
public static final String ROLLING_STOCK_IDENTIFIER public static final String ROLLING_STOCK_IDENTIFIER
= "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|" + Coach.IDENTIFIER_PATTERN; = "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|" + Coach.IDENTIFIER_PATTERN;
/**
* Regex to accept one boolean (true or false).
*/
public static final String BOOL = "true|false";
/** /**
* Name of the add track command. * Name of the add track command.
@ -137,11 +137,12 @@ public final class CommandFactory {
* Utility class -> private constructor. * Utility class -> private constructor.
*/ */
private CommandFactory() { private CommandFactory() {
} }
/** /**
* Parse a single line of user input into one command. * Parse a single line of user input into one command.
*
* @param input user input line * @param input user input line
* @return a fully specified command object * @return a fully specified command object
* @throws InvalidInputException if user input is invalid * @throws InvalidInputException if user input is invalid

View File

@ -26,7 +26,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.NUMBER;
*/ */
public class CreateEngine extends Command { public class CreateEngine extends Command {
private static final Pattern CREATE_ENGINE_ARGUMENTS private static final Pattern CREATE_ENGINE_ARGUMENTS
= Pattern.compile(" (electrical|diesel|steam) (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") (" = Pattern.compile(" (electrical|diesel|steam) (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") ("
+ NUMBER + ") (" + BOOL + ") (" + BOOL + ")"); + NUMBER + ") (" + BOOL + ") (" + BOOL + ")");
/** /**

View File

@ -22,7 +22,7 @@ import static edu.kit.informatik.ui.command.CommandFactory.NUMBER;
*/ */
public class CreateTrainSet extends Command { public class CreateTrainSet extends Command {
private static final Pattern CREATE_TRAIN_SET_ARGUMENTS private static final Pattern CREATE_TRAIN_SET_ARGUMENTS
= Pattern.compile(" (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") (" = Pattern.compile(" (" + ALPHANUMERIC_WORD + ") (" + ALPHANUMERIC_WORD + ") ("
+ NUMBER + ") (" + BOOL + ") (" + BOOL + ")"); + NUMBER + ") (" + BOOL + ") (" + BOOL + ")");
/** /**

View File

@ -32,11 +32,11 @@ public class Step extends Command {
} else { } else {
for (final int id : simulation.getTrains().keySet()) { for (final int id : simulation.getTrains().keySet()) {
final SortedSet<Integer> collisionSet = collisions.stream() final SortedSet<Integer> collisionSet = collisions.stream()
.filter(collision -> collision.first() == id) .filter(collision -> collision.first() == id)
.findFirst().orElse(null); .findFirst().orElse(null);
if (collisionSet != null) { // print collision if (collisionSet != null) { // print collision
Terminal.printLine("Crash of train " + String.join(",", Terminal.printLine("Crash of train " + String.join(",",
collisionSet.stream().map(Object::toString).toArray(String[]::new))); collisionSet.stream().map(Object::toString).toArray(String[]::new)));
} else { // or position, if not in collision } else { // or position, if not in collision
final Vector2D position = simulation.getTrainPosition(id); final Vector2D position = simulation.getTrainPosition(id);
if (position != null) { // only print placed trains if (position != null) { // only print placed trains