From bb9d0145084bebc1ba0aaa227bb22478fc371f40 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 17 Feb 2020 13:59:42 +0100 Subject: [PATCH] Checkstyle --- src/edu/kit/informatik/model/Coach.java | 4 +- .../kit/informatik/model/RailwayNetwork.java | 90 +++++++------------ src/edu/kit/informatik/model/TrainSet.java | 6 +- 3 files changed, 37 insertions(+), 63 deletions(-) diff --git a/src/edu/kit/informatik/model/Coach.java b/src/edu/kit/informatik/model/Coach.java index d4ded99..b0498e8 100644 --- a/src/edu/kit/informatik/model/Coach.java +++ b/src/edu/kit/informatik/model/Coach.java @@ -64,11 +64,9 @@ public class Coach extends RollingStock { */ public Coach(final int identifier, final CoachType type, final int length, final boolean couplingFront, final boolean couplingBack) { + super(length, couplingFront, couplingBack); this.identifier = identifier; this.type = type; - super.length = length; - super.couplingFront = couplingFront; - super.couplingBack = couplingBack; } @Override diff --git a/src/edu/kit/informatik/model/RailwayNetwork.java b/src/edu/kit/informatik/model/RailwayNetwork.java index ecabf2a..1dafb5d 100644 --- a/src/edu/kit/informatik/model/RailwayNetwork.java +++ b/src/edu/kit/informatik/model/RailwayNetwork.java @@ -2,12 +2,13 @@ package edu.kit.informatik.model; import edu.kit.informatik.Terminal; -import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; -import java.util.List; +import java.util.Map; import java.util.Queue; 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. @@ -19,7 +20,7 @@ public class RailwayNetwork { /** * Rail in this railway network. Usually contains tracks and switches. */ - private List rails = new ArrayList<>(); + private final Map rails = new HashMap<>(); /** * Add a new track to the rail network. @@ -31,25 +32,25 @@ public class RailwayNetwork { if (start.distanceTo(end) == 0) { return -1; } - long startPossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(start)).count(); - long endPossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(end)).count(); + long startPossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(start)).count(); + long endPossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end)).count(); if (startPossibleConnections == 2 || endPossibleConnections == 2) { return -1; // TODO better error msg? } if (rails.isEmpty()) { Track newTrack = new Track(start, end, 1); - rails.add(newTrack); - return newTrack.id; + rails.put(1, newTrack); + return newTrack.getIdentifier(); } else { - for (Rail rail : rails) { + for (Rail rail : rails.values()) { if (rail.canConnectTo(start) || rail.canConnectTo(end)) { int id = getNextRailIdentifier(); if (id < 0) { return -1; } Track newTrack = new Track(start, end, id); - rails.add(newTrack.id - 1, newTrack); - return newTrack.id; + rails.put(id, newTrack); + return newTrack.getIdentifier(); } } return -1; @@ -69,24 +70,24 @@ public class RailwayNetwork { } if (rails.isEmpty()) { Switch newSwitch = new Switch(start, end1, end2, 1); - rails.add(newSwitch); - return newSwitch.id; + rails.put(1, newSwitch); + return newSwitch.getIdentifier(); } else { - long startPossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(start)).count(); - long end1PossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(end1)).count(); - long end2PossibleConnections = rails.stream().filter((rail) -> rail.canConnectTo(end2)).count(); + long startPossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(start)).count(); + long end1PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end1)).count(); + long end2PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end2)).count(); if (startPossibleConnections == 2 || end1PossibleConnections == 2 || end2PossibleConnections == 2) { 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)) { int id = getNextRailIdentifier(); if (id < 0) { return -1; } Switch newSwitch = new Switch(start, end1, end2, id); - rails.add(newSwitch.id - 1, newSwitch); - return newSwitch.id; + rails.put(id, newSwitch); + return newSwitch.getIdentifier(); } } return -1; @@ -102,14 +103,14 @@ public class RailwayNetwork { if (rails.size() == 0) { return false; } else if (rails.size() == 1) { - if (rails.get(0).id == id) { + if (rails.get(id) != null) { rails.clear(); return true; } else { return false; } } - Rail toRemove = getRail(id); + Rail toRemove = rails.get(id); if (toRemove == null) { return false; } @@ -117,7 +118,7 @@ public class RailwayNetwork { // locate one other rail: TODO use rail.connectedrails Rail otherRail = null; - for (Rail anotherRail : rails) { + for (Rail anotherRail : rails.values()) { if (anotherRail.getIdentifier() != id && toRemove.canConnectToRail(anotherRail)) { otherRail = anotherRail; break; @@ -136,7 +137,7 @@ public class RailwayNetwork { while (!toProcess.isEmpty()) { Rail connected = toProcess.poll(); - for (Rail other : rails) { + for (Rail other : rails.values()) { if (other != toRemove && !connectedRails.contains(other) && other.canConnectToRail(connected)) { connectedRails.add(other); toProcess.offer(other); @@ -145,7 +146,7 @@ public class RailwayNetwork { } if (connectedRails.size() == rails.size() - 1) { - rails.remove(toRemove); + rails.remove(id); return true; } else { return false; @@ -159,7 +160,7 @@ public class RailwayNetwork { * @return whether the switch could be set */ public boolean setSwitch(final int id, final Vector2D position) { - Rail toSwitch = getRail(id); + Rail toSwitch = rails.get(id); if (toSwitch != null) { return toSwitch.switchTo(position); } @@ -174,8 +175,8 @@ public class RailwayNetwork { if (rails.isEmpty()) { Terminal.printLine("No track exists"); } else { - for (Rail rail : rails) { - Terminal.printLine(rail.toString()); + for (int id : rails.keySet().stream().sorted().collect(Collectors.toList())) { + Terminal.printLine(rails.get(id)); } } } @@ -184,30 +185,12 @@ public class RailwayNetwork { * @return the next positive rail identifier, or -1 if none available */ private int getNextRailIdentifier() { - int id = 1; - for (Rail rail : rails) { - if (rail.id == id) { - id++; + for (int i = 1; i > 0; i++) { + if (!rails.containsKey(i)) { + return i; } } - if (id > 0) { - return id; - } else { - 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; + return -1; } /** @@ -262,12 +245,7 @@ public class RailwayNetwork { * @return the rail that contains the position, null if none found */ public Rail findContainingRail(final Vector2D position) { - for (Rail rail : rails) { - if (rail.contains(position)) { - return rail; - } - } - return null; + return rails.values().stream().filter(rail -> rail.contains(position)).findFirst().orElse(null); } /** @@ -276,7 +254,7 @@ public class RailwayNetwork { * @return the rail(s) that touch this 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 */ public boolean isReadyForTrains() { - return rails.stream().allMatch(Rail::isReadyForTrains); + return rails.values().stream().allMatch(Rail::isReadyForTrains); } } diff --git a/src/edu/kit/informatik/model/TrainSet.java b/src/edu/kit/informatik/model/TrainSet.java index d78eba2..e6e1d83 100644 --- a/src/edu/kit/informatik/model/TrainSet.java +++ b/src/edu/kit/informatik/model/TrainSet.java @@ -40,11 +40,9 @@ public class TrainSet extends RollingStock { */ public TrainSet(final String series, final String name, final int length, final boolean couplingFront, final boolean couplingBack) { + super(length, couplingFront, couplingBack); this.name = name; this.series = series; - super.length = length; - super.couplingFront = couplingFront; - super.couplingBack = couplingBack; } @Override @@ -54,7 +52,7 @@ public class TrainSet extends RollingStock { @Override 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