Ticket #19885: 19885-splitway.patch

File 19885-splitway.patch, 5.9 KB (added by GerdP, 4 years ago)

control flow is really a mess, both in SplitWayAction and in SplitWayCommand

  • src/org/openstreetmap/josm/actions/SplitWayAction.java

     
    222222            } else {
    223223                setHighlightedWaySegments(Collections.emptyList());
    224224                DISPLAY_COUNT.decrementAndGet();
     225                if (getValue() != 1) {
     226                    newWays.forEach(w -> w.setNodes(null)); // see 19885
     227                }
    225228            }
    226229        }
    227230
     
    303306                way.getDataSet().setSelected(newSel);
    304307            }
    305308        });
     309        if (!splitWayCommand.isPresent()) {
     310            newWays.forEach(w -> w.setNodes(null)); // see 19885
     311        }
    306312    }
    307313
    308314    @Override
  • src/org/openstreetmap/josm/command/SplitWayCommand.java

     
    1616import java.util.HashMap;
    1717import java.util.HashSet;
    1818import java.util.Iterator;
     19import java.util.LinkedHashSet;
    1920import java.util.LinkedList;
    2021import java.util.List;
    2122import java.util.Map;
     
    8990     * @param commandList The sequence of commands that should be executed.
    9091     * @param newSelection The new list of selected primitives ids (which is saved for later retrieval with {@link #getNewSelection})
    9192     * @param originalWay The original way being split (which is saved for later retrieval with {@link #getOriginalWay})
    92      * @param newWays The resulting new ways (which is saved for later retrieval with {@link #getOriginalWay})
     93     * @param newWays The resulting new ways (which is saved for later retrieval with {@link #getNewWays})
    9394     */
    9495    public SplitWayCommand(String name, Collection<Command> commandList,
    9596            List<? extends PrimitiveId> newSelection, Way originalWay, List<Way> newWays) {
     
    395396            }
    396397        }
    397398
    398         switch (missingMemberStrategy) {
     399        try {
     400            switch (missingMemberStrategy) {
    399401            case GO_AHEAD_WITH_DOWNLOADS:
    400402                try {
    401403                    downloadMissingMembers(incompleteMembers);
     
    405407                }
    406408                // If missing relation members were downloaded, perform the analysis again to find the relation
    407409                // member order for all relations.
     410                analysis.cleanup();
    408411                analysis = analyseSplit(way, wayToKeep, newWays);
    409                 return Optional.of(splitBasedOnAnalyses(way, newWays, newSelection, analysis, indexOfWayToKeep));
     412                break;
    410413            case GO_AHEAD_WITHOUT_DOWNLOADS:
    411414                // Proceed with the split with the information we have.
    412415                // This can mean that there are no missing members we want, or that the user chooses to continue
    413416                // the split without downloading them.
    414                 return Optional.of(splitBasedOnAnalyses(way, newWays, newSelection, analysis, indexOfWayToKeep));
     417                break;
    415418            case USER_ABORTED:
    416419            default:
    417420                return Optional.empty();
     421            }
     422            return Optional.of(splitBasedOnAnalyses(way, newWays, newSelection, analysis, indexOfWayToKeep));
     423        } finally {
     424            // see #19885
     425            wayToKeep.setNodes(null);
     426            analysis.cleanup();
    418427        }
    419428    }
    420429
     
    441450            if (!r.isUsable()) {
    442451                continue;
    443452            }
    444 
    445453            numberOfRelations++;
     454            boolean isSimpleCase = true;
    446455
    447456            Relation c = null;
    448457            String type = Optional.ofNullable(r.get("type")).orElse("");
     
    548557                            missingWays = Collections.emptySet();
    549558                        }
    550559                        relationAnalyses.add(new RelationAnalysis(c, rm, direction, missingWays));
     560                        isSimpleCase = false;
    551561                    }
    552562                }
    553563            }
    554 
    555             if (c != null) {
    556                 commandList.add(new ChangeCommand(r.getDataSet(), r, c));
     564            if (c != null && isSimpleCase && !r.getMembers().equals(c.getMembers())) {
     565                commandList.add(new ChangeMembersCommand(r, new ArrayList<>(c.getMembers())));
     566                c.setMembers(null); // see #19885
    557567            }
    558568        }
    559569        changedWay.setNodes(null); // see #19885
     
    576586            this.numberOfRelations = numberOfRelations;
    577587        }
    578588
     589        /**
     590         * Unlink temporary copies of relations. See #19885
     591         */
     592        void cleanup() {
     593            for (RelationAnalysis ra : relationAnalyses) {
     594                if (ra.relation.getDataSet() == null)
     595                    ra.relation.setMembers(null);
     596            }
     597        }
     598
    579599        List<RelationAnalysis> getRelationAnalyses() {
    580600            return relationAnalyses;
    581601        }
     
    687707            newSelection.addAll(newWays);
    688708        }
    689709
     710        Set<Relation> modifiedRelations = new LinkedHashSet<>();
    690711        // Perform the split.
    691712        for (RelationAnalysis relationAnalysis : analysis.getRelationAnalyses()) {
    692713            RelationMember rm = relationAnalysis.getRelationMember();
     
    728749                    relation.addMember(j, em);
    729750                }
    730751            }
     752            modifiedRelations.add(relation);
    731753        }
     754        for (Relation r : modifiedRelations) {
     755            DataSet ds = way.getDataSet();
     756            Relation orig = (Relation) ds.getPrimitiveById(r);
     757            analysis.getCommands().add(new ChangeMembersCommand(orig, new ArrayList<>(r.getMembers())));
     758            r.setMembers(null); // see #19885
     759        }
    732760
    733761        EnumSet<WarningType> warnings = analysis.getWarningTypes();
    734762