mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-08 18:00:38 +00:00
Checkstyle
This commit is contained in:
parent
0e56a1c73b
commit
61b6739ab4
@ -8,6 +8,7 @@ import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
@ -70,7 +71,8 @@ public class ModelRailwaySimulation {
|
||||
*/
|
||||
public boolean removeRail(int id) {
|
||||
// check whether any trains are on this rail
|
||||
return !trainManager.anyTrainOnRail(railNetwork.getRail(id)) && railNetwork.removeRail(id);
|
||||
return railNetwork.getRail(id).isPresent() && !trainManager.anyTrainOnRail(railNetwork.getRail(id).get())
|
||||
&& railNetwork.removeRail(id);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,7 +94,7 @@ public class ModelRailwaySimulation {
|
||||
public boolean setSwitch(int id, Vector2D position) {
|
||||
final boolean success = railNetwork.setSwitch(id, position);
|
||||
if (success) { // derail trains on switch, explicitly not (!) printing any removed trains (source: forum post)
|
||||
trainManager.removeTrainsOnRail(railNetwork.getRail(id));
|
||||
trainManager.removeTrainsOnRail(railNetwork.getRail(id).get());
|
||||
}
|
||||
return success;
|
||||
}
|
||||
@ -217,26 +219,32 @@ public class ModelRailwaySimulation {
|
||||
* @return whether the rolling was successfully removed
|
||||
*/
|
||||
public boolean deleteRollingStock(String id) {
|
||||
final RollingStock rollingStock = getRollingStock(id);
|
||||
if (trainManager.getTrainContainingRollingStock(rollingStock).isPresent()) {
|
||||
final Optional<RollingStock> rollingStock = getRollingStock(id);
|
||||
if (!rollingStock.isPresent()) {
|
||||
return false;
|
||||
}
|
||||
if (trainManager.getTrainContainingRollingStock(rollingStock.get()).isPresent()) {
|
||||
return false; // can not delete rolling stock in use
|
||||
}
|
||||
return engines.remove(rollingStock) || trainSets.remove(rollingStock) || coaches.values().remove(rollingStock);
|
||||
return engines.remove(rollingStock.get())
|
||||
|| trainSets.remove(rollingStock.get())
|
||||
|| coaches.values().remove(rollingStock.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a rolling stock by identifier.
|
||||
*
|
||||
* @param id identifier of the rolling stock to find
|
||||
* @return the specified rolling stock, or null if not found
|
||||
* @return the specified rolling stock
|
||||
*/
|
||||
public RollingStock getRollingStock(String id) {
|
||||
public Optional<RollingStock> getRollingStock(String id) {
|
||||
if (Coach.IDENTIFIER_PATTERN.matcher(id).matches()) {
|
||||
final int coachId = Integer.parseInt(id.substring(1));
|
||||
return coaches.get(coachId);
|
||||
return Optional.ofNullable(coaches.get(coachId));
|
||||
} else {
|
||||
return Stream.concat(engines.stream(), trainSets.stream())
|
||||
.filter(rollingStock -> rollingStock.getIdentifier().equals(id))
|
||||
.findFirst().orElse(null);
|
||||
.findFirst();
|
||||
}
|
||||
}
|
||||
|
||||
@ -247,11 +255,11 @@ public class ModelRailwaySimulation {
|
||||
* @throws InvalidInputException if input is incorrect
|
||||
*/
|
||||
public void addTrain(int trainId, String rollingStockId) throws InvalidInputException {
|
||||
final RollingStock rollingStock = getRollingStock(rollingStockId);
|
||||
if (rollingStock == null) {
|
||||
final Optional<RollingStock> rollingStock = getRollingStock(rollingStockId);
|
||||
if (!rollingStock.isPresent()) {
|
||||
throw new InvalidInputException("rolling stock not found");
|
||||
}
|
||||
trainManager.addTrain(trainId, rollingStock);
|
||||
trainManager.addTrain(trainId, rollingStock.get());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,10 +102,10 @@ public class RailwayNetwork {
|
||||
/**
|
||||
* Get the rail with the specified identifier.
|
||||
* @param id rail identifier to get
|
||||
* @return the rail, or null if not found
|
||||
* @return the rail
|
||||
*/
|
||||
public Rail getRail(int id) {
|
||||
return rails.get(id);
|
||||
public Optional<Rail> getRail(int id) {
|
||||
return Optional.ofNullable(rails.get(id));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -202,15 +202,16 @@ public class RailwayNetwork {
|
||||
* @return next position, null if off the rails after single step
|
||||
*/
|
||||
public Vector2D move(Vector2D position, Vector2D direction, long steps) {
|
||||
final Rail containingRail = findContainingRail(position);
|
||||
if (containingRail != null) {
|
||||
return containingRail.move(position, direction, steps);
|
||||
final Optional<Rail> containingRail = findContainingRail(position);
|
||||
if (containingRail.isPresent()) {
|
||||
return containingRail.get().move(position, direction, steps);
|
||||
}
|
||||
final Rail[] touchingRails = findTouchingRails(position);
|
||||
final Vector2D nextPossiblePosition = position.add(direction);
|
||||
final Rail possibleContainingRail = findRailPotentiallyContaining(nextPossiblePosition);
|
||||
if (possibleContainingRail != null && !possibleContainingRail.contains(position.subtract(direction))) {
|
||||
return possibleContainingRail.move(position, direction, steps);
|
||||
final Optional<Rail> possibleContainingRail = findRailPotentiallyContaining(nextPossiblePosition);
|
||||
if (possibleContainingRail.isPresent()
|
||||
&& !possibleContainingRail.get().contains(position.subtract(direction))) {
|
||||
return possibleContainingRail.get().move(position, direction, steps);
|
||||
|
||||
}
|
||||
if (touchingRails.length == 0) {
|
||||
@ -240,14 +241,14 @@ public class RailwayNetwork {
|
||||
/**
|
||||
* Find a rail that *contains* (not touch) this position.
|
||||
* @param position the position to check
|
||||
* @return the rail that contains the position, null if none found
|
||||
* @return the rail that contains the position
|
||||
*/
|
||||
public Rail findContainingRail(Vector2D position) {
|
||||
return rails.values().stream().filter(rail -> rail.contains(position)).findFirst().orElse(null);
|
||||
public Optional<Rail> findContainingRail(Vector2D position) {
|
||||
return rails.values().stream().filter(rail -> rail.contains(position)).findFirst();
|
||||
}
|
||||
|
||||
private Rail findRailPotentiallyContaining(Vector2D position) {
|
||||
return rails.values().stream().filter(rail -> rail.canContain(position)).findFirst().orElse(null);
|
||||
private Optional<Rail> findRailPotentiallyContaining(Vector2D position) {
|
||||
return rails.values().stream().filter(rail -> rail.canContain(position)).findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,6 +7,7 @@ import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -154,8 +155,8 @@ public final class Train {
|
||||
positionList.addLast(position);
|
||||
Vector2D rollingStockPosition = position;
|
||||
// check that the requested direction is equal to the direction of one the tracks
|
||||
if ((railNetwork.findContainingRail(position) != null
|
||||
&& !railNetwork.findContainingRail(position).allowsDirectionFrom(position, positioningDirection))
|
||||
if ((railNetwork.findContainingRail(position).isPresent()
|
||||
&& !railNetwork.findContainingRail(position).get().allowsDirectionFrom(position, positioningDirection))
|
||||
|| (railNetwork.findTouchingRails(position).length > 0
|
||||
&& Arrays.stream(railNetwork.findTouchingRails(position))
|
||||
.noneMatch(rail -> rail.allowsDirectionFrom(position, direction)))) {
|
||||
@ -265,12 +266,12 @@ public final class Train {
|
||||
* @param nextPosition position to move train to
|
||||
*/
|
||||
public void moveTo(RailwayNetwork railNetwork, Vector2D nextPosition) {
|
||||
final Rail railUnderBackOfTrain = railNetwork.findContainingRail(positions.getLast());
|
||||
final Optional<Rail> railUnderBackOfTrain = railNetwork.findContainingRail(positions.getLast());
|
||||
positions.getLast().subtractInPlace(getRearDirection());
|
||||
if (positions.getLast().equals(positions.get(positions.size() - 2))) {
|
||||
if (railNetwork.findContainingRail(nextPosition) != railUnderBackOfTrain
|
||||
if (!railNetwork.findContainingRail(nextPosition).equals(railUnderBackOfTrain)
|
||||
|| nextPosition == null) {
|
||||
occupiedRails.remove(railUnderBackOfTrain);
|
||||
occupiedRails.remove(railUnderBackOfTrain.orElse(null));
|
||||
}
|
||||
positions.removeLast();
|
||||
}
|
||||
@ -294,12 +295,12 @@ public final class Train {
|
||||
* @param backPosition position to move back of the train to
|
||||
*/
|
||||
public void moveBackTo(RailwayNetwork railNetwork, Vector2D backPosition) {
|
||||
final Rail railUnderFrontOfTrain = railNetwork.findContainingRail(positions.getFirst());
|
||||
final Optional<Rail> railUnderFrontOfTrain = railNetwork.findContainingRail(positions.getFirst());
|
||||
positions.getFirst().subtractInPlace(getDirection());
|
||||
if (positions.getFirst().equals(positions.get(1))) {
|
||||
if (railNetwork.findContainingRail(backPosition) != railUnderFrontOfTrain
|
||||
if (!railNetwork.findContainingRail(backPosition).equals(railUnderFrontOfTrain)
|
||||
|| backPosition == null) {
|
||||
occupiedRails.remove(railUnderFrontOfTrain);
|
||||
occupiedRails.remove(railUnderFrontOfTrain.orElse(null));
|
||||
}
|
||||
positions.removeFirst();
|
||||
}
|
||||
@ -336,16 +337,6 @@ public final class Train {
|
||||
return positions != null && other.isOnAnyPosition(positions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this train is on the specified position. This includes the front and back of the train.
|
||||
*
|
||||
* @param point position to check
|
||||
* @return whether this train is on the specified position
|
||||
*/
|
||||
public boolean isOnPosition(Vector2D point) {
|
||||
return positions.contains(point);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this train is on any of the provided positions. This includes the front and back of the train.
|
||||
*
|
||||
|
@ -37,7 +37,7 @@ public class AddTrain extends Command {
|
||||
throw new IllegalStateException("command not initialized");
|
||||
}
|
||||
simulation.addTrain(trainId, rollingStockId);
|
||||
final RollingStock rollingStock = simulation.getRollingStock(rollingStockId);
|
||||
final RollingStock rollingStock = simulation.getRollingStock(rollingStockId).get();
|
||||
Terminal.printLine(String.format("%s %s added to train %d",
|
||||
rollingStock.description(), rollingStock.getIdentifier(), trainId));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user