Checkstyle

This commit is contained in:
Arne Keller 2020-03-08 09:22:40 +01:00
parent fa3c9cf113
commit f0b35e0707
6 changed files with 27 additions and 19 deletions

View File

@ -102,7 +102,7 @@ public final class Terminal {
if ("breakpoint".equals(line)) {
return readLine();
} else if (line != null && line.startsWith("#")) {
return readLine();
line = IN.readLine();
}
return line;
} catch (final IOException e) {

View File

@ -322,10 +322,10 @@ public class ModelRailwaySimulation {
* Move the trains in this simulation.
*
* @param speed amount of steps to move the trains
* @return train collisions, null if no trains are placed
* @return train collisions, empty if no trains are placed
* @throws InvalidInputException if the simulation is not yet ready
*/
public List<SortedSet<Integer>> step(short speed) throws InvalidInputException {
public Optional<List<SortedSet<Integer>>> step(short speed) throws InvalidInputException {
return trainManager.step(speed);
}
}

View File

@ -197,7 +197,7 @@ public class RailwayNetwork {
return null; // there is no rail to move on
} else if (touchingRails.length == 1) { // simply move along the rail
return touchingRails[0].move(position, direction, 1);
} // else: we check movement along both rails and pick the correct rail
} // else: check movement along both rails and pick the correct rail
final Vector2D onRailOne = touchingRails[0].move(position, new Vector2D(direction), steps);
final Vector2D onRailTwo = touchingRails[1].move(position, new Vector2D(direction), steps);
// if we are stuck on the first rail
@ -205,8 +205,8 @@ public class RailwayNetwork {
// or move in the correct direction on the second rail
if (position.equals(onRailOne) || onRailOne == null
|| onRailTwo != null && onRailTwo.subtract(position).directionEquals(direction)) {
return onRailTwo; // return that position
} else if (position.equals(onRailTwo) || onRailTwo == null
return onRailTwo; // return movement on rail two
} else if (position.equals(onRailTwo) || onRailTwo == null // see above, same logic for the other rail
|| onRailOne.subtract(position).directionEquals(direction)) {
return onRailOne;
} else {
@ -235,10 +235,10 @@ public class RailwayNetwork {
// rail should not be orthogonal to the requested direction
return touchingRails[0].allowsPlacement(position, direction)
|| touchingRails[0].allowsMovement(position, direction);
} else if (!touchingRails[0].allowsPlacement(position, direction)
&& !touchingRails[1].allowsPlacement(position, direction)
&& !(touchingRails[0].allowsMovement(position, direction)
|| touchingRails[1].allowsMovement(position, direction))) {
} else if (!touchingRails[0].allowsPlacement(position, direction) // train can not be placed on either rail
&& !touchingRails[1].allowsPlacement(position, direction) // in the specified direction
&& !(touchingRails[0].allowsMovement(position, direction) // *and* it can not move in the specified
|| touchingRails[1].allowsMovement(position, direction))) { // direction afterwards
// rail network does not allow this direction according to
// https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378
return false;

View File

@ -8,6 +8,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
@ -252,7 +253,7 @@ public final class Train implements Comparable<Train> {
* @param backPosition position to move back of the train to
*/
public void moveBackTo(RailwayNetwork railNetwork, Vector2D backPosition) {
// TODO: create common method for both directions
// a common method for both directions would be a huge mess: requires at least six (!) extra parameters
final Optional<Rail> railUnderFrontOfTrain = railNetwork.findContainingRail(positions.getFirst());
positions.getFirst().subtractInPlace(getDirection());
if (positions.getFirst().equals(positions.get(1))) {
@ -372,6 +373,12 @@ public final class Train implements Comparable<Train> {
return stringBuilder.toString();
}
/**
* Compare two trains based on their identifier.
*
* @param other train to compare to
* @return integer comparison of the identifiers
*/
@Override
public int compareTo(Train other) {
return Integer.compare(this.identifier, other.identifier);

View File

@ -333,24 +333,24 @@ public final class TrainManager {
* Move the trains in this simulation.
*
* @param speed amount of steps to move the trains
* @return train collisions, null if no trains are placed
* @return train collisions, empty if no trains are placed
* @throws InvalidInputException if simulation is not yet ready
*/
public List<SortedSet<Integer>> step(short speed) throws InvalidInputException {
public Optional<List<SortedSet<Integer>>> step(short speed) throws InvalidInputException {
if (!railNetwork.isReadyForTrains()) {
throw new InvalidInputException("rail tracks/switches not set up");
}
if (trains.values().stream().noneMatch(Train::isPlaced)) {
return null;
return Optional.empty();
}
return IntStream.range(0, Math.abs(speed))
return Optional.of(IntStream.range(0, Math.abs(speed))
.mapToObj(step -> speed >= 0 ? getCollisionsOfOneStep() : getCollisionsOfOneReverseStep())
// replace train references with identifiers to prevent modification by caller
.flatMap(collisions -> collisions.stream()
.map(collision -> collision.stream().map(Train::getIdentifier)
.collect(Collectors.toCollection(TreeSet::new))))
.sorted(Comparator.comparing(TreeSet::first))
.collect(Collectors.toList());
.collect(Collectors.toList()));
}
}

View File

@ -6,6 +6,7 @@ import edu.kit.informatik.model.Vector2D;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.List;
import java.util.Optional;
import java.util.SortedSet;
import static edu.kit.informatik.ui.CommandLine.OK;
@ -26,12 +27,12 @@ public class Step extends Command {
@Override
public void apply(ModelRailwaySimulation simulation) throws InvalidInputException {
final List<SortedSet<Integer>> collisions = simulation.step(speed);
if (collisions == null) {
final Optional<List<SortedSet<Integer>>> collisions = simulation.step(speed);
if (!collisions.isPresent()) {
Terminal.printLine(OK);
} else {
for (final int id : simulation.getTrains().keySet()) {
final SortedSet<Integer> collisionSet = collisions.stream()
final SortedSet<Integer> collisionSet = collisions.get().stream()
.filter(collision -> collision.first() == id)
.findFirst().orElse(null);
if (collisionSet != null) { // print collision