RailNodeShortestPath originShortestPath = new RailNodeShortestPath();
originShortestPath.setOrigin(this);
originShortestPath.setDestination(this);
originShortestPath.setDistance(0);
originShortestPath.resetRailPathList();
RailPath originRailPath = new RailPath(new ArrayList<RailArc>(0));
originShortestPath.addRailPath(originRailPath);
shortestPathMap.put(this, originShortestPath);
unvisitedShortestPathList.add(originShortestPath);
while (!unvisitedShortestPathList.isEmpty()) {
RailNodeShortestPath campingShortestPath = unvisitedShortestPathList.remove(0);
for (RailArc nextRailArc : campingShortestPath.getDestination().getOriginatingRailArcList()) {
RailNode nextNode = nextRailArc.getDestination();
int nextDistance = campingShortestPath.getDistance() + nextRailArc.getDistance();
RailNodeShortestPath nextShortestPath = shortestPathMap.get(nextNode);
if (nextShortestPath == null) {
nextShortestPath = new RailNodeShortestPath();
nextShortestPath.setOrigin(this);
nextShortestPath.setDestination(nextNode);
nextShortestPath.setDistance(Integer.MAX_VALUE);
shortestPathMap.put(nextNode, nextShortestPath);
unvisitedShortestPathList.add(nextShortestPath);
}
if (nextDistance <= nextShortestPath.getDistance()) {
if (nextDistance < nextShortestPath.getDistance()) {
nextShortestPath.setDistance(nextDistance);
nextShortestPath.resetRailPathList();
}
for (RailPath campingRailPath : campingShortestPath.getRailPathList()) {
List<RailArc> railArcList = new ArrayList<RailArc>(campingRailPath.getRailArcList());
railArcList.add(nextRailArc);
RailPath nextRailPath = new RailPath(railArcList);
nextShortestPath.addRailPath(nextRailPath);
}
}
}
Collections.sort(unvisitedShortestPathList, new Comparator<RailNodeShortestPath>() {