From 72bea80f9bf4c937cc315c43168054c871771dc0 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 20 Feb 2020 09:04:18 +0100 Subject: [PATCH] SonarQube --- .../model/ModelRailwaySimulation.java | 9 ++---- src/edu/kit/informatik/model/Train.java | 28 +++++-------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 367b57d..2fc501d 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -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> getCollisionsOfOneStep() { List> collisions = new ArrayList<>(); - Map> occupiedRails = new HashMap<>(); - for (Train train : trains.values()) { - if (train.isPlaced()) { - occupiedRails.put(train, train.getOccupiedRails()); - } - } + Map> occupiedRails = trains.values().stream().filter(Train::isPlaced) + .collect(Collectors.toMap(Function.identity(), Train::getOccupiedRails)); // perform step Map> nextOccupiedRails = new HashMap<>(); trains.values().stream().filter(Train::isPlaced).forEach(train -> { diff --git a/src/edu/kit/informatik/model/Train.java b/src/edu/kit/informatik/model/Train.java index c438920..8f9d44b 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -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 } /**