Changeset 21177 in osm for applications/editors/josm
- Timestamp:
- 2010-05-08T15:24:20+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/DuplicateNode.java
r21128 r21177 8 8 import java.util.ArrayList; 9 9 import java.util.Collection; 10 import java.util.HashMap;11 10 import java.util.Iterator; 12 11 import java.util.LinkedHashSet; … … 14 13 import java.util.List; 15 14 import java.util.Map; 16 import java.util.Map.Entry;17 15 18 16 import javax.swing.JLabel; … … 24 22 import org.openstreetmap.josm.command.Command; 25 23 import org.openstreetmap.josm.data.coor.LatLon; 24 import org.openstreetmap.josm.data.osm.Hash; 26 25 import org.openstreetmap.josm.data.osm.Node; 27 26 import org.openstreetmap.josm.data.osm.OsmPrimitive; 27 import org.openstreetmap.josm.data.osm.Storage; 28 28 import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; 29 29 import org.openstreetmap.josm.gui.progress.ProgressMonitor; … … 39 39 public class DuplicateNode extends Test{ 40 40 41 private class NodeHash implements Hash<Object, Object> { 42 43 @SuppressWarnings("unchecked") 44 private LatLon getLatLon(Object o) { 45 if (o instanceof Node) { 46 return ((Node) o).getCoor().getRoundedToOsmPrecision(); 47 } else if (o instanceof List<?>) { 48 return ((List<Node>) o).get(0).getCoor().getRoundedToOsmPrecision(); 49 } else { 50 throw new AssertionError(); 51 } 52 } 53 54 public boolean equals(Object k, Object t) { 55 return getLatLon(k).equals(getLatLon(t)); 56 } 57 58 public int getHashCode(Object k) { 59 return getLatLon(k).hashCode(); 60 } 61 62 } 63 41 64 protected static int DUPLICATE_NODE = 1; 42 65 … … 47 70 * <pos, NodesByEqualTagsMap> 48 71 */ 49 Map<LatLon,Object> potentialDuplicates;72 Storage<Object> potentialDuplicates; 50 73 51 74 /** … … 55 78 { 56 79 super(tr("Duplicated nodes")+".", 57 tr("This test checks that there are no nodes at the very same location."));80 tr("This test checks that there are no nodes at the very same location.")); 58 81 } 59 82 60 83 @Override 61 84 public void startTest(ProgressMonitor monitor) { 62 super.startTest(monitor); 63 potentialDuplicates = new HashMap<LatLon, Object>(); 64 } 65 66 67 @Override 68 public void endTest() { 69 for (Entry<LatLon, Object> entry: potentialDuplicates.entrySet()) { 70 Object v = entry.getValue(); 71 if (v instanceof Node) { 72 // just one node at this position. Nothing to report as 73 // error 74 continue; 75 } 76 77 // multiple nodes at the same position -> report errors 78 // 79 NodesByEqualTagsMap map = (NodesByEqualTagsMap)v; 80 errors.addAll(map.buildTestErrors(this)); 81 } 82 super.endTest(); 83 potentialDuplicates = null; 84 } 85 86 @Override 87 public void visit(Node n) { 88 if (n.isUsable()) { 89 LatLon rounded = n.getCoor().getRoundedToOsmPrecision(); 90 if (potentialDuplicates.get(rounded) == null) { 91 // in most cases there is just one node at a given position. We 92 // avoid to create an extra object and add remember the node 93 // itself at this position 94 potentialDuplicates.put(rounded, n); 95 } else if (potentialDuplicates.get(rounded) instanceof Node) { 96 // we have an additional node at the same position. Create an extra 97 // object to keep track of the nodes at this position. 98 // 99 Node n1 = (Node)potentialDuplicates.get(rounded); 100 NodesByEqualTagsMap map = new NodesByEqualTagsMap(); 101 map.add(n1); 102 map.add(n); 103 potentialDuplicates.put(rounded, map); 104 } else if (potentialDuplicates.get(rounded) instanceof NodesByEqualTagsMap) { 105 // we have multiple nodes at the same position. 106 // 107 NodesByEqualTagsMap map = (NodesByEqualTagsMap)potentialDuplicates.get(rounded); 108 map.add(n); 109 } 110 } 111 } 85 super.startTest(monitor); 86 potentialDuplicates = new Storage<Object>(new NodeHash()); 87 } 88 89 90 @SuppressWarnings("unchecked") 91 @Override 92 public void endTest() { 93 for (Object v: potentialDuplicates) { 94 if (v instanceof Node) { 95 // just one node at this position. Nothing to report as 96 // error 97 continue; 98 } 99 100 // multiple nodes at the same position -> report errors 101 // 102 List<Node> nodes = (List<Node>)v; 103 errors.addAll(buildTestErrors(this, nodes)); 104 } 105 super.endTest(); 106 potentialDuplicates = null; 107 } 108 109 public List<TestError> buildTestErrors(Test parentTest, List<Node> nodes) { 110 List<TestError> errors = new ArrayList<TestError>(); 111 112 Bag<Map<String,String>, OsmPrimitive> bag = new Bag<Map<String,String>, OsmPrimitive>(); 113 for (Node n: nodes) { 114 bag.add(n.getKeys(), n); 115 } 116 // check whether we have multiple nodes at the same position with 117 // the same tag set 118 // 119 for (Iterator<Map<String,String>> it = bag.keySet().iterator(); it.hasNext(); ) { 120 Map<String,String> tagSet = it.next(); 121 if (bag.get(tagSet).size() > 1) { 122 errors.add(new TestError( 123 parentTest, 124 Severity.ERROR, 125 tr("Duplicated nodes"), 126 DUPLICATE_NODE, 127 bag.get(tagSet) 128 )); 129 it.remove(); 130 } 131 132 } 133 134 // check whether we have multiple nodes at the same position with 135 // differing tag sets 136 // 137 if (!bag.isEmpty()) { 138 List<OsmPrimitive> duplicates = new ArrayList<OsmPrimitive>(); 139 for (List<OsmPrimitive> l: bag.values()) { 140 duplicates.addAll(l); 141 } 142 if (duplicates.size() > 1) { 143 errors.add(new TestError( 144 parentTest, 145 Severity.WARNING, 146 tr("Nodes at same position"), 147 DUPLICATE_NODE, 148 duplicates 149 )); 150 } 151 } 152 return errors; 153 } 154 155 @SuppressWarnings("unchecked") 156 @Override 157 public void visit(Node n) { 158 if (n.isUsable()) { 159 if (potentialDuplicates.get(n) == null) { 160 // in most cases there is just one node at a given position. We 161 // avoid to create an extra object and add remember the node 162 // itself at this position 163 potentialDuplicates.put(n); 164 } else if (potentialDuplicates.get(n) instanceof Node) { 165 // we have an additional node at the same position. Create an extra 166 // object to keep track of the nodes at this position. 167 // 168 Node n1 = (Node)potentialDuplicates.get(n); 169 List<Node> nodes = new ArrayList<Node>(2); 170 nodes.add(n1); 171 nodes.add(n); 172 potentialDuplicates.put(nodes); 173 } else if (potentialDuplicates.get(n) instanceof List<?>) { 174 // we have multiple nodes at the same position. 175 // 176 List<Node> nodes = (List<Node>)potentialDuplicates.get(n); 177 nodes.add(n); 178 } 179 } 180 } 112 181 113 182 /** … … 139 208 } 140 209 141 142 143 144 210 @Override 211 public boolean isFixable(TestError testError) { 212 return (testError.getTester() instanceof DuplicateNode); 213 } 145 214 146 215 /** … … 166 235 167 236 return ConditionalOptionPaneUtil.showConfirmationDialog( 168 "delete_outside_nodes",169 Main.parent,170 msg,171 tr("Delete confirmation"),172 JOptionPane.YES_NO_OPTION,173 JOptionPane.QUESTION_MESSAGE,174 JOptionPane.YES_OPTION);237 "delete_outside_nodes", 238 Main.parent, 239 msg, 240 tr("Delete confirmation"), 241 JOptionPane.YES_NO_OPTION, 242 JOptionPane.QUESTION_MESSAGE, 243 JOptionPane.YES_OPTION); 175 244 } 176 245 } … … 179 248 return true; 180 249 } 181 182 static private class NodesByEqualTagsMap {183 /**184 * a bag of primitives with the same position. The bag key is represented185 * by the tag set of the primitive. This allows for easily find nodes at186 * the same position with the same tag sets later.187 */188 private Bag<Map<String,String>, OsmPrimitive> bag;189 190 public NodesByEqualTagsMap() {191 bag = new Bag<Map<String,String>, OsmPrimitive>();192 }193 194 public void add(Node n) {195 bag.add(n.getKeys(), n);196 }197 198 public List<TestError> buildTestErrors(Test parentTest) {199 List<TestError> errors = new ArrayList<TestError>();200 // check whether we have multiple nodes at the same position with201 // the same tag set202 //203 for (Iterator<Map<String,String>> it = bag.keySet().iterator(); it.hasNext(); ) {204 Map<String,String> tagSet = it.next();205 if (bag.get(tagSet).size() > 1) {206 errors.add(new TestError(207 parentTest,208 Severity.ERROR,209 tr("Duplicated nodes"),210 DUPLICATE_NODE,211 bag.get(tagSet)212 ));213 it.remove();214 }215 216 }217 218 // check whether we have multiple nodes at the same position with219 // differing tag sets220 //221 if (!bag.isEmpty()) {222 List<OsmPrimitive> duplicates = new ArrayList<OsmPrimitive>();223 for (List<OsmPrimitive> l: bag.values()) {224 duplicates.addAll(l);225 }226 if (duplicates.size() > 1) {227 errors.add(new TestError(228 parentTest,229 Severity.WARNING,230 tr("Nodes at same position"),231 DUPLICATE_NODE,232 duplicates233 ));234 }235 }236 return errors;237 }238 }239 250 }
Note:
See TracChangeset
for help on using the changeset viewer.