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.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -430,12 +431,8 @@ public class ModelRailwaySimulation {
*/ */
private List<Set<Train>> getCollisionsOfOneStep() { private List<Set<Train>> getCollisionsOfOneStep() {
List<Set<Train>> collisions = new ArrayList<>(); List<Set<Train>> collisions = new ArrayList<>();
Map<Train, Set<Rail>> occupiedRails = new HashMap<>(); Map<Train, Set<Rail>> occupiedRails = trains.values().stream().filter(Train::isPlaced)
for (Train train : trains.values()) { .collect(Collectors.toMap(Function.identity(), Train::getOccupiedRails));
if (train.isPlaced()) {
occupiedRails.put(train, train.getOccupiedRails());
}
}
// perform step // perform step
Map<Train, Set<Rail>> nextOccupiedRails = new HashMap<>(); Map<Train, Set<Rail>> nextOccupiedRails = new HashMap<>();
trains.values().stream().filter(Train::isPlaced).forEach(train -> { 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 // update occupied blocks, first removing the rail block left behind
Rail leavingRail = railNetwork.findContainingRail(lastPosition); Rail leavingRail = railNetwork.findContainingRail(lastPosition);
if (leavingRail != null) { if (leavingRail != null && !leavingRail.contains(secondToLastPosition)) {
// check whether we really leave rail // we are leaving this rail
if (!leavingRail.contains(secondToLastPosition)) {
// we are really leaving this rail
occupiedRails.remove(leavingRail); occupiedRails.remove(leavingRail);
} // else: we are still on the rail
} else if (getLength() == 1 && railNetwork.findContainingRail(secondToLastPosition) == null) { } else if (getLength() == 1 && railNetwork.findContainingRail(secondToLastPosition) == null) {
// we evidently just moved into another rail // we evidently just moved into another rail
occupiedRails.clear(); occupiedRails.clear();
@ -308,21 +305,10 @@ public final class Train {
// we evidently just moved into another rail // we evidently just moved into another rail
Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition); Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition);
Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition); Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition);
// TODO: do we need all of these cases??? Arrays.stream(nextTouchingRails)
if (nextTouchingRails[0] == alreadyTouchingRails[0]) { .filter(rail -> Arrays.stream(alreadyTouchingRails).anyMatch(rail2 -> rail == rail2))
occupiedRails.add(nextTouchingRails[0]); .forEach(occupiedRails::add);
} else if (nextTouchingRails.length == 2) { } // else: only touching new rail
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
} }
/** /**