- Timestamp:
- 2009-09-27T18:15:34+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/Way.java
r2181 r2204 2 2 package org.openstreetmap.josm.data.osm; 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 4 6 import java.util.ArrayList; 5 6 7 import java.util.Arrays; 7 8 import java.util.Collection; … … 11 12 import org.openstreetmap.josm.tools.CopyList; 12 13 import org.openstreetmap.josm.tools.Pair; 13 import static org.openstreetmap.josm.tools.I18n.tr;14 14 15 15 /** … … 24 24 * 25 25 */ 26 private final List<Node> nodes = new ArrayList<Node>();26 private Node[] nodes = new Node[0]; 27 27 28 28 /** … … 34 34 */ 35 35 public List<Node> getNodes() { 36 return new CopyList<Node>(nodes.toArray(new Node[nodes.size()])); 37 } 38 39 /** 36 return new CopyList<Node>(nodes); 37 } 38 39 /** 40 * Set new list of nodes to way. This method is preferred to multiple calls to addNode/removeNode 41 * and similar methods because nodes are internally saved as array which means lower memory overhead 42 * but also slower modifying operations. 40 43 * @param nodes New way nodes. Can be null, in that case all way nodes are removed 41 44 * @since 1862 42 45 */ 43 46 public void setNodes(List<Node> nodes) { 44 this.nodes.clear(); 45 if (nodes != null) { 46 this.nodes.addAll(nodes); 47 if (nodes == null) { 48 this.nodes = new Node[0]; 49 } else { 50 this.nodes = nodes.toArray(new Node[nodes.size()]); 47 51 } 48 52 clearCached(); … … 51 55 /** 52 56 * Replies the number of nodes in this ways. 53 * 57 * 54 58 * @return the number of nodes in this ways. 55 59 * @since 1862 56 60 */ 57 61 public int getNodesCount() { 58 return nodes. size();62 return nodes.length; 59 63 } 60 64 61 65 /** 62 66 * Replies the node at position <code>index</code>. 63 * 67 * 64 68 * @param index the position 65 69 * @return the node at position <code>index</code> … … 69 73 */ 70 74 public Node getNode(int index) { 71 return nodes .get(index);75 return nodes[index]; 72 76 } 73 77 … … 75 79 * Replies true if this way contains the node <code>node</code>, false 76 80 * otherwise. Replies false if <code>node</code> is null. 77 * 81 * 78 82 * @param node the node. May be null. 79 83 * @return true if this way contains the node <code>node</code>, false … … 83 87 public boolean containsNode(Node node) { 84 88 if (node == null) return false; 85 return nodes.contains(node); 89 for (int i=0; i<nodes.length; i++) { 90 if (nodes[i].equals(node)) { 91 return true; 92 } 93 } 94 return false; 86 95 } 87 96 … … 122 131 /** 123 132 * Creates a new way with id 0. 124 * 133 * 125 134 */ 126 135 public Way(){ … … 130 139 /** 131 140 * Create an identical clone of the argument (including the id). 132 * 141 * 133 142 * @param original the original way. Must not be null. 134 143 */ … … 141 150 * Creates a new way for the given id. If the id > 0, the way is marked 142 151 * as incomplete. 143 * 152 * 144 153 * @param id the id. > 0 required 145 154 * @throws IllegalArgumentException thrown if id < 0 … … 151 160 @Override public void cloneFrom(OsmPrimitive osm) { 152 161 super.cloneFrom(osm); 153 nodes.clear(); 154 nodes.addAll(((Way)osm).nodes); 162 Way otherWay = (Way)osm; 163 nodes = new Node[otherWay.nodes.length]; 164 System.arraycopy(otherWay.nodes, 0, nodes, 0, otherWay.nodes.length); 155 165 } 156 166 157 167 @Override public String toString() { 158 168 if (incomplete) return "{Way id="+getId()+" version="+getVersion()+" (incomplete)}"; 159 return "{Way id="+getId()+" version="+getVersion()+" nodes="+Arrays.toString(nodes .toArray())+"}";169 return "{Way id="+getId()+" version="+getVersion()+" nodes="+Arrays.toString(nodes)+"}"; 160 170 } 161 171 … … 180 190 boolean closed = (lastNode() == n && firstNode() == n); 181 191 int i; 182 while ((i = nodes.indexOf(n)) >= 0) { 183 nodes.remove(i); 184 } 185 i = nodes.size(); 192 List<Node> copy = getNodes(); 193 while ((i = copy.indexOf(n)) >= 0) { 194 copy.remove(i); 195 } 196 i = copy.size(); 186 197 if (closed && i > 2) { 187 198 addNode(firstNode()); 188 } else if (i >= 2 && i <= 3 && nodes.get(0) == nodes.get(i-1)) { 189 nodes.remove(i-1); 190 } 199 } else if (i >= 2 && i <= 3 && copy.get(0) == copy.get(i-1)) { 200 copy.remove(i-1); 201 } 202 setNodes(copy); 191 203 } 192 204 … … 202 214 /** 203 215 * Adds a node to the end of the list of nodes. Ignored, if n is null. 204 * 216 * 205 217 * @param n the node. Ignored, if null. 206 218 * @throws IllegalStateException thrown, if this way is marked as incomplete. We can't add a node … … 211 223 if (incomplete) 212 224 throw new IllegalStateException(tr("Cannot add node {0} to incomplete way {1}.", n.getId(), getId())); 213 if (incomplete) return;214 225 clearCached(); 215 nodes.add(n); 226 Node[] newNodes = new Node[nodes.length + 1]; 227 System.arraycopy(nodes, 0, newNodes, 0, nodes.length); 228 newNodes[nodes.length] = n; 229 nodes = newNodes; 216 230 } 217 231 218 232 /** 219 233 * Adds a node at position offs. 220 * 234 * 221 235 * @param int offs the offset 222 236 * @param n the node. Ignored, if null. … … 230 244 throw new IllegalStateException(tr("Cannot add node {0} to incomplete way {1}.", n.getId(), getId())); 231 245 clearCached(); 232 nodes.add(offs, n); 246 Node[] newNodes = new Node[nodes.length + 1]; 247 System.arraycopy(nodes, 0, newNodes, 0, offs); 248 System.arraycopy(nodes, offs, newNodes, offs + 1, nodes.length - offs); 249 newNodes[offs] = n; 250 nodes = newNodes; 233 251 } 234 252 235 253 public boolean isClosed() { 236 254 if (incomplete) return false; 237 return nodes. size()>= 3 && lastNode() == firstNode();255 return nodes.length >= 3 && lastNode() == firstNode(); 238 256 } 239 257 240 258 public Node lastNode() { 241 if (incomplete || nodes. size()== 0) return null;242 return nodes .get(nodes.size()-1);259 if (incomplete || nodes.length == 0) return null; 260 return nodes[nodes.length-1]; 243 261 } 244 262 245 263 public Node firstNode() { 246 if (incomplete || nodes. size()== 0) return null;247 return nodes .get(0);264 if (incomplete || nodes.length == 0) return null; 265 return nodes[0]; 248 266 } 249 267 250 268 public boolean isFirstLastNode(Node n) { 251 if (incomplete || nodes. size()== 0) return false;269 if (incomplete || nodes.length == 0) return false; 252 270 return n == firstNode() || n == lastNode(); 253 271 }
Note:
See TracChangeset
for help on using the changeset viewer.