SonarQube

This commit is contained in:
Arne Keller 2020-02-20 09:04:18 +01:00
parent 938ace5a5b
commit 72bea80f9b
2 changed files with 10 additions and 27 deletions

View File

@ -13,6 +13,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@ -430,12 +431,8 @@ public class ModelRailwaySimulation {
*/
private List<Set<Train>> getCollisionsOfOneStep() {
List<Set<Train>> collisions = new ArrayList<>();
Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
for (Train train : trains.values()) {
if (train.isPlaced()) {
occupiedRails.put(train, train.getOccupiedRails());
}
}
Map<Train, Set<Rail>> occupiedRails = trains.values().stream().filter(Train::isPlaced)
.collect(Collectors.toMap(Function.identity(), Train::getOccupiedRails));
// perform step
Map<Train, Set<Rail>> nextOccupiedRails = new HashMap<>();
trains.values().stream().filter(Train::isPlaced).forEach(train -> {

View File

@ -290,12 +290,9 @@ public final class Train {
}
// update occupied blocks, first removing the rail block left behind
Rail leavingRail = railNetwork.findContainingRail(lastPosition);
if (leavingRail != null) {
// check whether we really leave rail
if (!leavingRail.contains(secondToLastPosition)) {
// we are really leaving this rail
occupiedRails.remove(leavingRail);
} // else: we are still on the rail
if (leavingRail != null && !leavingRail.contains(secondToLastPosition)) {
// we are leaving this rail
occupiedRails.remove(leavingRail);
} else if (getLength() == 1 && railNetwork.findContainingRail(secondToLastPosition) == null) {
// we evidently just moved into another rail
occupiedRails.clear();
@ -308,21 +305,10 @@ public final class Train {
// we evidently just moved into another rail
Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition);
Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition);
// TODO: do we need all of these cases???
if (nextTouchingRails[0] == alreadyTouchingRails[0]) {
occupiedRails.add(nextTouchingRails[0]);
} else if (nextTouchingRails.length == 2) {
if (nextTouchingRails[1] == alreadyTouchingRails[0]) {
occupiedRails.add(nextTouchingRails[1]);
} else if (alreadyTouchingRails.length == 2) {
if (alreadyTouchingRails[1] == nextTouchingRails[0]) {
occupiedRails.add(nextTouchingRails[0]);
} else if (alreadyTouchingRails[1] == nextTouchingRails[1]) {
occupiedRails.add(nextTouchingRails[1]);
}
}
}
} // else: only touching rail
Arrays.stream(nextTouchingRails)
.filter(rail -> Arrays.stream(alreadyTouchingRails).anyMatch(rail2 -> rail == rail2))
.forEach(occupiedRails::add);
} // else: only touching new rail
}
/**