[josm-dev] [PATCH 02/26] Way modifications

Dave Hansen dave at sr71.net
Tue Apr 29 03:02:44 BST 2008


These are some modifications to help Way be more manageable.  First,
it requires that object methods be used to access way.nodes.  Doing
this gives us some chance of being able to track when people are
doing things to nodes and lets us do things like reverse lookup
caching.

---

 core-dave/src/org/openstreetmap/josm/data/osm/Way.java |   94 ++++++++++++++++-
 1 file changed, 89 insertions(+), 5 deletions(-)

diff -puN src/org/openstreetmap/josm/data/osm/Way.java~wayhelpers src/org/openstreetmap/josm/data/osm/Way.java
--- core/src/org/openstreetmap/josm/data/osm/Way.java~wayhelpers	2008-04-28 18:59:24.000000000 -0700
+++ core-dave/src/org/openstreetmap/josm/data/osm/Way.java	2008-04-28 18:59:24.000000000 -0700
@@ -1,10 +1,14 @@
 // License: GPL. Copyright 2007 by Immanuel Scholz and others
 package org.openstreetmap.josm.data.osm;
+//package org.openstreetmap.josm.data.osm.visitor;
 
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Collection;
+import java.util.Collections;
 
+import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.data.osm.visitor.Visitor;
 import org.openstreetmap.josm.tools.Pair;
 
@@ -18,7 +22,70 @@ public final class Way extends OsmPrimit
 	/**
 	 * All way nodes in this way
 	 */
-	public final List<Node> nodes = new ArrayList<Node>();
+	private final List<Node> private_nodes = new ArrayList<Node>();
+	public final List<Node> nodes = Collections.unmodifiableList(private_nodes);
+
+	public void addNodes(List<Node> _nodes) {
+		private_nodes.addAll(_nodes);
+	}
+
+	public void addNodeNr(int nr, Node n) {
+		private_nodes.add(nr, n);
+	}
+
+	public int replaceNode(Node old, Node newNode, int index) {
+		Node removed = this.removeNode(index);
+		if (removed == null || removed != old) {
+			Main.debug("Way.replaceNode() could not find " + old + " at index: " + index +
+							   "found instead: " + removed);
+			if (removed != null)
+				this.addNodeNr(index, removed);
+			return -1;
+		}
+		this.addNodeNr(index, newNode);
+		return index;
+	}
+	public int replaceNode(Node old, Node newNode) {
+		int index = nodes.indexOf(old);
+		return replaceNode(old, newNode, index);
+	}
+
+	public void addNode(Node n) {
+		private_nodes.add(n);
+	}
+
+	public int removeNode(Node n) {
+		int index = private_nodes.indexOf(n);
+		if (index == -1) {
+			return index;
+		}
+		private_nodes.remove(index);
+		return index;
+	}
+
+	public Node removeNode(int nodeNr) {
+		Node removed = private_nodes.remove(nodeNr);
+		return removed;
+	}
+
+	public void removeNodes(Collection<OsmPrimitive> del_nodes) {
+		private_nodes.removeAll(del_nodes);
+	}
+
+	public void reverseNodes() {
+		Collections.reverse(this.private_nodes);
+	}
+
+	public void clearAllNodes() {
+		private_nodes.clear();
+	}
+
+	public Node firstNode() {
+		return nodes.get(0);
+	}
+	public Node lastNode() {
+		return nodes.get(nodes.size()-1);
+	}
 
 	public void visitNodes(Visitor v) {
 		for (Node n : this.nodes)
@@ -43,11 +110,19 @@ public final class Way extends OsmPrimit
 		return chunkSet;
 	}
 
-
 	@Override public void visit(Visitor visitor) {
 		visitor.visit(this);
 	}
 
+	@Override public void delete(boolean deleted) {
+		boolean old_deleted = this.deleted;
+		super.delete(deleted);
+		// we didn't change state
+		if (old_deleted == deleted)
+			return;
+		old_deleted = deleted;
+	}
+
 	/**
 	 * Create an identical clone of the argument (including the id)
 	 */
@@ -72,8 +147,8 @@ public final class Way extends OsmPrimit
 	
 	@Override public void cloneFrom(OsmPrimitive osm) {
 		super.cloneFrom(osm);
-		nodes.clear();
-		nodes.addAll(((Way)osm).nodes);
+		private_nodes.clear();
+		private_nodes.addAll(((Way)osm).nodes);
                 checkDirectionTagged();
 	}
 
@@ -82,7 +157,10 @@ public final class Way extends OsmPrimit
     }
 
 	@Override public boolean realEqual(OsmPrimitive osm, boolean semanticOnly) {
-		return osm instanceof Way ? super.realEqual(osm, semanticOnly) && nodes.equals(((Way)osm).nodes) : false;
+		if (!(osm instanceof Way))
+			return false;
+		//Main.debug("Way.realEqual() nodes equal: "+(nodes.equals(((Way)osm).nodes)));
+		return super.realEqual(osm, semanticOnly) && nodes.equals(((Way)osm).nodes);
     }
 
 	public int compareTo(OsmPrimitive o) {
@@ -93,4 +171,10 @@ public final class Way extends OsmPrimit
 	public boolean isIncomplete() {
 		return incomplete;
 	}
+
+	@Override public boolean mayDelete() {
+			if (nodes.size() <= 1)
+					return true;
+			return false;
+	}
 }
_




More information about the josm-dev mailing list