SonarQube

This commit is contained in:
Arne Keller 2020-02-20 08:36:35 +01:00
parent e54bf43cbd
commit 938ace5a5b
2 changed files with 28 additions and 45 deletions

View File

@ -11,8 +11,10 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
/** /**
@ -176,12 +178,8 @@ public class ModelRailwaySimulation {
* @return the next coach identifier, or -1 if none available * @return the next coach identifier, or -1 if none available
*/ */
private int getNextCoachIdentifier() { private int getNextCoachIdentifier() {
for (int id = 1; id > 0; id++) { return IntStream.rangeClosed(1, Integer.MAX_VALUE)
if (!coaches.containsKey(id)) { .filter(id -> !coaches.containsKey(id)).findFirst().orElse(-1);
return id;
}
}
return -1;
} }
/** /**
@ -315,12 +313,8 @@ public class ModelRailwaySimulation {
* @return the next train identfier, or -1 if none available * @return the next train identfier, or -1 if none available
*/ */
private int getNextTrainIdentifier() { private int getNextTrainIdentifier() {
for (int id = 1; id > 0; id++) { return IntStream.rangeClosed(1, Integer.MAX_VALUE)
if (!trains.containsKey(id)) { .filter(id -> !trains.containsKey(id)).findFirst().orElse(-1);
return id;
}
}
return -1;
} }
/** /**
@ -407,16 +401,12 @@ public class ModelRailwaySimulation {
continue; continue;
} }
HashSet<Train> collision = new HashSet<>(); HashSet<Train> collision = new HashSet<>();
for (int id2 = id1 + 1; id2 <= maxId; id2++) { IntStream.rangeClosed(id1 + 1, maxId)
Train train2 = trains.get(id2); .mapToObj(trains::get)
if (train2 == null || !train2.isPlaced()) { .filter(Objects::nonNull)
continue; .filter(Train::isPlaced)
} .filter(train1::touches)
.forEach(collision::add);
if (train1.touches(train2)) {
collision.add(train2);
}
}
if (!collision.isEmpty()) { if (!collision.isEmpty()) {
// check for existing collision // check for existing collision
Set<Train> otherCollision = collisions.stream() Set<Train> otherCollision = collisions.stream()

View File

@ -4,10 +4,13 @@ import edu.kit.informatik.Terminal;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set; 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. * A train consisting of one or more rolling stock. Can be placed on a rail network.
@ -94,18 +97,16 @@ public final class Train {
public void print() { public void print() {
List<StringBuilder> lines = new ArrayList<>(); List<StringBuilder> lines = new ArrayList<>();
for (RollingStock rollingStock : rollingStocks) { for (RollingStock rollingStock : rollingStocks) {
String[] text = rollingStock.textRepresentation();
for (StringBuilder line : lines) { for (StringBuilder line : lines) {
line.append(' '); line.append(' ');
} }
String[] text = rollingStock.textRepresentation();
int currentLength = lines.stream().mapToInt(StringBuilder::length).max().orElse(0); int currentLength = lines.stream().mapToInt(StringBuilder::length).max().orElse(0);
while (lines.size() < text.length) { while (lines.size() < text.length) {
StringBuilder line = new StringBuilder(); StringBuilder line = new StringBuilder();
if (!lines.isEmpty()) {
for (int i = 0; i < currentLength; i++) { for (int i = 0; i < currentLength; i++) {
line.append(' '); line.append(' ');
} }
}
lines.add(0, line); lines.add(0, line);
} }
for (int i = 0; i < text.length; i++) { for (int i = 0; i < text.length; i++) {
@ -118,9 +119,7 @@ public final class Train {
} }
} }
} }
for (StringBuilder line : lines) { lines.forEach(Terminal::printLine);
Terminal.printLine(line.toString());
}
} }
/** /**
@ -166,24 +165,18 @@ public final class Train {
} }
positions.addLast(rollingStockPosition); positions.addLast(rollingStockPosition);
} }
Set<Rail> occupiedRailsSet = new HashSet<>();
for (int i = 0; i < positions.size(); i++) { Set<Rail> occupiedRailsSet = positions.stream().flatMap(point -> {
Vector2D point = positions.get(i);
Rail containingRail = railNetwork.findContainingRail(point); Rail containingRail = railNetwork.findContainingRail(point);
if (containingRail != null) { if (containingRail != null) {
occupiedRailsSet.add(containingRail); return Stream.of(containingRail);
} else if (i > 0) { } else {
Rail[] touchingRails = railNetwork.findTouchingRails(point); return Arrays.stream(railNetwork.findTouchingRails(point))
for (Rail rail : touchingRails) { // ONLY add rails we fully occupy
if (positions.stream().filter(rail::connectsTo).count() == 2) { .filter(rail -> 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);
}
}
}
} }
}).collect(Collectors.toSet());
this.position = positions; this.position = positions;
this.direction = rawDirection.normalized(); this.direction = rawDirection.normalized();
this.occupiedRails = occupiedRailsSet; this.occupiedRails = occupiedRailsSet;