Ignore:
Timestamp:
2024-03-03T10:25:25+01:00 (9 months ago)
Author:
GerdP
Message:

fix #23521: fix some memory leaks

  • dispose dialogs
  • either avoid to create clones of ways or relations or use setNodes(null) / setMembers(null)
  • replaces most ChangeCommand instances by better specialized alternatives
  • add some comments
  • fix some checkstyle / sonar issues
File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/reltoolbox/src/relcontext/relationfix/AssociatedStreetFixer.java

    r36103 r36217  
    55
    66import java.util.ArrayList;
     7import java.util.Collections;
    78import java.util.HashMap;
    89import java.util.List;
    910import java.util.Map;
    1011
    11 import org.openstreetmap.josm.command.ChangeCommand;
     12import org.openstreetmap.josm.command.ChangeMembersCommand;
     13import org.openstreetmap.josm.command.ChangePropertyCommand;
    1214import org.openstreetmap.josm.command.Command;
    1315import org.openstreetmap.josm.command.SequenceCommand;
     
    2325public class AssociatedStreetFixer extends RelationFixer {
    2426
     27    private static final String ADDR_HOUSENUMBER = "addr:housenumber";
     28    private static final String BUILDING = "building";
     29    private static final String HOUSE = "house";
     30    private static final String STREET = "street";
     31
    2532    public AssociatedStreetFixer() {
    2633        super("associatedStreet");
     
    3037    public boolean isRelationGood(Relation rel) {
    3138        for (RelationMember m : rel.getMembers()) {
    32             if (m.getType().equals(OsmPrimitiveType.NODE) && !"house".equals(m.getRole())) {
     39            if (m.getType() == OsmPrimitiveType.NODE && !HOUSE.equals(m.getRole())) {
    3340                setWarningMessage(tr("Node without ''house'' role found"));
    3441                return false;
    3542            }
    36             if (m.getType().equals(OsmPrimitiveType.WAY) && !("house".equals(m.getRole()) || "street".equals(m.getRole()))) {
     43            if (m.getType() == OsmPrimitiveType.WAY && !(HOUSE.equals(m.getRole()) || STREET.equals(m.getRole()))) {
    3744                setWarningMessage(tr("Way without ''house'' or ''street'' role found"));
    3845                return false;
    3946            }
    40             if (m.getType().equals(OsmPrimitiveType.RELATION) && !"house".equals(m.getRole())) {
     47            if (m.getType() == OsmPrimitiveType.RELATION && !HOUSE.equals(m.getRole())) {
    4148                setWarningMessage(tr("Relation without ''house'' role found"));
    4249                return false;
     
    5461        }
    5562        for (RelationMember m : rel.getMembers()) {
    56             if ("street".equals(m.getRole()) && !streetName.equals(m.getWay().get("name"))) {
     63            if (STREET.equals(m.getRole()) && !streetName.equals(m.getWay().get("name"))) {
    5764                String anotherName = m.getWay().get("name");
    5865                if (anotherName != null && !anotherName.isEmpty()) {
     
    7279        // name - check which name is most used in street members and add to relation
    7380        // copy this name to the other street members (???)
    74         Relation rel = new Relation(source);
     81        List<RelationMember> members = source.getMembers();
    7582        boolean fixed = false;
    7683
    77         for (int i = 0; i < rel.getMembersCount(); i++) {
    78             RelationMember m = rel.getMember(i);
    79 
     84        for (int i = 0; i < members.size(); i++) {
     85            RelationMember m = members.get(i);
    8086            if (m.isNode()) {
    8187                Node node = m.getNode();
    82                 if (!"house".equals(m.getRole()) &&
    83                         (node.hasKey("building") || node.hasKey("addr:housenumber"))) {
     88                if (!HOUSE.equals(m.getRole()) &&
     89                        (node.hasKey(BUILDING) || node.hasKey(ADDR_HOUSENUMBER))) {
    8490                    fixed = true;
    85                     rel.setMember(i, new RelationMember("house", node));
     91                    members.set(i, new RelationMember(HOUSE, node));
    8692                }
    8793            } else if (m.isWay()) {
    8894                Way way = m.getWay();
    89                 if (!"street".equals(m.getRole()) && way.hasKey("highway")) {
     95                if (!STREET.equals(m.getRole()) && way.hasKey("highway")) {
    9096                    fixed = true;
    91                     rel.setMember(i, new RelationMember("street", way));
    92                 } else if (!"house".equals(m.getRole()) &&
    93                         (way.hasKey("building") || way.hasKey("addr:housenumber"))) {
     97                    members.set(i, new RelationMember(STREET, way));
     98                } else if (!HOUSE.equals(m.getRole()) &&
     99                        (way.hasKey(BUILDING) || way.hasKey(ADDR_HOUSENUMBER))) {
    94100                    fixed = true;
    95                     rel.setMember(i, new RelationMember("house", way));
     101                    members.set(i, new RelationMember(HOUSE, way));
    96102                }
    97103            } else if (m.isRelation()) {
    98104                Relation relation = m.getRelation();
    99                 if (!"house".equals(m.getRole()) &&
    100                         (relation.hasKey("building") || relation.hasKey("addr:housenumber") || "multipolygon".equals(relation.get("type")))) {
     105                if (!HOUSE.equals(m.getRole()) &&
     106                        (relation.hasKey(BUILDING) || relation.hasKey(ADDR_HOUSENUMBER) || "multipolygon".equals(relation.get("type")))) {
    101107                    fixed = true;
    102                     rel.setMember(i, new RelationMember("house", relation));
     108                    members.set(i, new RelationMember(HOUSE, relation));
    103109                }
    104110            }
     
    107113        // fill relation name
    108114        Map<String, Integer> streetNames = new HashMap<>();
    109         for (RelationMember m : rel.getMembers()) {
    110             if ("street".equals(m.getRole()) && m.isWay()) {
     115        for (RelationMember m : members) {
     116            if (STREET.equals(m.getRole()) && m.isWay()) {
    111117                String name = m.getWay().get("name");
    112118                if (name == null || name.isEmpty()) {
    113119                    continue;
    114120                }
    115 
    116121                Integer count = streetNames.get(name);
    117 
    118122                streetNames.put(name, count != null ? count + 1 : 1);
    119123            }
     
    128132        }
    129133
    130         if (!rel.hasKey("name") && !commonName.isEmpty()) {
    131             fixed = true;
    132             rel.put("name", commonName);
    133         } else {
    134             commonName = ""; // set empty common name - if we already have name on relation, do not overwrite it
     134        Map<String, String> nameTag = new HashMap<>();
     135        if (!source.hasKey("name") && !commonName.isEmpty()) {
     136            nameTag.put("name", commonName);
    135137        }
    136138
    137139        List<Command> commandList = new ArrayList<>();
     140        final DataSet ds = Utils.firstNonNull(source.getDataSet(), MainApplication.getLayerManager().getEditDataSet());
    138141        if (fixed) {
    139             final DataSet ds = Utils.firstNonNull(source.getDataSet(), MainApplication.getLayerManager().getEditDataSet());
    140             commandList.add(new ChangeCommand(ds, source, rel));
     142            commandList.add(new ChangeMembersCommand(ds, source, members));
     143        }
     144        if (!nameTag.isEmpty()) {
     145            commandList.add(new ChangePropertyCommand(ds, Collections.singleton(source), nameTag));
    141146        }
    142147
    143         /*if (!commonName.isEmpty())
    144         // fill common name to streets
    145         for (RelationMember m : rel.getMembers())
    146             if ("street".equals(m.getRole()) && m.isWay()) {
    147                 String name = m.getWay().get("name");
    148                 if (commonName.equals(name)) continue;
    149 
    150                 // TODO: ask user if he really wants to overwrite street name??
    151 
    152                 Way oldWay = m.getWay();
    153                 Way newWay = new Way(oldWay);
    154                 newWay.put("name", commonName);
    155 
    156                 commandList.add(new ChangeCommand(MainApplication.getLayerManager().getEditDataSet(), oldWay, newWay));
    157             }
    158          */
    159148        // return results
    160         if (commandList.isEmpty()) {
     149        if (commandList.isEmpty())
    161150            return null;
    162         }
    163151        return SequenceCommand.wrapIfNeeded(tr("fix associatedStreet relation"), commandList);
    164152    }
Note: See TracChangeset for help on using the changeset viewer.