| 427 | | List<? extends OsmPrimitive> candidates = null; |
| 428 | | for (Node node: sourceDataSet.getNodes()) { |
| 429 | | // lazy initialisation to improve performance, see #19898 |
| 430 | | if (candidates == null) { |
| 431 | | candidates = new ArrayList<>(targetDataSet.getNodes()); |
| | 429 | for (Class<? extends OsmPrimitive> clazz : Arrays.asList(Node.class, Way.class, Relation.class)) { |
| | 430 | MergeNewHelper mergeNewHelper = new MergeNewHelper(targetDataSet); |
| | 431 | for (OsmPrimitive source: sourceDataSet.getPrimitives(clazz::isInstance)) { |
| | 432 | mergePrimitive(source, mergeNewHelper); |
| | 433 | if (progressMonitor != null) { |
| | 434 | progressMonitor.worked(1); |
| | 435 | } |
| 438 | | candidates = null; |
| 439 | | for (Way way: sourceDataSet.getWays()) { |
| 440 | | // lazy initialisation to improve performance |
| 441 | | if (candidates == null) { |
| 442 | | candidates = new ArrayList<>(targetDataSet.getWays()); |
| 443 | | } |
| 444 | | mergePrimitive(way, candidates); |
| 445 | | if (progressMonitor != null) { |
| 446 | | progressMonitor.worked(1); |
| 447 | | } |
| 448 | | } |
| 449 | | candidates = null; |
| 450 | | for (Relation relation: sourceDataSet.getRelations()) { |
| 451 | | // lazy initialisation to improve performance |
| 452 | | if (candidates == null) { |
| 453 | | candidates = new ArrayList<>(targetDataSet.getRelations()); |
| 454 | | } |
| 455 | | mergePrimitive(relation, candidates); |
| 456 | | if (progressMonitor != null) { |
| 457 | | progressMonitor.worked(1); |
| 458 | | } |
| 459 | | } |
| 460 | | candidates = null; |
| | 492 | |
| | 493 | /** |
| | 494 | * Helper class to avoid unnecessary calculation of the candidates list for new primitives. |
| | 495 | * |
| | 496 | * @author Gerd Petermann |
| | 497 | * |
| | 498 | */ |
| | 499 | private static class MergeNewHelper { |
| | 500 | private final DataSet target; |
| | 501 | private List<OsmPrimitive> candidates; |
| | 502 | |
| | 503 | MergeNewHelper(DataSet target) { |
| | 504 | this.target = target; |
| | 505 | } |
| | 506 | |
| | 507 | /** |
| | 508 | * Get possible merge candidates for a new primitive. |
| | 509 | * @param primitive the new primitive |
| | 510 | * @return a list of new and undeleted objects of the same type already existing in the target dataset. Might be empty but it not null. |
| | 511 | */ |
| | 512 | public List<OsmPrimitive> getCandidates(OsmPrimitive primitive) { |
| | 513 | if (candidates == null) { |
| | 514 | candidates = target.getPrimitives(primitive.getClass()::isInstance).stream() |
| | 515 | .filter(p -> p.isNew() && !p.isDeleted()).collect(Collectors.toList()); |
| | 516 | } |
| | 517 | return candidates; |
| | 518 | } |
| | 519 | |
| | 520 | } |