From 938ace5a5b83a0301043f09069a2919cde61b2a3 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 20 Feb 2020 08:36:35 +0100 Subject: [PATCH] SonarQube --- .../model/ModelRailwaySimulation.java | 34 ++++++---------- src/edu/kit/informatik/model/Train.java | 39 ++++++++----------- 2 files changed, 28 insertions(+), 45 deletions(-) diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 13bcce8..367b57d 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -11,8 +11,10 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; /** @@ -176,12 +178,8 @@ public class ModelRailwaySimulation { * @return the next coach identifier, or -1 if none available */ private int getNextCoachIdentifier() { - for (int id = 1; id > 0; id++) { - if (!coaches.containsKey(id)) { - return id; - } - } - return -1; + return IntStream.rangeClosed(1, Integer.MAX_VALUE) + .filter(id -> !coaches.containsKey(id)).findFirst().orElse(-1); } /** @@ -315,12 +313,8 @@ public class ModelRailwaySimulation { * @return the next train identfier, or -1 if none available */ private int getNextTrainIdentifier() { - for (int id = 1; id > 0; id++) { - if (!trains.containsKey(id)) { - return id; - } - } - return -1; + return IntStream.rangeClosed(1, Integer.MAX_VALUE) + .filter(id -> !trains.containsKey(id)).findFirst().orElse(-1); } /** @@ -407,16 +401,12 @@ public class ModelRailwaySimulation { continue; } HashSet collision = new HashSet<>(); - for (int id2 = id1 + 1; id2 <= maxId; id2++) { - Train train2 = trains.get(id2); - if (train2 == null || !train2.isPlaced()) { - continue; - } - - if (train1.touches(train2)) { - collision.add(train2); - } - } + IntStream.rangeClosed(id1 + 1, maxId) + .mapToObj(trains::get) + .filter(Objects::nonNull) + .filter(Train::isPlaced) + .filter(train1::touches) + .forEach(collision::add); if (!collision.isEmpty()) { // check for existing collision Set otherCollision = collisions.stream() diff --git a/src/edu/kit/informatik/model/Train.java b/src/edu/kit/informatik/model/Train.java index 5ca4fec..c438920 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -4,10 +4,13 @@ import edu.kit.informatik.Terminal; import edu.kit.informatik.ui.InvalidInputException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * A train consisting of one or more rolling stock. Can be placed on a rail network. @@ -94,17 +97,15 @@ public final class Train { public void print() { List lines = new ArrayList<>(); for (RollingStock rollingStock : rollingStocks) { - String[] text = rollingStock.textRepresentation(); for (StringBuilder line : lines) { line.append(' '); } + String[] text = rollingStock.textRepresentation(); int currentLength = lines.stream().mapToInt(StringBuilder::length).max().orElse(0); while (lines.size() < text.length) { StringBuilder line = new StringBuilder(); - if (!lines.isEmpty()) { - for (int i = 0; i < currentLength; i++) { - line.append(' '); - } + for (int i = 0; i < currentLength; i++) { + line.append(' '); } lines.add(0, line); } @@ -118,9 +119,7 @@ public final class Train { } } } - for (StringBuilder line : lines) { - Terminal.printLine(line.toString()); - } + lines.forEach(Terminal::printLine); } /** @@ -166,24 +165,18 @@ public final class Train { } positions.addLast(rollingStockPosition); } - Set occupiedRailsSet = new HashSet<>(); - for (int i = 0; i < positions.size(); i++) { - Vector2D point = positions.get(i); + + Set occupiedRailsSet = positions.stream().flatMap(point -> { Rail containingRail = railNetwork.findContainingRail(point); if (containingRail != null) { - occupiedRailsSet.add(containingRail); - } else if (i > 0) { - Rail[] touchingRails = railNetwork.findTouchingRails(point); - for (Rail rail : touchingRails) { - if (positions.stream().filter(rail::connectsTo).count() == 2) { - // ONLY add this rail if we actually occupy it fully - // note that this edge case only happens with rails of length one: - // otherwise at least one position will be contained in the rail (see above) - occupiedRailsSet.add(rail); - } - } + return Stream.of(containingRail); + } else { + return Arrays.stream(railNetwork.findTouchingRails(point)) + // ONLY add rails we fully occupy + .filter(rail -> positions.stream().filter(rail::connectsTo).count() == 2); } - } + }).collect(Collectors.toSet()); + this.position = positions; this.direction = rawDirection.normalized(); this.occupiedRails = occupiedRailsSet;