mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 09:24:58 +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) {
|
public boolean removeRail(final int id) {
|
||||||
// check whether any trains are on this rail
|
// check whether any trains are on this rail
|
||||||
if (trains.values().stream().anyMatch(train -> train.isPlaced() && train.isOnRail(id))) {
|
return trains.values().stream().noneMatch(train -> train.isOnRail(id)) && railNetwork.removeRail(id);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return railNetwork.removeRail(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,15 +108,9 @@ public class ModelRailwaySimulation {
|
|||||||
*/
|
*/
|
||||||
public void createEngine(final Engine newEngine) throws InvalidInputException {
|
public void createEngine(final Engine newEngine) throws InvalidInputException {
|
||||||
String id = newEngine.getIdentifier();
|
String id = newEngine.getIdentifier();
|
||||||
for (Engine engine : engines) {
|
if (Stream.concat(engines.stream(), trainSets.stream())
|
||||||
if (engine.getIdentifier().equals(id)) {
|
.anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) {
|
||||||
throw new InvalidInputException("engine identifier already used by engine");
|
throw new InvalidInputException("engine identifier already used");
|
||||||
}
|
|
||||||
}
|
|
||||||
for (TrainSet trainSet : trainSets) {
|
|
||||||
if (trainSet.getIdentifier().equals(id)) {
|
|
||||||
throw new InvalidInputException("engine identifier already used by train set");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
engines.add(newEngine);
|
engines.add(newEngine);
|
||||||
}
|
}
|
||||||
@ -212,15 +203,9 @@ public class ModelRailwaySimulation {
|
|||||||
*/
|
*/
|
||||||
public void createTrainSet(final TrainSet newTrainSet) throws InvalidInputException {
|
public void createTrainSet(final TrainSet newTrainSet) throws InvalidInputException {
|
||||||
String id = newTrainSet.getIdentifier();
|
String id = newTrainSet.getIdentifier();
|
||||||
for (Engine engine : engines) {
|
if (Stream.concat(engines.stream(), trainSets.stream())
|
||||||
if (engine.getIdentifier().equals(id)) {
|
.anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) {
|
||||||
throw new InvalidInputException("train set identifier already used by engine");
|
throw new InvalidInputException("train set identifier already used");
|
||||||
}
|
|
||||||
}
|
|
||||||
for (TrainSet trainSet : trainSets) {
|
|
||||||
if (trainSet.getIdentifier().equals(id)) {
|
|
||||||
throw new InvalidInputException("train set identifier already used by train set");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
trainSets.add(newTrainSet);
|
trainSets.add(newTrainSet);
|
||||||
}
|
}
|
||||||
@ -288,10 +273,8 @@ public class ModelRailwaySimulation {
|
|||||||
if (rollingStock == null) {
|
if (rollingStock == null) {
|
||||||
throw new InvalidInputException("rolling stock not found");
|
throw new InvalidInputException("rolling stock not found");
|
||||||
}
|
}
|
||||||
for (Train train : trains.values()) {
|
if (trains.values().stream().anyMatch(train -> train.contains(rollingStock))) {
|
||||||
if (train.contains(rollingStock)) {
|
throw new InvalidInputException("rolling stock already used");
|
||||||
throw new InvalidInputException("rolling stock already used");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Train train = trains.get(trainId);
|
Train train = trains.get(trainId);
|
||||||
if (train != null && train.isPlaced()) {
|
if (train != null && train.isPlaced()) {
|
||||||
@ -299,14 +282,14 @@ public class ModelRailwaySimulation {
|
|||||||
}
|
}
|
||||||
if (train != null) {
|
if (train != null) {
|
||||||
train.add(rollingStock);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Set<Train>> collisions = new ArrayList<>();
|
List<Set<Train>> collisions;
|
||||||
// first, handle positive speed (forward)
|
if (speed >= 0) {
|
||||||
for (int i = 0; i < speed; i++) {
|
collisions = IntStream.range(0, speed)
|
||||||
List<Set<Train>> newCollisions = getCollisionsOfOneStep();
|
.mapToObj(step -> getCollisionsOfOneStep())
|
||||||
collisions.addAll(newCollisions);
|
.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())) {
|
for (int id : trains.keySet().stream().sorted().collect(Collectors.toList())) {
|
||||||
Train train = trains.get(id);
|
Train train = trains.get(id);
|
||||||
Set<Train> collisionSet = collisions.stream()
|
Set<Train> collisionSet = collisions.stream()
|
||||||
|
@ -129,7 +129,7 @@ public final class Train {
|
|||||||
* @return whether this train is on that rail
|
* @return whether this train is on that rail
|
||||||
*/
|
*/
|
||||||
public boolean isOnRail(final int id) {
|
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