Changeset 2399 in josm for trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
- Timestamp:
- 2009-11-07T14:41:34+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/DataSet.java
r2396 r2399 1 1 // License: GPL. Copyright 2007 by Immanuel Scholz and others 2 2 package org.openstreetmap.josm.data.osm; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.awt.geom.Area; … … 14 16 import java.util.LinkedList; 15 17 import java.util.List; 18 import java.util.Map; 16 19 import java.util.Set; 17 20 … … 30 33 public class DataSet implements Cloneable { 31 34 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 32 47 /** 33 48 * A list of listeners to selection changed events. The list is static, as listeners register … … 48 63 } 49 64 } 65 66 private Storage<OsmPrimitive> allPrimitives = new Storage<OsmPrimitive>(new IdHash()); 67 private Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives.foreignKey(new IdHash()); 50 68 51 69 /** … … 179 197 */ 180 198 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 181 203 if (primitive instanceof Node) { 182 204 nodes.add((Node) primitive); … … 186 208 relations.add((Relation) primitive); 187 209 } 210 allPrimitives.add(primitive); 188 211 } 189 212 190 213 public OsmPrimitive addPrimitive(PrimitiveData data) { 214 OsmPrimitive result; 191 215 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); 195 217 } 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); 199 219 } 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); 203 221 } else 204 222 throw new AssertionError(); 223 addPrimitive(result); 224 return result; 205 225 } 206 226 … … 214 234 * @param primitive the primitive. Ignored if null. 215 235 */ 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(); 218 241 return; 242 } 219 243 if (primitive instanceof Node) { 220 244 nodes.remove(primitive); … … 225 249 } 226 250 selectedPrimitives.remove(primitive); 227 } 228 229 public void removePrimitive(long id, OsmPrimitiveType type) { 230 removePrimitive(getPrimitiveById(id, type)); 251 allPrimitives.remove(primitive); 231 252 } 232 253 … … 542 563 */ 543 564 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; 564 580 } 565 581 addPrimitive(result); 566 return result;567 } else 568 return null;582 } 583 584 return result; 569 585 } 570 586
Note:
See TracChangeset
for help on using the changeset viewer.