Source:
Result resolve(Collection<Integer> nodeIds) { final Criteria idCriteria = new CriteriaBuilder(OnmsNode.class).in("id", nodeIds).toCriteria(); final List<OnmsNode> nodes = nodeDao.findMatching(idCriteria); final List<OnmsNode> nodesWithGeoLocation = nodes.stream() .filter(node -> geoLocation(node) != null) .collect(Collectors.toList()); final List<OnmsNode> nodesWithLongLat = nodesWithGeoLocation.stream() .filter(node -> { OnmsGeolocation geoLocation = geoLocation(node); return geoLocation != null && geoLocation.getLatitude() != null && geoLocation.getLongitude() != null; }) .collect(Collectors.toList()); final List<OnmsNode> nodesWithAddress = nodesWithGeoLocation.stream() .filter(node -> !Strings.isNullOrEmpty(geoLocation(node).asAddressString())) .collect(Collectors.toList()); Map<Integer, Coordinates> nodeIdToCoordinateMapping = new HashMap<>(); for (OnmsNode eachNode : nodesWithAddress) { final String addressString = geoLocation(eachNode).asAddressString(); try { org.opennms.features.geocoder.Coordinates coordinates = geocoderService.getCoordinates(addressString); if (coordinates != null) { nodeIdToCoordinateMapping.put(eachNode.getId(), new Coordinates(coordinates.getLongitude(), coordinates.getLatitude())); } } catch (GeocoderException e) { LOG.warn("Couldn't resolve address '%s' for node id: %s, label: %s'", addressString, eachNode.getId(), eachNode.getLabel(), e); } } nodesWithLongLat.forEach(eachNode -> { OnmsGeolocation geoLocation = geoLocation(eachNode); nodeIdToCoordinateMapping.put(eachNode.getId(), new Coordinates(geoLocation.getLongitude(), geoLocation.getLatitude())); }); return new Result(nodeIdToCoordinateMapping, nodesWithGeoLocation.stream().collect(Collectors.toMap(node -> node.getId(), node -> geoLocation(node)))); }

Target:
Result resolve(Collection<Integer> nodeIds) { final Criteria idCriteria = new CriteriaBuilder(OnmsNode.class).in("id", nodeIds).toCriteria(); final List<OnmsNode> nodes = nodeDao.findMatching(idCriteria); final List<OnmsNode> nodesWithGeoLocation = nodes.stream() .filter(node -> geoLocation(node) != null) .collect(Collectors.toList()); final List<OnmsNode> nodesWithLongLat = nodesWithGeoLocation.stream() .filter(node -> { OnmsGeolocation geoLocation = geoLocation(node); return geoLocation != null && geoLocation.getLatitude() != null && geoLocation.getLongitude() != null; }) .collect(Collectors.toList()); final Map<Integer, Coordinates> nodeIdToCoordinateMapping = new HashMap<>(); nodesWithLongLat.forEach(eachNode -> { OnmsGeolocation geoLocation = geoLocation(eachNode); nodeIdToCoordinateMapping.put(eachNode.getId(), new Coordinates(geoLocation.getLongitude(), geoLocation.getLatitude())); }); if (resolveCoordinatesFromAddressString) { nodesWithGeoLocation.stream() .filter(node -> !Strings.isNullOrEmpty(geoLocation(node).asAddressString())) .forEach(eachNode -> { final String addressString = geoLocation(eachNode).asAddressString(); try { org.opennms.features.geocoder.Coordinates coordinates = geocoderService.getCoordinates(addressString); if (coordinates != null) { nodeIdToCoordinateMapping.put(eachNode.getId(), new Coordinates(coordinates.getLongitude(), coordinates.getLatitude())); } } catch (GeocoderException e) { LOG.warn("Couldn't resolve address '%s' for node id: %s, label: %s'", addressString, eachNode.getId(), eachNode.getLabel(), e); } }); } return new Result(nodeIdToCoordinateMapping, nodesWithGeoLocation.stream().collect(Collectors.toMap(node -> node.getId(), node -> geoLocation(node)))); }