SonarQube

This commit is contained in:
Arne Keller 2020-02-20 09:31:20 +01:00
parent 72bea80f9b
commit 2f8f79a7a1
2 changed files with 27 additions and 45 deletions

View File

@ -76,10 +76,7 @@ public class ModelRailwaySimulation {
*/
public boolean removeRail(final int id) {
// check whether any trains are on this rail
if (trains.values().stream().anyMatch(train -> train.isPlaced() && train.isOnRail(id))) {
return false;
}
return railNetwork.removeRail(id);
return trains.values().stream().noneMatch(train -> train.isOnRail(id)) && railNetwork.removeRail(id);
}
/**
@ -111,15 +108,9 @@ public class ModelRailwaySimulation {
*/
public void createEngine(final Engine newEngine) throws InvalidInputException {
String id = newEngine.getIdentifier();
for (Engine engine : engines) {
if (engine.getIdentifier().equals(id)) {
throw new InvalidInputException("engine identifier already used by engine");
}
}
for (TrainSet trainSet : trainSets) {
if (trainSet.getIdentifier().equals(id)) {
throw new InvalidInputException("engine identifier already used by train set");
}
if (Stream.concat(engines.stream(), trainSets.stream())
.anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) {
throw new InvalidInputException("engine identifier already used");
}
engines.add(newEngine);
}
@ -212,15 +203,9 @@ public class ModelRailwaySimulation {
*/
public void createTrainSet(final TrainSet newTrainSet) throws InvalidInputException {
String id = newTrainSet.getIdentifier();
for (Engine engine : engines) {
if (engine.getIdentifier().equals(id)) {
throw new InvalidInputException("train set identifier already used by engine");
}
}
for (TrainSet trainSet : trainSets) {
if (trainSet.getIdentifier().equals(id)) {
throw new InvalidInputException("train set identifier already used by train set");
}
if (Stream.concat(engines.stream(), trainSets.stream())
.anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) {
throw new InvalidInputException("train set identifier already used");
}
trainSets.add(newTrainSet);
}
@ -288,19 +273,16 @@ public class ModelRailwaySimulation {
if (rollingStock == null) {
throw new InvalidInputException("rolling stock not found");
}
for (Train train : trains.values()) {
if (train.contains(rollingStock)) {
if (trains.values().stream().anyMatch(train -> train.contains(rollingStock))) {
throw new InvalidInputException("rolling stock already used");
}
}
Train train = trains.get(trainId);
if (train != null && train.isPlaced()) {
throw new InvalidInputException("can not add rolling stock to placed train");
}
if (train != null) {
train.add(rollingStock);
return;
}
} else {
int correctId = getNextTrainIdentifier();
if (trainId != correctId) {
throw new InvalidInputException("new train identifier must be next free identifier");
@ -308,6 +290,7 @@ public class ModelRailwaySimulation {
Train newTrain = new Train(trainId, rollingStock);
trains.put(trainId, newTrain);
}
}
/**
* Calculate the next train identifier.
@ -569,18 +552,17 @@ public class ModelRailwaySimulation {
return;
}
List<Set<Train>> collisions = new ArrayList<>();
// first, handle positive speed (forward)
for (int i = 0; i < speed; i++) {
List<Set<Train>> newCollisions = getCollisionsOfOneStep();
collisions.addAll(newCollisions);
List<Set<Train>> collisions;
if (speed >= 0) {
collisions = IntStream.range(0, speed)
.mapToObj(step -> getCollisionsOfOneStep())
.flatMap(List::stream).collect(Collectors.toList());
} else {
collisions = IntStream.range(0, -speed)
.mapToObj(step -> getCollisionsOfOneReverseStep())
.flatMap(List::stream).collect(Collectors.toList());
}
// then, handle negative speed (backward)
for (int i = 0; i > speed; i--) {
List<Set<Train>> newCollisions = getCollisionsOfOneReverseStep();
collisions.addAll(newCollisions);
}
// TODO (only one of the loops above actually runs)
for (int id : trains.keySet().stream().sorted().collect(Collectors.toList())) {
Train train = trains.get(id);
Set<Train> collisionSet = collisions.stream()

View File

@ -129,7 +129,7 @@ public final class Train {
* @return whether this train is on that rail
*/
public boolean isOnRail(final int id) {
return occupiedRails != null && occupiedRails.stream().anyMatch(rail -> rail.getIdentifier() == id);
return isPlaced() && occupiedRails.stream().anyMatch(rail -> rail.getIdentifier() == id);
}
/**