IDistanceMatrixRow addLocation()

in prototype/dispatch/order-dispatcher/src/main/java/com/aws/proto/dispatching/routing/DistanceMatrix.java [105:132]


        IDistanceMatrixRow addLocation(ILocation newLocation) {
            logger.trace("Adding location {}", newLocation.coordinates());
            Long start = System.currentTimeMillis();

            Map<ILocation, Distance> distancesToOthers = new ConcurrentHashMap<>();
            distancesToOthers.put(newLocation, Distance.ZERO);

            matrix.entrySet().stream().parallel().forEach(distanceRow -> {
                ILocation other = distanceRow.getKey();
                Map<ILocation, Distance> distancesFromOthers = distanceRow.getValue();
                distancesFromOthers.put(newLocation, calculateDistance(other, newLocation));
                distancesToOthers.put(other, calculateDistance(newLocation, other));
            });

            matrix.put(newLocation, distancesToOthers);
            logger.trace("Location {} added, took {}ms. Current matrix size: {}", newLocation, (System.currentTimeMillis() - start), matrix.size());

            return location -> {
                if (!distancesToOthers.containsKey(location)) {
                    throw new IllegalArgumentException(
                      "Distance from " + newLocation
                        + " to " + location
                        + " hasn't been recorded.\n"
                        + "We only know distances to " + distancesToOthers.keySet());
                }
                return distancesToOthers.get(location);
            };
        }