Changeset 2399 in josm


Ignore:
Timestamp:
2009-11-07T14:41:34+01:00 (12 years ago)
Author:
jttt
Message:

Added map of primitives to dataset to make search by id faster
check if primitive already exist in addPrimitive and removePrimitive
use PrimitiveId instead of id + primitive type

Location:
trunk
Files:
5 added
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/UploadSelectionAction.java

    r2381 r2399  
    22package org.openstreetmap.josm.actions;
    33
     4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    56
     
    2122import org.openstreetmap.josm.data.osm.Node;
    2223import org.openstreetmap.josm.data.osm.OsmPrimitive;
    23 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    2424import org.openstreetmap.josm.data.osm.Relation;
    2525import org.openstreetmap.josm.data.osm.Way;
     
    3333import org.openstreetmap.josm.tools.ExceptionUtil;
    3434import org.xml.sax.SAXException;
    35 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    3635
    3736/**
     
    326325                    for (OsmPrimitive p: ds.allPrimitives()) {
    327326                        if (canceled) return;
    328                         OsmPrimitive myDeletedParent = layer.data.getPrimitiveById(p.getId(), OsmPrimitiveType.from(p));
     327                        OsmPrimitive myDeletedParent = layer.data.getPrimitiveById(p);
    329328                        // our local dataset includes a deleted parent of a primitive we want
    330329                        // to delete. Include this parent in the collection of uploaded primitives
  • trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java

    r2349 r2399  
    1313
    1414import org.openstreetmap.josm.data.osm.OsmPrimitive;
    15 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1615import org.openstreetmap.josm.data.osm.PrimitiveData;
    1716
     
    2928
    3029        for (PrimitiveData pd:data) {
    31             createdPrimitives.add(getLayer().data.getPrimitiveById(pd.getId(), OsmPrimitiveType.fromData(pd), true));
     30            createdPrimitives.add(getLayer().data.getPrimitiveById(pd, true));
    3231        }
    3332
     
    4140    @Override public void undoCommand() {
    4241        for (PrimitiveData p:data) {
    43             getLayer().data.removePrimitive(p.getId(), OsmPrimitiveType.fromData(p));
     42            getLayer().data.removePrimitive(p);
    4443        }
    4544    }
     
    4746    @Override
    4847    public MutableTreeNode description() {
    49          return new DefaultMutableTreeNode(
     48        return new DefaultMutableTreeNode(
    5049                new JLabel(tr("Added {0} objects", data.size()), null,
    5150                        JLabel.HORIZONTAL
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r2396 r2399  
    11// License: GPL. Copyright 2007 by Immanuel Scholz and others
    22package org.openstreetmap.josm.data.osm;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.awt.geom.Area;
     
    1416import java.util.LinkedList;
    1517import java.util.List;
     18import java.util.Map;
    1619import java.util.Set;
    1720
     
    3033public class DataSet implements Cloneable {
    3134
     35    private static class IdHash implements Hash<PrimitiveId,OsmPrimitive> {
     36
     37        public int getHashCode(PrimitiveId k) {
     38            return (int)k.getUniqueId() ^ k.getType().hashCode();
     39        }
     40
     41        public boolean equals(PrimitiveId key, OsmPrimitive value) {
     42            if (key == null || value == null) return false;
     43            return key.getUniqueId() == value.getUniqueId() && key.getType() == value.getType();
     44        }
     45    }
     46
    3247    /**
    3348     * A list of listeners to selection changed events. The list is static, as listeners register
     
    4863        }
    4964    }
     65
     66    private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash());
     67    private Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new IdHash());
    5068
    5169    /**
     
    179197     */
    180198    public void addPrimitive(OsmPrimitive primitive) {
     199        if (getPrimitiveById(primitive) != null)
     200            throw new DataIntegrityProblemException(
     201                    tr("Unable to add primitive {0} to the dataset because it's already included", primitive.toString()));
     202
    181203        if (primitive instanceof Node) {
    182204            nodes.add((Node) primitive);
     
    186208            relations.add((Relation) primitive);
    187209        }
     210        allPrimitives.add(primitive);
    188211    }
    189212
    190213    public OsmPrimitive addPrimitive(PrimitiveData data) {
     214        OsmPrimitive result;
    191215        if (data instanceof NodeData) {
    192             Node node = new Node((NodeData)data, this);
    193             nodes.add(node);
    194             return node;
     216            result = new Node((NodeData)data, this);
    195217        } else if (data instanceof WayData) {
    196             Way way = new Way((WayData)data, this);
    197             ways.add(way);
    198             return way;
     218            result = new Way((WayData)data, this);
    199219        } else if (data instanceof RelationData) {
    200             Relation relation = new Relation((RelationData)data, this);
    201             relations.add(relation);
    202             return relation;
     220            result = new Relation((RelationData)data, this);
    203221        } else
    204222            throw new AssertionError();
     223        addPrimitive(result);
     224        return result;
    205225    }
    206226
     
    214234     * @param primitive the primitive. Ignored if null.
    215235     */
    216     public void removePrimitive(OsmPrimitive primitive) {
    217         if (primitive == null)
     236    public void removePrimitive(PrimitiveId primitiveId) {
     237        OsmPrimitive primitive = getPrimitiveById(primitiveId);
     238        if (primitive == null) {
     239            System.out.println("Warning: somebody is trying to remove nonexisting primitive from the Dataset. Action will be ignored. You can report this problem on http://josm.openstreetmap.de");
     240            new Exception().printStackTrace();
    218241            return;
     242        }
    219243        if (primitive instanceof Node) {
    220244            nodes.remove(primitive);
     
    225249        }
    226250        selectedPrimitives.remove(primitive);
    227     }
    228 
    229     public void removePrimitive(long id, OsmPrimitiveType type) {
    230         removePrimitive(getPrimitiveById(id, type));
     251        allPrimitives.remove(primitive);
    231252    }
    232253
     
    542563     */
    543564    public OsmPrimitive getPrimitiveById(long id, OsmPrimitiveType type) {
    544         return getPrimitiveById(id, type, false);
    545     }
    546 
    547     public OsmPrimitive getPrimitiveById(long id, OsmPrimitiveType type, boolean createNew) {
    548         Collection<? extends OsmPrimitive> primitives = null;
    549         switch(type) {
    550         case NODE: primitives = nodes; break;
    551         case WAY: primitives = ways; break;
    552         case RELATION: primitives = relations; break;
    553         }
    554         for (OsmPrimitive primitive : primitives) {
    555             if (primitive.getUniqueId() == id) return primitive;
    556         }
    557 
    558         if (createNew) {
    559             OsmPrimitive result = null;
    560             switch (type) {
    561             case NODE: result = new Node(id, true); break;
    562             case WAY: result = new Way(id, true); break;
    563             case RELATION: result = new Relation(id, true); break;
     565        return getPrimitiveById(new SimplePrimitiveId(id, type), false);
     566    }
     567
     568    public OsmPrimitive getPrimitiveById(PrimitiveId primitiveId) {
     569        return getPrimitiveById(primitiveId, false);
     570    }
     571
     572    public OsmPrimitive getPrimitiveById(PrimitiveId primitiveId, boolean createNew) {
     573        OsmPrimitive result = primitivesMap.get(primitiveId);
     574
     575        if (result == null && createNew) {
     576            switch (primitiveId.getType()) {
     577            case NODE: result = new Node(primitiveId.getUniqueId(), true); break;
     578            case WAY: result = new Way(primitiveId.getUniqueId(), true); break;
     579            case RELATION: result = new Relation(primitiveId.getUniqueId(), true); break;
    564580            }
    565581            addPrimitive(result);
    566             return result;
    567         } else
    568             return null;
     582        }
     583
     584        return result;
    569585    }
    570586
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r2363 r2399  
    139139        return formatter.format(this);
    140140    }
     141
     142    public OsmPrimitiveType getType() {
     143        return OsmPrimitiveType.NODE;
     144    }
    141145}
  • trunk/src/org/openstreetmap/josm/data/osm/NodeData.java

    r2305 r2399  
    5959    }
    6060
     61    public OsmPrimitiveType getType() {
     62        return OsmPrimitiveType.NODE;
     63    }
     64
    6165}
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r2363 r2399  
    1010import java.util.Date;
    1111import java.util.HashMap;
    12 import java.util.HashSet;
    1312import java.util.LinkedHashSet;
    1413import java.util.LinkedList;
     
    1615import java.util.Locale;
    1716import java.util.Map;
    18 import java.util.Set;
    1917import java.util.Map.Entry;
    2018import java.util.concurrent.atomic.AtomicLong;
     
    3533 * @author imi
    3634 */
    37 abstract public class OsmPrimitive implements Comparable<OsmPrimitive>, Tagged {
     35abstract public class OsmPrimitive implements Comparable<OsmPrimitive>, Tagged, PrimitiveId {
    3836
    3937    private static final AtomicLong idCounter = new AtomicLong(0);
  • trunk/src/org/openstreetmap/josm/data/osm/PrimitiveData.java

    r2305 r2399  
    1515 *
    1616 */
    17 public abstract class PrimitiveData implements Tagged {
     17public abstract class PrimitiveData implements Tagged, PrimitiveId {
    1818
    1919    // Useful?
     
    169169    }
    170170
     171    /**
     172     * PrimitiveId implementation. Returns the same value as getId()
     173     */
     174    public long getUniqueId() {
     175        return id;
     176    }
     177
    171178
    172179
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r2381 r2399  
    171171        for (RelationMemberData member : relationData.getMembers()) {
    172172            switch (member.getMemberType()) {
    173                 case NODE:
    174                     nodes.put(member.getMemberId(), nodeMarker);
    175                     break;
    176                 case WAY:
    177                     ways.put(member.getMemberId(), wayMarker);
    178                     break;
    179                 case RELATION:
    180                     relations.put(member.getMemberId(), relationMarker);
    181                     break;
     173            case NODE:
     174                nodes.put(member.getMemberId(), nodeMarker);
     175                break;
     176            case WAY:
     177                ways.put(member.getMemberId(), wayMarker);
     178                break;
     179            case RELATION:
     180                relations.put(member.getMemberId(), relationMarker);
     181                break;
    182182            }
    183183        }
     
    203203            OsmPrimitive foundMember = null;
    204204            switch (member.getMemberType()) {
    205                 case NODE:
    206                     foundMember = nodes.get(member.getMemberId());
    207                     if (foundMember == nodeMarker)
    208                         throw new AssertionError("Data consistency problem - relation with missing member detected");
    209                     break;
    210                 case WAY:
    211                     foundMember = ways.get(member.getMemberId());
    212                     if (foundMember == wayMarker)
    213                         throw new AssertionError("Data consistency problem - relation with missing member detected");
    214                     break;
    215                 case RELATION:
    216                     foundMember = relations.get(member.getMemberId());
    217                     if (foundMember == relationMarker)
    218                         throw new AssertionError("Data consistency problem - relation with missing member detected");
    219                     break;
     205            case NODE:
     206                foundMember = nodes.get(member.getMemberId());
     207                if (foundMember == nodeMarker)
     208                    throw new AssertionError("Data consistency problem - relation with missing member detected");
     209                break;
     210            case WAY:
     211                foundMember = ways.get(member.getMemberId());
     212                if (foundMember == wayMarker)
     213                    throw new AssertionError("Data consistency problem - relation with missing member detected");
     214                break;
     215            case RELATION:
     216                foundMember = relations.get(member.getMemberId());
     217                if (foundMember == relationMarker)
     218                    throw new AssertionError("Data consistency problem - relation with missing member detected");
     219                break;
    220220            }
    221221            newMembers.add(new RelationMember(member.getRole(), foundMember));
     
    340340        return ret;
    341341    }
     342
     343    public OsmPrimitiveType getType() {
     344        return OsmPrimitiveType.RELATION;
     345    }
    342346}
  • trunk/src/org/openstreetmap/josm/data/osm/RelationData.java

    r2305 r2399  
    3737    }
    3838
     39    public OsmPrimitiveType getType() {
     40        return OsmPrimitiveType.RELATION;
     41    }
     42
    3943}
  • trunk/src/org/openstreetmap/josm/data/osm/RelationMember.java

    r2285 r2399  
    77 *
    88 */
    9 public class RelationMember {
     9public class RelationMember implements PrimitiveId {
    1010
    1111    /**
     
    157157            return false;
    158158    }
     159
     160    /**
     161     * PrimitiveId implementation. Returns the same value as getMember().getType()
     162     */
     163    public OsmPrimitiveType getType() {
     164        return member.getType();
     165    }
     166
     167    /**
     168     * PrimitiveId implementation. Returns the same value as getMemberType().getUniqueId()
     169     */
     170    public long getUniqueId() {
     171        return member.getUniqueId();
     172    }
    159173}
  • trunk/src/org/openstreetmap/josm/data/osm/RelationMemberData.java

    r2305 r2399  
    22package org.openstreetmap.josm.data.osm;
    33
    4 public class RelationMemberData {
     4public class RelationMemberData implements PrimitiveId {
    55
    66    private final String role;
     
    3333    }
    3434
     35    /**
     36     * PrimitiveId implementation. Returns the same value as {@link #getMemberType()}
     37     */
     38    public OsmPrimitiveType getType() {
     39        return memberType;
     40    }
     41
     42    /**
     43     * PrimitiveId implementation. Returns the same value as {@link #getMemberId()()}
     44     */
     45    public long getUniqueId() {
     46        return memberId;
     47    }
     48
    3549}
  • trunk/src/org/openstreetmap/josm/data/osm/Way.java

    r2381 r2399  
    328328        return formatter.format(this);
    329329    }
     330
     331    public OsmPrimitiveType getType() {
     332        return OsmPrimitiveType.WAY;
     333    }
    330334}
  • trunk/src/org/openstreetmap/josm/data/osm/WayData.java

    r2305 r2399  
    3737    }
    3838
     39    public OsmPrimitiveType getType() {
     40        return OsmPrimitiveType.WAY;
     41    }
     42
    3943}
  • trunk/test/functional/org/openstreetmap/josm/io/MultiFetchServerObjectReaderTest.java

    r2396 r2399  
    247247        assertEquals(10, out.getNodes().size());
    248248        for (Node n1:out.getNodes()) {
    249             Node n2 = (Node)ds.getPrimitiveById(n1.getId(), OsmPrimitiveType.NODE);
     249            Node n2 = (Node)ds.getPrimitiveById(n1);
    250250            assertNotNull(n2);
    251251            assertEquals(n2.get("name"),n2.get("name"));
     
    264264        assertEquals(10, out.getWays().size());
    265265        for (Way w1: out.getWays()) {
    266             Way w2 = (Way)ds.getPrimitiveById(w1.getId(), OsmPrimitiveType.WAY);
     266            Way w2 = (Way)ds.getPrimitiveById(w1);
    267267            assertNotNull(w2);
    268268            assertEquals(w2.getNodesCount(), w1.getNodesCount());
     
    300300        assertEquals(812, out.getNodes().size());
    301301        for (Node n1:out.getNodes()) {
    302             Node n2 = (Node)ds.getPrimitiveById(n1.getId(), OsmPrimitiveType.NODE);
     302            Node n2 = (Node)ds.getPrimitiveById(n1);
    303303            assertNotNull(n2);
    304304            assertEquals(n2.get("name"),n2.get("name"));
     
    319319        assertEquals(10, out.getNodes().size());
    320320        for (Node n1:out.getNodes()) {
    321             Node n2 = (Node)ds.getPrimitiveById(n1.getId(), OsmPrimitiveType.NODE);
     321            Node n2 = (Node)ds.getPrimitiveById(n1);
    322322            assertNotNull(n2);
    323323            assertEquals(n2.get("name"),n2.get("name"));
  • trunk/test/functional/org/openstreetmap/josm/io/OsmServerBackreferenceReaderTest.java

    r2396 r2399  
    322322        Set<Long> expectedNodeIds = new HashSet<Long>();
    323323        for (Way way : referers.getWays()) {
    324             Way orig = (Way) ds.getPrimitiveById(way.getId(), OsmPrimitiveType.WAY);
     324            Way orig = (Way) ds.getPrimitiveById(way);
    325325            for (Node n : orig.getNodes()) {
    326326                expectedNodeIds.add(n.getId());
Note: See TracChangeset for help on using the changeset viewer.