From 29a1953dab9c9711d21370a1ee9a1feb913519c4 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 20 Feb 2020 11:51:57 +0100 Subject: [PATCH] Checkstyle and SonarQube --- src/edu/kit/informatik/model/Coach.java | 11 ++ .../model/ModelRailwaySimulation.java | 172 +++++++----------- src/edu/kit/informatik/ui/CommandLine.java | 2 +- .../informatik/ui/command/CommandFactory.java | 3 +- .../informatik/ui/command/CreateEngine.java | 3 +- .../informatik/ui/command/CreateTrainSet.java | 3 +- testcoverage_output.txt | 10 +- 7 files changed, 87 insertions(+), 117 deletions(-) diff --git a/src/edu/kit/informatik/model/Coach.java b/src/edu/kit/informatik/model/Coach.java index 8e96ef8..8319ba3 100644 --- a/src/edu/kit/informatik/model/Coach.java +++ b/src/edu/kit/informatik/model/Coach.java @@ -2,6 +2,8 @@ package edu.kit.informatik.model; import edu.kit.informatik.ui.InvalidInputException; +import java.util.regex.Pattern; + /** * A coach. * @@ -9,6 +11,15 @@ import edu.kit.informatik.ui.InvalidInputException; * @version 1.0 */ public abstract class Coach extends RollingStock { + /** + * String used to prefix coach identifiers. + */ + public static final String IDENTIFIER_PREFIX = "W"; + /** + * Regex pattern to match coach identifiers. + */ + public static final Pattern IDENTIFIER_PATTERN = Pattern.compile(IDENTIFIER_PREFIX + "\\+?\\d+"); + /** * The unique identifier of this coach. */ diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index c75eaa9..2b6f074 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -95,8 +95,7 @@ public class ModelRailwaySimulation { */ public boolean setSwitch(final int id, final Vector2D position) { boolean success = railNetwork.setSwitch(id, position); - if (success) { - // derail trains on switch, explicitly not (!) printing any removed trains (source: forum post) + if (success) { // derail trains on switch, explicitly not (!) printing any removed trains (source: forum post) trains.values().stream().filter(train -> train.isOnRail(id)).forEach(Train::removeFromRails); } return success; @@ -124,12 +123,10 @@ public class ModelRailwaySimulation { } else { engines.sort(Comparator.comparing(Engine::getIdentifier)); for (Engine engine : engines) { - Train train = trains.values().stream().filter(x -> x.contains(engine)).findFirst().orElse(null); - if (train != null) { - Terminal.printLine(String.format("%s %s", train.getIdentifier(), engine)); - } else { - Terminal.printLine(String.format("none %s", engine)); - } + String trainId = trains.values().stream().filter(train -> train.contains(engine)) + .map(train -> Integer.toString(train.getIdentifier())) + .findFirst().orElse("none"); + Terminal.printLine(String.format("%s %s", trainId, engine)); } } } @@ -183,15 +180,12 @@ public class ModelRailwaySimulation { } else { for (Integer identifier : coaches.keySet().stream().sorted().collect(Collectors.toList())) { Coach coach = coaches.get(identifier); - Train train = trains.values().stream().filter(x -> x.contains(coach)).findFirst().orElse(null); - if (train != null) { - Terminal.printLine(String.format("%d %s %s %d %b %b", - coach.getNumericalIdentifier(), train.getIdentifier(), coach.getType(), - coach.getLength(), coach.hasCouplingFront(), coach.hasCouplingBack())); - } else { - Terminal.printLine(String.format("%d none %s %d %b %b", coach.getNumericalIdentifier(), - coach.getType(), coach.getLength(), coach.hasCouplingFront(), coach.hasCouplingBack())); - } + String trainId = trains.values().stream().filter(train -> train.contains(coach)) + .map(train -> Integer.toString(train.getIdentifier())) + .findFirst().orElse("none"); + Terminal.printLine(String.format("%d %s %s %d %b %b", + coach.getNumericalIdentifier(), trainId, coach.getType(), + coach.getLength(), coach.hasCouplingFront(), coach.hasCouplingBack())); } } } @@ -219,12 +213,10 @@ public class ModelRailwaySimulation { } else { trainSets.sort(Comparator.comparing(TrainSet::getIdentifier)); for (TrainSet trainSet : trainSets) { - Train train = trains.values().stream().filter(x -> x.contains(trainSet)).findFirst().orElse(null); - if (train != null) { - Terminal.printLine(String.format("%s %s", train.getIdentifier(), trainSet)); - } else { - Terminal.printLine(String.format("none %s", trainSet)); - } + String trainId = trains.values().stream().filter(train -> train.contains(trainSet)) + .map(train -> Integer.toString(train.getIdentifier())) + .findFirst().orElse("none"); + Terminal.printLine(String.format("%s %s", trainId, trainSet)); } } } @@ -236,14 +228,10 @@ public class ModelRailwaySimulation { */ public boolean deleteRollingStock(final String id) { RollingStock rollingStock = getRollingStock(id); - if (rollingStock == null || trains.values().stream().anyMatch(train -> train.contains(rollingStock))) { - // can not delete rolling stock in use - return false; + if (trains.values().stream().anyMatch(train -> train.contains(rollingStock))) { + return false; // can not delete rolling stock in use } - engines.remove(rollingStock); - trainSets.remove(rollingStock); - coaches.values().remove(rollingStock); - return true; + return engines.remove(rollingStock) || trainSets.remove(rollingStock) || coaches.values().remove(rollingStock); } /** @@ -252,7 +240,7 @@ public class ModelRailwaySimulation { * @return the specified rolling stock, or null if not found */ public RollingStock getRollingStock(final String id) { - if (id.matches("W\\+?\\d+")) { + if (Coach.IDENTIFIER_PATTERN.matcher(id).matches()) { int coachId = Integer.parseInt(id.substring(1)); return coaches.get(coachId); } else { @@ -433,44 +421,7 @@ public class ModelRailwaySimulation { nextOccupiedRails.put(train, train.getOccupiedRails()); } }); - // check for block collisions - trains.values().stream().filter(train -> train.isPlaced() - && collisions.stream().noneMatch(x -> x.contains(train))).forEach(train -> { - Set occupiedByThisTrain = nextOccupiedRails.get(train); - trains.values().stream().filter(x -> x != train).forEach(otherTrain -> { - Set occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); - Set occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); - if (occupiedByOtherTrain == null || occupiedByOtherTrainPreviously == null) { - return; - } - boolean anyIntersection = occupiedByOtherTrain.stream().anyMatch(occupiedByThisTrain::contains) - || occupiedByOtherTrainPreviously.stream().anyMatch(occupiedByThisTrain::contains); - if (anyIntersection || train.touches(otherTrain)) { - train.removeFromRails(); - otherTrain.removeFromRails(); - // try to find/merge existing collisions - Set existingCollision = null; - for (Set collision : collisions) { - if (collision.contains(otherTrain) || collision.contains(train)) { - if (existingCollision == null) { - existingCollision = collision; - collision.add(train); - collision.add(otherTrain); - } else { - existingCollision.addAll(collision); - existingCollision.add(train); - existingCollision.add(otherTrain); - collisions.remove(collision); - break; - } - } - } - if (existingCollision == null) { - collisions.add(new HashSet<>(Arrays.asList(train, otherTrain))); - } - } - }); - }); + checkForBlockCollisions(collisions, occupiedRails, nextOccupiedRails); return collisions; } @@ -504,38 +455,50 @@ public class ModelRailwaySimulation { nextOccupiedRails.put(train, train.getOccupiedRails()); } }); - // check for block collisions - for (Train train : trains.values()) { - if (!train.isPlaced() || collisions.stream().anyMatch(x -> x.contains(train))) { - continue; // already included in collision - } - Set occupiedByThisTrain = nextOccupiedRails.get(train); - trains.values().stream().filter(x -> x != train).forEach(otherTrain -> { - Set occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); - Set occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); - if (occupiedByOtherTrain == null || occupiedByOtherTrainPreviously == null) { - return; - } - boolean anyIntersection = occupiedByOtherTrain.stream().anyMatch(occupiedByThisTrain::contains) - || occupiedByOtherTrainPreviously.stream().anyMatch(occupiedByThisTrain::contains); - if (anyIntersection) { - train.removeFromRails(); - // try to find/merge existing collisions - Set existingCollision = null; - for (Set collision : collisions) { - if (collision.contains(otherTrain)) { + checkForBlockCollisions(collisions, occupiedRails, nextOccupiedRails); + return collisions; + } + + private void checkForBlockCollisions(List> collisions, Map> occupiedRails, + Map> nextOccupiedRails) { + trains.values().stream().filter(train -> train.isPlaced() + && collisions.stream().noneMatch(x -> x.contains(train))).forEach(train -> { + Set occupiedByThisTrain = nextOccupiedRails.get(train); + trains.values().stream().filter(x -> x != train).forEach(otherTrain -> { + Set occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); + Set occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); + if (occupiedByOtherTrain == null) { + return; + } + boolean anyIntersection = Stream.concat( + occupiedByOtherTrain.stream(), occupiedByOtherTrainPreviously.stream()) + .anyMatch(occupiedByThisTrain::contains); + if (anyIntersection || train.touches(otherTrain)) { + train.removeFromRails(); + otherTrain.removeFromRails(); + // try to find/merge existing collisions + Set existingCollision = null; + for (Set collision : collisions) { + if (collision.contains(otherTrain) || collision.contains(train)) { + if (existingCollision == null) { + existingCollision = collision; + collision.add(train); + collision.add(otherTrain); + } else { + existingCollision.addAll(collision); + existingCollision.add(train); + existingCollision.add(otherTrain); + collisions.remove(collision); + break; + } + } + } if (existingCollision == null) { - existingCollision = collision; - collision.add(train); - } else { - existingCollision.addAll(collision); + collisions.add(Stream.of(train, otherTrain).collect(Collectors.toSet())); } } - } - } - }); - } - return collisions; + }); + }); } /** @@ -552,16 +515,9 @@ public class ModelRailwaySimulation { return; } - List> collisions; - if (speed >= 0) { - collisions = IntStream.range(0, speed) - .mapToObj(step -> getCollisionsOfOneStep()) + List> collisions = IntStream.range(0, Math.abs(speed)) + .mapToObj(step -> speed >= 0 ? getCollisionsOfOneStep() : getCollisionsOfOneReverseStep()) .flatMap(List::stream).collect(Collectors.toList()); - } else { - collisions = IntStream.range(0, -speed) - .mapToObj(step -> getCollisionsOfOneReverseStep()) - .flatMap(List::stream).collect(Collectors.toList()); - } for (int id : trains.keySet().stream().sorted().collect(Collectors.toList())) { Train train = trains.get(id); @@ -582,4 +538,4 @@ public class ModelRailwaySimulation { } } } -} +} \ No newline at end of file diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java index 8b580f3..2966b92 100644 --- a/src/edu/kit/informatik/ui/CommandLine.java +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -42,7 +42,7 @@ public final class CommandLine { if (input.startsWith(EXIT)) { if (input.length() != EXIT.length()) { - Terminal.printError("space after exit command"); + Terminal.printError("input after exit command"); continue; } else { break; diff --git a/src/edu/kit/informatik/ui/command/CommandFactory.java b/src/edu/kit/informatik/ui/command/CommandFactory.java index 1969b33..5101194 100644 --- a/src/edu/kit/informatik/ui/command/CommandFactory.java +++ b/src/edu/kit/informatik/ui/command/CommandFactory.java @@ -1,5 +1,6 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.model.Coach; import edu.kit.informatik.ui.InvalidInputException; import java.util.HashMap; @@ -29,7 +30,7 @@ public final class CommandFactory { * Regex to accept one rolling stock identifier. */ public static final String ROLLING_STOCK_IDENTIFIER - = "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|W" + NUMBER; + = "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|" + Coach.IDENTIFIER_PATTERN; /** * Name of the add track command. diff --git a/src/edu/kit/informatik/ui/command/CreateEngine.java b/src/edu/kit/informatik/ui/command/CreateEngine.java index 5ab2b47..b71947c 100644 --- a/src/edu/kit/informatik/ui/command/CreateEngine.java +++ b/src/edu/kit/informatik/ui/command/CreateEngine.java @@ -1,6 +1,7 @@ package edu.kit.informatik.ui.command; import edu.kit.informatik.Terminal; +import edu.kit.informatik.model.Coach; import edu.kit.informatik.model.DieselEngine; import edu.kit.informatik.model.ElectricalEngine; import edu.kit.informatik.model.Engine; @@ -86,7 +87,7 @@ public class CreateEngine extends Command { } type = EngineType.parse(matcher.group(1)); series = matcher.group(2); - if ("W".equals(series)) { + if (Coach.IDENTIFIER_PREFIX.equals(series)) { throw new InvalidInputException("invalid engine class/series"); } name = matcher.group(3); diff --git a/src/edu/kit/informatik/ui/command/CreateTrainSet.java b/src/edu/kit/informatik/ui/command/CreateTrainSet.java index e5d53f6..430b0dc 100644 --- a/src/edu/kit/informatik/ui/command/CreateTrainSet.java +++ b/src/edu/kit/informatik/ui/command/CreateTrainSet.java @@ -1,5 +1,6 @@ package edu.kit.informatik.ui.command; +import edu.kit.informatik.model.Coach; import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.Terminal; import edu.kit.informatik.model.TrainSet; @@ -64,7 +65,7 @@ public class CreateTrainSet extends Command { throw new InvalidInputException("invalid create train-set arguments"); } series = matcher.group(1); - if ("W".equals(series)) { + if (Coach.IDENTIFIER_PREFIX.equals(series)) { throw new InvalidInputException("invalid train-set class/series"); } name = matcher.group(2); diff --git a/testcoverage_output.txt b/testcoverage_output.txt index 10f8776..faf3ec6 100644 --- a/testcoverage_output.txt +++ b/testcoverage_output.txt @@ -38,11 +38,11 @@ OK 2 OK Crash of train 2 -Error, engine identifier already used by engine +Error, engine identifier already used T2-Epsilon -Error, engine identifier already used by train set -Error, train set identifier already used by train set -Error, train set identifier already used by engine +Error, engine identifier already used +Error, train set identifier already used +Error, train set identifier already used T2-Lambda train-set T2-Lambda added to train 3 ++ @@ -129,7 +129,7 @@ Error, invalid train direction Error, invalid add track argument syntax Error, unknown command Error, invalid step argument -Error, space after exit command +Error, input after exit command OK OK OK