mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-12 19:53:12 +00:00
SonarQube
This commit is contained in:
parent
72bea80f9b
commit
2f8f79a7a1
@ -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,10 +273,8 @@ public class ModelRailwaySimulation {
|
||||
if (rollingStock == null) {
|
||||
throw new InvalidInputException("rolling stock not found");
|
||||
}
|
||||
for (Train train : trains.values()) {
|
||||
if (train.contains(rollingStock)) {
|
||||
throw new InvalidInputException("rolling stock already used");
|
||||
}
|
||||
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()) {
|
||||
@ -299,14 +282,14 @@ public class ModelRailwaySimulation {
|
||||
}
|
||||
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");
|
||||
}
|
||||
Train newTrain = new Train(trainId, rollingStock);
|
||||
trains.put(trainId, newTrain);
|
||||
}
|
||||
int correctId = getNextTrainIdentifier();
|
||||
if (trainId != correctId) {
|
||||
throw new InvalidInputException("new train identifier must be next free identifier");
|
||||
}
|
||||
Train newTrain = new Train(trainId, rollingStock);
|
||||
trains.put(trainId, newTrain);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -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()
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user