mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 09:24:58 +00:00
SonarQube
This commit is contained in:
parent
e54bf43cbd
commit
938ace5a5b
@ -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<Train> 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<Train> otherCollision = collisions.stream()
|
||||
|
@ -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<StringBuilder> 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<Rail> occupiedRailsSet = new HashSet<>();
|
||||
for (int i = 0; i < positions.size(); i++) {
|
||||
Vector2D point = positions.get(i);
|
||||
|
||||
Set<Rail> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user