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);
/**
* The (unique) identifier of this coach.
* The unique positive identifier of this coach.
*/
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 couplingBack whether the coach should have a back coupling
* @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 {
super(length, couplingFront, couplingBack);
if (identifier < 1) {
throw new IllegalArgumentException("coach id has to be positive!");
}
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.InvalidInputException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* Model railway simulation. Can simulate trains on a railway network.
@ -31,17 +28,17 @@ public class ModelRailwaySimulation {
*/
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.
*/
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.
@ -111,11 +108,10 @@ public class ModelRailwaySimulation {
*/
public void createEngine(Engine newEngine) throws InvalidInputException {
final String id = newEngine.getIdentifier();
if (Stream.concat(engines.stream(), trainSets.stream())
.anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) {
if (engines.containsKey(id) || trainSets.containsKey(id)) {
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)
*/
public List<String> listEngines() {
engines.sort(Comparator.comparing(Engine::getIdentifier));
return engines.stream().map(engine -> {
return engines.values().stream().map(engine -> {
final String trainId = trainManager.getTrainContainingRollingStock(engine)
.map(train -> Integer.toString(train.getIdentifier()))
.orElse("none");
@ -169,8 +164,7 @@ public class ModelRailwaySimulation {
* @return list of coaches (never null, sometimes empty)
*/
public List<String> listCoaches() {
return coaches.keySet().stream().sorted().map(coachId -> {
final Coach coach = coaches.get(coachId);
return coaches.values().stream().map(coach -> {
final String trainId = trainManager.getTrainContainingRollingStock(coach)
.map(train -> Integer.toString(train.getIdentifier()))
.orElse("none");
@ -188,11 +182,10 @@ public class ModelRailwaySimulation {
*/
public void createTrainSet(TrainSet newTrainSet) throws InvalidInputException {
final String id = newTrainSet.getIdentifier();
if (Stream.concat(engines.stream(), trainSets.stream())
.anyMatch(rollingStock -> rollingStock.getIdentifier().equals(id))) {
if (engines.containsKey(id) || trainSets.containsKey(id)) {
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)
*/
public List<String> listTrainSets() {
trainSets.sort(Comparator.comparing(TrainSet::getIdentifier));
return trainSets.stream().map(trainSet -> {
return trainSets.values().stream().map(trainSet -> {
String trainId = trainManager.getTrainContainingRollingStock(trainSet)
.map(train -> Integer.toString(train.getIdentifier()))
.orElse("none");
@ -225,8 +217,8 @@ public class ModelRailwaySimulation {
return false; // can not delete rolling stock in use
}
// remove the rolling stock from one of the collections
return engines.remove(rollingStock.get())
|| trainSets.remove(rollingStock.get())
return engines.remove(rollingStock.get().getIdentifier()) != null
|| trainSets.remove(rollingStock.get().getIdentifier()) != null
|| coaches.values().remove(rollingStock.get());
}
@ -241,9 +233,12 @@ public class ModelRailwaySimulation {
final int coachId = Integer.parseInt(id.substring(1));
return Optional.ofNullable(coaches.get(coachId));
} else {
return Stream.concat(engines.stream(), trainSets.stream())
.filter(rollingStock -> rollingStock.getIdentifier().equals(id))
.findFirst();
final Engine engine = engines.get(id);
if (engine != null) {
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 {
/**
* Unique identifier of this rail.
* Unique positive identifier of this rail.
*/
private final int id;
@ -16,8 +16,12 @@ public abstract class Rail {
* Initialize a new rail with the specified identifier.
*
* @param id the identifier of this rail
* @throws IllegalArgumentException if the id is not positive
*/
protected Rail(int id) {
if (id < 1) {
throw new IllegalArgumentException("rail id has to be positive!");
}
this.id = id;
}
@ -36,24 +40,35 @@ public abstract class Rail {
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
* @return whether the rail currently connects to the 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
* @return whether the rail can connect to the point
*/
public abstract boolean canConnectTo(Vector2D point);
/**
* Check whether this rail can connect to another rail.
*
* @param rail rail to check for possible connection
* @return whether this rail can connect to the specified 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
* @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.
* Returns only one direction since movements have to be deterministic.
*
* @param position the position to check
* @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
* @version 1.0
*/
public abstract class RollingStock {
public abstract class RollingStock implements Comparable<RollingStock> {
/**
* Length of this rolling stock.
*/
@ -96,4 +96,15 @@ public abstract class RollingStock {
* @return textual description of this rolling stock
*/
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.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -33,7 +32,7 @@ public final class TrainManager {
/**
* 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.
@ -132,9 +131,7 @@ public final class TrainManager {
* @return sorted collection of trains
*/
public SortedMap<Integer, String> getTrains() {
return trains.keySet().stream()
.sorted()
.map(trains::get)
return trains.values().stream()
.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.Map;
import java.util.Optional;
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
* @version 1.0
@ -36,6 +37,9 @@ public final class CommandFactory {
public static final String ROLLING_STOCK_IDENTIFIER
= "(" + 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<>();
static {
@ -75,13 +79,15 @@ public final class CommandFactory {
* @throws InvalidInputException if user input is invalid
*/
public static Command getCommand(String input) throws InvalidInputException {
for (final Map.Entry<String, Supplier<Command>> entry : COMMANDS.entrySet()) {
if (input.startsWith(entry.getKey())) {
final Command command = entry.getValue().get();
command.parse(input);
return command;
}
final Optional<Command> matchingCommand = COMMANDS.keySet().stream()
.filter(input::startsWith)
.map(COMMANDS::get).map(Supplier::get)
.findAny();
if (!matchingCommand.isPresent()) {
throw new InvalidInputException("unknown command");
}
throw new InvalidInputException("unknown command");
final Command command = matchingCommand.get();
command.parse(input);
return command;
}
}