/** * Gets the shortest route between two baggage points. * * @param source The baggage point from which the bag has to be routed. * @param destination The baggage point to which the bag has to be routed. * @return A String holding a delimiter-separated list of baggage points that forms the shortest * route. */ public String route(BaggagePoint source, BaggagePoint destination) { ArgumentValidator.assertNotNull("Baggage point source", source); ArgumentValidator.assertNotNull("Baggage point destination", destination); if (!shortestPathsNetwork.containsKey(source)) { throw new IllegalArgumentException( String.format("Baggage point source %s cannot be found.", source.getId())); } if (!shortestPathsNetwork.containsKey(destination)) { throw new IllegalArgumentException( String.format("Baggage point destination %s cannot be found.", destination.getId())); } String shortestRoute; if (source.equals(destination)) { List<BaggagePoint> path = new ArrayList<BaggagePoint>(); path.add(source); path.add(source); shortestRoute = new ShortestRoute(0d, path).toString(); } else { if (shortestPathsNetwork.get(source).containsKey(destination)) { shortestRoute = shortestPathsNetwork.get(source).get(destination).toString(); } else { shortestRoute = String.format( "%s %s : %s", source.getId(), destination.getId(), OUTPUT_DISTANCE_FOR_UNCONNECTED_POINTS); } } return shortestRoute; }
private Map<BaggagePoint, ShortestRoute> getSavedShortestPaths(BaggagePoint baggagePoint) { Map<BaggagePoint, ShortestRoute> savedShortestPaths; if (shortestPathsNetwork.containsKey(baggagePoint)) { savedShortestPaths = shortestPathsNetwork.get(baggagePoint); LOGGER.fine("Entry exists for shortest paths for : " + baggagePoint.getId()); } else { savedShortestPaths = new HashMap<BaggagePoint, ShortestRoute>(); shortestPathsNetwork.put(baggagePoint, savedShortestPaths); LOGGER.fine("Added entry for shortest path for : " + baggagePoint.getId()); } return savedShortestPaths; }
private void saveShortestPath( Map<BaggagePoint, ShortestRoute> savedShortestPaths, BaggagePoint destination, List<BaggagePoint> path, double distance) { if (savedShortestPaths.containsKey(destination)) { LOGGER.fine("Entry exists for shortest path to : " + destination.getId()); ShortestRoute savedShortestPath = savedShortestPaths.get(destination); if (savedShortestPath.distance > distance) { savedShortestPath.distance = distance; savedShortestPath.path = path; } } else { LOGGER.fine("Adding entry for shortest path to : " + destination.getId()); savedShortestPaths.put(destination, new ShortestRoute(distance, path)); } }
@Override public String toString() { StringBuffer shortestPath = new StringBuffer(); if (null != path) { for (BaggagePoint baggagePoint : path) { shortestPath.append(String.format("%s ", baggagePoint.getId())); } shortestPath.append(String.format(": %s", decimalFormat.format(distance))); } return shortestPath.toString(); }