SonarQube

This commit is contained in:
Arne Keller 2020-03-05 22:32:18 +01:00
parent f5706a2edf
commit 8ec474520c
4 changed files with 12 additions and 9 deletions

View File

@ -71,7 +71,7 @@ public class ModelRailwaySimulation {
*/ */
public boolean removeRail(int id) { public boolean removeRail(int id) {
// check whether any trains are on this rail // check whether any trains are on this rail
return railNetwork.getRail(id).isPresent() && !trainManager.anyTrainOnRail(railNetwork.getRail(id).get()) return !railNetwork.getRail(id).map(trainManager::anyTrainOnRail).orElse(true)
&& railNetwork.removeRail(id); && railNetwork.removeRail(id);
} }
@ -250,16 +250,19 @@ public class ModelRailwaySimulation {
/** /**
* Create a new train or add rolling stock to an existing train. * Create a new train or add rolling stock to an existing train.
*
* @param trainId identifier of the train * @param trainId identifier of the train
* @param rollingStockId identifier of the rolling stock * @param rollingStockId identifier of the rolling stock
* @return the added rolling stock
* @throws InvalidInputException if input is incorrect * @throws InvalidInputException if input is incorrect
*/ */
public void addTrain(int trainId, String rollingStockId) throws InvalidInputException { public RollingStock addTrain(int trainId, String rollingStockId) throws InvalidInputException {
final Optional<RollingStock> rollingStock = getRollingStock(rollingStockId); final Optional<RollingStock> rollingStock = getRollingStock(rollingStockId);
if (!rollingStock.isPresent()) { if (!rollingStock.isPresent()) {
throw new InvalidInputException("rolling stock not found"); throw new InvalidInputException("rolling stock not found");
} }
trainManager.addTrain(trainId, rollingStock.get()); trainManager.addTrain(trainId, rollingStock.get());
return rollingStock.get();
} }
/** /**

View File

@ -153,9 +153,9 @@ public final class Train {
length -= distanceToLastPosition; length -= distanceToLastPosition;
final Rail occupiedRail = railNetwork.findRail(positionList.getLast(), rollingStockPosition).get(); final Rail occupiedRail = railNetwork.findRail(positionList.getLast(), rollingStockPosition).get();
if (occupiedRailsSet.contains(occupiedRail) && positionList.size() >= 4) { if (occupiedRailsSet.contains(occupiedRail) && positionList.size() > 4) {
// perhaps a self intersection // perhaps a self intersection
final long occupiedAtFrontOfTrain = positionList.get(0).distanceTo(positionList.get(1)); final long occupiedAtFrontOfTrain = positionList.getFirst().distanceTo(positionList.get(1));
final long occupiedAtBackOfTrain = positionList.getLast() final long occupiedAtBackOfTrain = positionList.getLast()
.distanceTo(positionList.get(positionList.size() - 2)); .distanceTo(positionList.get(positionList.size() - 2));
if (occupiedAtFrontOfTrain + occupiedAtBackOfTrain > occupiedRail.getLength()) { if (occupiedAtFrontOfTrain + occupiedAtBackOfTrain > occupiedRail.getLength()) {
@ -269,6 +269,7 @@ public final class Train {
* @param backPosition position to move back of the train to * @param backPosition position to move back of the train to
*/ */
public void moveBackTo(RailwayNetwork railNetwork, Vector2D backPosition) { public void moveBackTo(RailwayNetwork railNetwork, Vector2D backPosition) {
// TODO: create common method for both directions
final Optional<Rail> railUnderFrontOfTrain = railNetwork.findContainingRail(positions.getFirst()); final Optional<Rail> railUnderFrontOfTrain = railNetwork.findContainingRail(positions.getFirst());
positions.getFirst().subtractInPlace(getDirection()); positions.getFirst().subtractInPlace(getDirection());
if (positions.getFirst().equals(positions.get(1))) { if (positions.getFirst().equals(positions.get(1))) {

View File

@ -32,10 +32,10 @@ public final class CommandLine {
while (true) { while (true) {
final String input = Terminal.readLine(); final String input = Terminal.readLine();
if (input == null) { if (input == null) {
break; // TODO remove break; // FIXME remove
} }
if (input.startsWith("#")) { if (input.startsWith("#")) {
continue; // TODO remove continue; // FIXME remove
} }
if (input.startsWith(EXIT)) { if (input.startsWith(EXIT)) {
@ -43,7 +43,7 @@ public final class CommandLine {
Terminal.printError("input after exit command"); Terminal.printError("input after exit command");
continue; continue;
} else { } else {
break; return;
} }
} }

View File

@ -36,8 +36,7 @@ public class AddTrain extends Command {
if (rollingStockId == null) { if (rollingStockId == null) {
throw new IllegalStateException("command not initialized"); throw new IllegalStateException("command not initialized");
} }
simulation.addTrain(trainId, rollingStockId); final RollingStock rollingStock = simulation.addTrain(trainId, rollingStockId);
final RollingStock rollingStock = simulation.getRollingStock(rollingStockId).get();
Terminal.printLine(String.format("%s %s added to train %d", Terminal.printLine(String.format("%s %s added to train %d",
rollingStock.description(), rollingStock.getIdentifier(), trainId)); rollingStock.description(), rollingStock.getIdentifier(), trainId));
} }