diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index 2fc501d..c75eaa9 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -76,10 +76,7 @@ public class ModelRailwaySimulation { */ public boolean removeRail(final int id) { // check whether any trains are on this rail - if (trains.values().stream().anyMatch(train -> train.isPlaced() && train.isOnRail(id))) { - return false; - } - return railNetwork.removeRail(id); + return trains.values().stream().noneMatch(train -> train.isOnRail(id)) && railNetwork.removeRail(id); } /** @@ -111,15 +108,9 @@ public class ModelRailwaySimulation { */ public void createEngine(final Engine newEngine) throws InvalidInputException { String id = newEngine.getIdentifier(); - for (Engine engine : engines) { - if (engine.getIdentifier().equals(id)) { - throw new InvalidInputException("engine identifier already used by engine"); - } - } - for (TrainSet trainSet : trainSets) { - if (trainSet.getIdentifier().equals(id)) { - throw new InvalidInputException("engine identifier already used by train set"); - } + if (Stream.concat(engines.stream(), trainSets.stream()) + .anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) { + throw new InvalidInputException("engine identifier already used"); } engines.add(newEngine); } @@ -212,15 +203,9 @@ public class ModelRailwaySimulation { */ public void createTrainSet(final TrainSet newTrainSet) throws InvalidInputException { String id = newTrainSet.getIdentifier(); - for (Engine engine : engines) { - if (engine.getIdentifier().equals(id)) { - throw new InvalidInputException("train set identifier already used by engine"); - } - } - for (TrainSet trainSet : trainSets) { - if (trainSet.getIdentifier().equals(id)) { - throw new InvalidInputException("train set identifier already used by train set"); - } + if (Stream.concat(engines.stream(), trainSets.stream()) + .anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) { + throw new InvalidInputException("train set identifier already used"); } trainSets.add(newTrainSet); } @@ -288,10 +273,8 @@ public class ModelRailwaySimulation { if (rollingStock == null) { throw new InvalidInputException("rolling stock not found"); } - for (Train train : trains.values()) { - if (train.contains(rollingStock)) { - throw new InvalidInputException("rolling stock already used"); - } + if (trains.values().stream().anyMatch(train -> train.contains(rollingStock))) { + throw new InvalidInputException("rolling stock already used"); } Train train = trains.get(trainId); if (train != null && train.isPlaced()) { @@ -299,14 +282,14 @@ public class ModelRailwaySimulation { } if (train != null) { train.add(rollingStock); - return; + } else { + int correctId = getNextTrainIdentifier(); + if (trainId != correctId) { + throw new InvalidInputException("new train identifier must be next free identifier"); + } + Train newTrain = new Train(trainId, rollingStock); + trains.put(trainId, newTrain); } - int correctId = getNextTrainIdentifier(); - if (trainId != correctId) { - throw new InvalidInputException("new train identifier must be next free identifier"); - } - Train newTrain = new Train(trainId, rollingStock); - trains.put(trainId, newTrain); } /** @@ -569,18 +552,17 @@ public class ModelRailwaySimulation { return; } - List> collisions = new ArrayList<>(); - // first, handle positive speed (forward) - for (int i = 0; i < speed; i++) { - List> newCollisions = getCollisionsOfOneStep(); - collisions.addAll(newCollisions); + List> collisions; + if (speed >= 0) { + collisions = IntStream.range(0, speed) + .mapToObj(step -> getCollisionsOfOneStep()) + .flatMap(List::stream).collect(Collectors.toList()); + } else { + collisions = IntStream.range(0, -speed) + .mapToObj(step -> getCollisionsOfOneReverseStep()) + .flatMap(List::stream).collect(Collectors.toList()); } - // then, handle negative speed (backward) - for (int i = 0; i > speed; i--) { - List> newCollisions = getCollisionsOfOneReverseStep(); - collisions.addAll(newCollisions); - } - // TODO (only one of the loops above actually runs) + for (int id : trains.keySet().stream().sorted().collect(Collectors.toList())) { Train train = trains.get(id); Set collisionSet = collisions.stream() diff --git a/src/edu/kit/informatik/model/Train.java b/src/edu/kit/informatik/model/Train.java index 8f9d44b..680684b 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -129,7 +129,7 @@ public final class Train { * @return whether this train is on that rail */ public boolean isOnRail(final int id) { - return occupiedRails != null && occupiedRails.stream().anyMatch(rail -> rail.getIdentifier() == id); + return isPlaced() && occupiedRails.stream().anyMatch(rail -> rail.getIdentifier() == id); } /**