Checkstyle

This commit is contained in:
Arne Keller 2020-03-09 10:41:53 +01:00
parent f32cff8d25
commit e0aea8543c
6 changed files with 71 additions and 42 deletions

View File

@ -23,7 +23,7 @@ public abstract class Coach extends RollingStock {
public static final Pattern IDENTIFIER_PATTERN = Pattern.compile(IDENTIFIER_PREFIX + NUMBER); public static final Pattern IDENTIFIER_PATTERN = Pattern.compile(IDENTIFIER_PREFIX + NUMBER);
/** /**
* The (unique) identifier of this coach. * The unique positive identifier of this coach.
*/ */
private final int identifier; private final int identifier;
@ -35,9 +35,13 @@ public abstract class Coach extends RollingStock {
* @param couplingFront whether the coach should have a front coupling * @param couplingFront whether the coach should have a front coupling
* @param couplingBack whether the coach should have a back coupling * @param couplingBack whether the coach should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach) * @throws InvalidInputException on invalid user input (zero-sized coach)
* @throws IllegalArgumentException if the identifier is not positive
*/ */
public Coach(int identifier, int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException { public Coach(int identifier, int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException {
super(length, couplingFront, couplingBack); super(length, couplingFront, couplingBack);
if (identifier < 1) {
throw new IllegalArgumentException("coach id has to be positive!");
}
this.identifier = identifier; this.identifier = identifier;
} }

View File

@ -3,17 +3,14 @@ package edu.kit.informatik.modelrailwaysimulator.model;
import edu.kit.informatik.modelrailwaysimulator.ui.CoachType; import edu.kit.informatik.modelrailwaysimulator.ui.CoachType;
import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream;
/** /**
* Model railway simulation. Can simulate trains on a railway network. * Model railway simulation. Can simulate trains on a railway network.
@ -31,17 +28,17 @@ public class ModelRailwaySimulation {
*/ */
private final TrainManager trainManager = new TrainManager(railNetwork); private final TrainManager trainManager = new TrainManager(railNetwork);
/** /**
* List of engines used in the simulation. * Collection of engines used in the simulation keyed by their identifier.
*/ */
private final List<Engine> engines = new ArrayList<>(); private final Map<String, Engine> engines = new TreeMap<>();
/** /**
* List of train sets used in the simulation. * Collection of train sets used in the simulation keyed by their identifier.
*/ */
private final List<TrainSet> trainSets = new ArrayList<>(); private final Map<String, TrainSet> trainSets = new TreeMap<>();
/** /**
* Map of coaches used in the simulation. * Map of coaches used in the simulation.
*/ */
private final Map<Integer, Coach> coaches = new HashMap<>(); private final Map<Integer, Coach> coaches = new TreeMap<>();
/** /**
* Add a new track to the rail network of this simulation. * Add a new track to the rail network of this simulation.
@ -111,11 +108,10 @@ public class ModelRailwaySimulation {
*/ */
public void createEngine(Engine newEngine) throws InvalidInputException { public void createEngine(Engine newEngine) throws InvalidInputException {
final String id = newEngine.getIdentifier(); final String id = newEngine.getIdentifier();
if (Stream.concat(engines.stream(), trainSets.stream()) if (engines.containsKey(id) || trainSets.containsKey(id)) {
.anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) {
throw new InvalidInputException("engine identifier already used"); throw new InvalidInputException("engine identifier already used");
} }
engines.add(newEngine); engines.put(id, newEngine);
} }
/** /**
@ -124,8 +120,7 @@ public class ModelRailwaySimulation {
* @return list of engines (never null, sometimes empty) * @return list of engines (never null, sometimes empty)
*/ */
public List<String> listEngines() { public List<String> listEngines() {
engines.sort(Comparator.comparing(Engine::getIdentifier)); return engines.values().stream().map(engine -> {
return engines.stream().map(engine -> {
final String trainId = trainManager.getTrainContainingRollingStock(engine) final String trainId = trainManager.getTrainContainingRollingStock(engine)
.map(train -> Integer.toString(train.getIdentifier())) .map(train -> Integer.toString(train.getIdentifier()))
.orElse("none"); .orElse("none");
@ -169,8 +164,7 @@ public class ModelRailwaySimulation {
* @return list of coaches (never null, sometimes empty) * @return list of coaches (never null, sometimes empty)
*/ */
public List<String> listCoaches() { public List<String> listCoaches() {
return coaches.keySet().stream().sorted().map(coachId -> { return coaches.values().stream().map(coach -> {
final Coach coach = coaches.get(coachId);
final String trainId = trainManager.getTrainContainingRollingStock(coach) final String trainId = trainManager.getTrainContainingRollingStock(coach)
.map(train -> Integer.toString(train.getIdentifier())) .map(train -> Integer.toString(train.getIdentifier()))
.orElse("none"); .orElse("none");
@ -188,11 +182,10 @@ public class ModelRailwaySimulation {
*/ */
public void createTrainSet(TrainSet newTrainSet) throws InvalidInputException { public void createTrainSet(TrainSet newTrainSet) throws InvalidInputException {
final String id = newTrainSet.getIdentifier(); final String id = newTrainSet.getIdentifier();
if (Stream.concat(engines.stream(), trainSets.stream()) if (engines.containsKey(id) || trainSets.containsKey(id)) {
.anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) {
throw new InvalidInputException("train set identifier already used"); throw new InvalidInputException("train set identifier already used");
} }
trainSets.add(newTrainSet); trainSets.put(id, newTrainSet);
} }
/** /**
@ -201,8 +194,7 @@ public class ModelRailwaySimulation {
* @return list of train sets (never null, sometimes empty) * @return list of train sets (never null, sometimes empty)
*/ */
public List<String> listTrainSets() { public List<String> listTrainSets() {
trainSets.sort(Comparator.comparing(TrainSet::getIdentifier)); return trainSets.values().stream().map(trainSet -> {
return trainSets.stream().map(trainSet -> {
String trainId = trainManager.getTrainContainingRollingStock(trainSet) String trainId = trainManager.getTrainContainingRollingStock(trainSet)
.map(train -> Integer.toString(train.getIdentifier())) .map(train -> Integer.toString(train.getIdentifier()))
.orElse("none"); .orElse("none");
@ -225,8 +217,8 @@ public class ModelRailwaySimulation {
return false; // can not delete rolling stock in use return false; // can not delete rolling stock in use
} }
// remove the rolling stock from one of the collections // remove the rolling stock from one of the collections
return engines.remove(rollingStock.get()) return engines.remove(rollingStock.get().getIdentifier()) != null
|| trainSets.remove(rollingStock.get()) || trainSets.remove(rollingStock.get().getIdentifier()) != null
|| coaches.values().remove(rollingStock.get()); || coaches.values().remove(rollingStock.get());
} }
@ -241,9 +233,12 @@ public class ModelRailwaySimulation {
final int coachId = Integer.parseInt(id.substring(1)); final int coachId = Integer.parseInt(id.substring(1));
return Optional.ofNullable(coaches.get(coachId)); return Optional.ofNullable(coaches.get(coachId));
} else { } else {
return Stream.concat(engines.stream(), trainSets.stream()) final Engine engine = engines.get(id);
.filter(rollingStock -> rollingStock.getIdentifier().equals(id)) if (engine != null) {
.findFirst(); return Optional.of(engine);
}
final TrainSet trainSet = trainSets.get(id);
return Optional.ofNullable(trainSet);
} }
} }

View File

@ -8,7 +8,7 @@ package edu.kit.informatik.modelrailwaysimulator.model;
*/ */
public abstract class Rail { public abstract class Rail {
/** /**
* Unique identifier of this rail. * Unique positive identifier of this rail.
*/ */
private final int id; private final int id;
@ -16,8 +16,12 @@ public abstract class Rail {
* Initialize a new rail with the specified identifier. * Initialize a new rail with the specified identifier.
* *
* @param id the identifier of this rail * @param id the identifier of this rail
* @throws IllegalArgumentException if the id is not positive
*/ */
protected Rail(int id) { protected Rail(int id) {
if (id < 1) {
throw new IllegalArgumentException("rail id has to be positive!");
}
this.id = id; this.id = id;
} }
@ -36,24 +40,35 @@ public abstract class Rail {
public abstract long getLength(); public abstract long getLength();
/** /**
* Check whether this rail connects to a specified position. Note that this will return false if the rail only
* contains the position!
*
* @param point point to check for connection * @param point point to check for connection
* @return whether the rail currently connects to the point * @return whether the rail currently connects to the point
*/ */
public abstract boolean connectsTo(Vector2D point); public abstract boolean connectsTo(Vector2D point);
/** /**
* Check whether this rail can connect to a specified position. Only relevant for rails with switching capabilities.
* Note that this will return false if the rail only contains the position!
*
* @param point point to check for connection * @param point point to check for connection
* @return whether the rail can connect to the point * @return whether the rail can connect to the point
*/ */
public abstract boolean canConnectTo(Vector2D point); public abstract boolean canConnectTo(Vector2D point);
/** /**
* Check whether this rail can connect to another rail.
*
* @param rail rail to check for possible connection * @param rail rail to check for possible connection
* @return whether this rail can connect to the specified rail * @return whether this rail can connect to the specified rail
*/ */
public abstract boolean canConnectToRail(Rail rail); public abstract boolean canConnectToRail(Rail rail);
/** /**
* Check whether this rail contains a position. Note that this will return false if the rail only touches the
* position.
*
* @param position the point to check * @param position the point to check
* @return whether the point is inside this rail (not on the edge) * @return whether the point is inside this rail (not on the edge)
*/ */
@ -71,6 +86,7 @@ public abstract class Rail {
/** /**
* Get the direction trains can move on this rail starting at the specified position. * Get the direction trains can move on this rail starting at the specified position.
* Returns only one direction since movements have to be deterministic.
* *
* @param position the position to check * @param position the position to check
* @return the direction trains can move starting at that position * @return the direction trains can move starting at that position

View File

@ -8,7 +8,7 @@ import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public abstract class RollingStock { public abstract class RollingStock implements Comparable<RollingStock> {
/** /**
* Length of this rolling stock. * Length of this rolling stock.
*/ */
@ -96,4 +96,15 @@ public abstract class RollingStock {
* @return textual description of this rolling stock * @return textual description of this rolling stock
*/ */
public abstract String description(); public abstract String description();
/**
* Compare two rolling stocks based on their textual identifier.
*
* @param other rolling stock to compare to
* @return lexicographic comparison of the identifiers
*/
@Override
public int compareTo(RollingStock other) {
return this.getIdentifier().compareTo(other.getIdentifier());
}
} }

View File

@ -5,7 +5,6 @@ import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -33,7 +32,7 @@ public final class TrainManager {
/** /**
* Map of trains simulated. The train identifier is used as key. * Map of trains simulated. The train identifier is used as key.
*/ */
private final Map<Integer, Train> trains = new HashMap<>(); private final Map<Integer, Train> trains = new TreeMap<>();
/** /**
* Construct a new train manager that will operate on the provided rail network. * Construct a new train manager that will operate on the provided rail network.
@ -132,9 +131,7 @@ public final class TrainManager {
* @return sorted collection of trains * @return sorted collection of trains
*/ */
public SortedMap<Integer, String> getTrains() { public SortedMap<Integer, String> getTrains() {
return trains.keySet().stream() return trains.values().stream()
.sorted()
.map(trains::get)
.collect(Collectors.toMap(Train::getIdentifier, Object::toString, (id1, id2) -> id1, TreeMap::new)); .collect(Collectors.toMap(Train::getIdentifier, Object::toString, (id1, id2) -> id1, TreeMap::new));
} }

View File

@ -5,10 +5,11 @@ import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
* Factory used to parse user input into commands. * Utility class used to parse user input into commands.
* *
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
@ -36,6 +37,9 @@ public final class CommandFactory {
public static final String ROLLING_STOCK_IDENTIFIER public static final String ROLLING_STOCK_IDENTIFIER
= "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|" + Coach.IDENTIFIER_PATTERN; = "(" + ALPHANUMERIC_WORD + "-" + ALPHANUMERIC_WORD + ")|" + Coach.IDENTIFIER_PATTERN;
/**
* Map of command names (prefixes) to Command constructors.
*/
private static final Map<String, Supplier<Command>> COMMANDS = new HashMap<>(); private static final Map<String, Supplier<Command>> COMMANDS = new HashMap<>();
static { static {
@ -75,13 +79,15 @@ public final class CommandFactory {
* @throws InvalidInputException if user input is invalid * @throws InvalidInputException if user input is invalid
*/ */
public static Command getCommand(String input) throws InvalidInputException { public static Command getCommand(String input) throws InvalidInputException {
for (final Map.Entry<String, Supplier<Command>> entry : COMMANDS.entrySet()) { final Optional<Command> matchingCommand = COMMANDS.keySet().stream()
if (input.startsWith(entry.getKey())) { .filter(input::startsWith)
final Command command = entry.getValue().get(); .map(COMMANDS::get).map(Supplier::get)
.findAny();
if (!matchingCommand.isPresent()) {
throw new InvalidInputException("unknown command");
}
final Command command = matchingCommand.get();
command.parse(input); command.parse(input);
return command; return command;
} }
} }
throw new InvalidInputException("unknown command");
}
}