Checkstyle

This commit is contained in:
Arne Keller 2020-02-17 13:59:42 +01:00
parent efcd3a721b
commit bb9d014508
3 changed files with 37 additions and 63 deletions

View File

@ -64,11 +64,9 @@ public class Coach extends RollingStock {
*/ */
public Coach(final int identifier, final CoachType type, final int length, public Coach(final int identifier, final CoachType type, final int length,
final boolean couplingFront, final boolean couplingBack) { final boolean couplingFront, final boolean couplingBack) {
super(length, couplingFront, couplingBack);
this.identifier = identifier; this.identifier = identifier;
this.type = type; this.type = type;
super.length = length;
super.couplingFront = couplingFront;
super.couplingBack = couplingBack;
} }
@Override @Override

View File

@ -2,12 +2,13 @@ package edu.kit.informatik.model;
import edu.kit.informatik.Terminal; import edu.kit.informatik.Terminal;
import java.util.ArrayList; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.Map;
import java.util.Queue; import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
/** /**
* A rail network consisting of tracks and switches. Is only concerned with rails, does not process trains. * A rail network consisting of tracks and switches. Is only concerned with rails, does not process trains.
@ -19,7 +20,7 @@ public class RailwayNetwork {
/** /**
* Rail in this railway network. Usually contains tracks and switches. * Rail in this railway network. Usually contains tracks and switches.
*/ */
private List<Rail> rails = new ArrayList<>(); private final Map<Integer, Rail> rails = new HashMap<>();
/** /**
* Add a new track to the rail network. * Add a new track to the rail network.
@ -31,25 +32,25 @@ public class RailwayNetwork {
if (start.distanceTo(end) == 0) { if (start.distanceTo(end) == 0) {
return -1; return -1;
} }
long startPossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(start)).count(); long startPossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(start)).count();
long endPossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(end)).count(); long endPossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end)).count();
if (startPossibleConnections == 2 || endPossibleConnections == 2) { if (startPossibleConnections == 2 || endPossibleConnections == 2) {
return -1; // TODO better error msg? return -1; // TODO better error msg?
} }
if (rails.isEmpty()) { if (rails.isEmpty()) {
Track newTrack = new Track(start, end, 1); Track newTrack = new Track(start, end, 1);
rails.add(newTrack); rails.put(1, newTrack);
return newTrack.id; return newTrack.getIdentifier();
} else { } else {
for (Rail rail : rails) { for (Rail rail : rails.values()) {
if (rail.canConnectTo(start) || rail.canConnectTo(end)) { if (rail.canConnectTo(start) || rail.canConnectTo(end)) {
int id = getNextRailIdentifier(); int id = getNextRailIdentifier();
if (id < 0) { if (id < 0) {
return -1; return -1;
} }
Track newTrack = new Track(start, end, id); Track newTrack = new Track(start, end, id);
rails.add(newTrack.id - 1, newTrack); rails.put(id, newTrack);
return newTrack.id; return newTrack.getIdentifier();
} }
} }
return -1; return -1;
@ -69,24 +70,24 @@ public class RailwayNetwork {
} }
if (rails.isEmpty()) { if (rails.isEmpty()) {
Switch newSwitch = new Switch(start, end1, end2, 1); Switch newSwitch = new Switch(start, end1, end2, 1);
rails.add(newSwitch); rails.put(1, newSwitch);
return newSwitch.id; return newSwitch.getIdentifier();
} else { } else {
long startPossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(start)).count(); long startPossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(start)).count();
long end1PossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(end1)).count(); long end1PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end1)).count();
long end2PossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(end2)).count(); long end2PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end2)).count();
if (startPossibleConnections == 2 || end1PossibleConnections == 2 || end2PossibleConnections == 2) { if (startPossibleConnections == 2 || end1PossibleConnections == 2 || end2PossibleConnections == 2) {
return -1; // TODO better error msg? return -1; // TODO better error msg?
} }
for (Rail rail : rails) { for (Rail rail : rails.values()) {
if (rail.canConnectTo(start) || rail.canConnectTo(end1) || rail.canConnectTo(end2)) { if (rail.canConnectTo(start) || rail.canConnectTo(end1) || rail.canConnectTo(end2)) {
int id = getNextRailIdentifier(); int id = getNextRailIdentifier();
if (id < 0) { if (id < 0) {
return -1; return -1;
} }
Switch newSwitch = new Switch(start, end1, end2, id); Switch newSwitch = new Switch(start, end1, end2, id);
rails.add(newSwitch.id - 1, newSwitch); rails.put(id, newSwitch);
return newSwitch.id; return newSwitch.getIdentifier();
} }
} }
return -1; return -1;
@ -102,14 +103,14 @@ public class RailwayNetwork {
if (rails.size() == 0) { if (rails.size() == 0) {
return false; return false;
} else if (rails.size() == 1) { } else if (rails.size() == 1) {
if (rails.get(0).id == id) { if (rails.get(id) != null) {
rails.clear(); rails.clear();
return true; return true;
} else { } else {
return false; return false;
} }
} }
Rail toRemove = getRail(id); Rail toRemove = rails.get(id);
if (toRemove == null) { if (toRemove == null) {
return false; return false;
} }
@ -117,7 +118,7 @@ public class RailwayNetwork {
// locate one other rail: TODO use rail.connectedrails // locate one other rail: TODO use rail.connectedrails
Rail otherRail = null; Rail otherRail = null;
for (Rail anotherRail : rails) { for (Rail anotherRail : rails.values()) {
if (anotherRail.getIdentifier() != id && toRemove.canConnectToRail(anotherRail)) { if (anotherRail.getIdentifier() != id && toRemove.canConnectToRail(anotherRail)) {
otherRail = anotherRail; otherRail = anotherRail;
break; break;
@ -136,7 +137,7 @@ public class RailwayNetwork {
while (!toProcess.isEmpty()) { while (!toProcess.isEmpty()) {
Rail connected = toProcess.poll(); Rail connected = toProcess.poll();
for (Rail other : rails) { for (Rail other : rails.values()) {
if (other != toRemove && !connectedRails.contains(other) && other.canConnectToRail(connected)) { if (other != toRemove && !connectedRails.contains(other) && other.canConnectToRail(connected)) {
connectedRails.add(other); connectedRails.add(other);
toProcess.offer(other); toProcess.offer(other);
@ -145,7 +146,7 @@ public class RailwayNetwork {
} }
if (connectedRails.size() == rails.size() - 1) { if (connectedRails.size() == rails.size() - 1) {
rails.remove(toRemove); rails.remove(id);
return true; return true;
} else { } else {
return false; return false;
@ -159,7 +160,7 @@ public class RailwayNetwork {
* @return whether the switch could be set * @return whether the switch could be set
*/ */
public boolean setSwitch(final int id, final Vector2D position) { public boolean setSwitch(final int id, final Vector2D position) {
Rail toSwitch = getRail(id); Rail toSwitch = rails.get(id);
if (toSwitch != null) { if (toSwitch != null) {
return toSwitch.switchTo(position); return toSwitch.switchTo(position);
} }
@ -174,8 +175,8 @@ public class RailwayNetwork {
if (rails.isEmpty()) { if (rails.isEmpty()) {
Terminal.printLine("No track exists"); Terminal.printLine("No track exists");
} else { } else {
for (Rail rail : rails) { for (int id : rails.keySet().stream().sorted().collect(Collectors.toList())) {
Terminal.printLine(rail.toString()); Terminal.printLine(rails.get(id));
} }
} }
} }
@ -184,31 +185,13 @@ public class RailwayNetwork {
* @return the next positive rail identifier, or -1 if none available * @return the next positive rail identifier, or -1 if none available
*/ */
private int getNextRailIdentifier() { private int getNextRailIdentifier() {
int id = 1; for (int i = 1; i > 0; i++) {
for (Rail rail : rails) { if (!rails.containsKey(i)) {
if (rail.id == id) { return i;
id++;
} }
} }
if (id > 0) {
return id;
} else {
return -1; return -1;
} }
}
/**
* @param id identifier of the rail to find
* @return the specified rail, or null if not found
*/
private Rail getRail(final int id) {
for (Rail rail : rails) {
if (rail.getIdentifier() == id) {
return rail;
}
}
return null;
}
/** /**
* Move a position along the rails of this rail network in the specified direction. * Move a position along the rails of this rail network in the specified direction.
@ -262,12 +245,7 @@ public class RailwayNetwork {
* @return the rail that contains the position, null if none found * @return the rail that contains the position, null if none found
*/ */
public Rail findContainingRail(final Vector2D position) { public Rail findContainingRail(final Vector2D position) {
for (Rail rail : rails) { return rails.values().stream().filter(rail -> rail.contains(position)).findFirst().orElse(null);
if (rail.contains(position)) {
return rail;
}
}
return null;
} }
/** /**
@ -276,7 +254,7 @@ public class RailwayNetwork {
* @return the rail(s) that touch this position * @return the rail(s) that touch this position
*/ */
public Rail[] findTouchingRails(final Vector2D position) { public Rail[] findTouchingRails(final Vector2D position) {
return rails.stream().filter((rail) -> rail.canConnectTo(position)).toArray(Rail[]::new); return rails.values().stream().filter((rail) -> rail.canConnectTo(position)).toArray(Rail[]::new);
} }
/** /**
@ -284,6 +262,6 @@ public class RailwayNetwork {
* @return whether the rail network is ready * @return whether the rail network is ready
*/ */
public boolean isReadyForTrains() { public boolean isReadyForTrains() {
return rails.stream().allMatch(Rail::isReadyForTrains); return rails.values().stream().allMatch(Rail::isReadyForTrains);
} }
} }

View File

@ -40,11 +40,9 @@ public class TrainSet extends RollingStock {
*/ */
public TrainSet(final String series, final String name, final int length, public TrainSet(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) { final boolean couplingFront, final boolean couplingBack) {
super(length, couplingFront, couplingBack);
this.name = name; this.name = name;
this.series = series; this.series = series;
super.length = length;
super.couplingFront = couplingFront;
super.couplingBack = couplingBack;
} }
@Override @Override
@ -54,7 +52,7 @@ public class TrainSet extends RollingStock {
@Override @Override
public String toString() { public String toString() {
return String.format("%s %s %d %b %b", series, name, length, couplingFront, couplingBack); return String.format("%s %s %d %b %b", series, name, getLength(), hasCouplingFront(), hasCouplingBack());
} }
@Override @Override