generic parent object handling
authorRichard Fairhurst <richard@systemed.net>
Wed, 2 Sep 2009 10:33:54 +0000 (10:33 +0000)
committerRichard Fairhurst <richard@systemed.net>
Wed, 2 Sep 2009 10:33:54 +0000 (10:33 +0000)
net/systemeD/halcyon/connection/Entity.as
net/systemeD/halcyon/connection/Node.as
net/systemeD/halcyon/connection/Relation.as
net/systemeD/halcyon/connection/Way.as

index 6b52c647205af1e54994543669efa7059e751f99..7e5e8a6b5720ef98875c0b193b663b516089cfcf 100644 (file)
@@ -1,12 +1,14 @@
 package net.systemeD.halcyon.connection {
 
     import flash.events.EventDispatcher;
+    import flash.utils.Dictionary;
 
     public class Entity extends EventDispatcher {
         private var _id:Number;
         private var _version:uint;
         private var tags:Object = {};
         private var modified:Boolean = false;
+               private var parents:Dictionary = new Dictionary();
 
         public function Entity(id:Number, version:uint, tags:Object) {
             this._id = id;
@@ -23,6 +25,8 @@ package net.systemeD.halcyon.connection {
             return _version;
         }
 
+               // Tag-handling methods
+
         public function hasTags():Boolean {
             for (var key:String in tags)
                 return true;
@@ -66,6 +70,12 @@ package net.systemeD.halcyon.connection {
             return copy;
         }
 
+               public function getTagsHash():Object {
+                       // hm, not sure we should be doing this, but for read-only purposes
+                       // it's faster than using getTagsCopy
+                       return tags;
+               }
+
         public function getTagArray():Array {
             var copy:Array = [];
             for (var key:String in tags )
@@ -73,6 +83,8 @@ package net.systemeD.halcyon.connection {
             return copy;
         }
 
+               // Clean/dirty methods
+
         public function get isDirty():Boolean {
             return modified;
         }
@@ -87,6 +99,40 @@ package net.systemeD.halcyon.connection {
             modified = true;
         }
 
+               // Parent handling
+               
+               public function addParent(parent:Entity):void {
+                       parents[parent]=true;
+               }
+
+               public function removeParent(parent:Entity):void {
+                       delete parents[parent];
+               }
+               
+               public function get parentWays():Array {
+                       var a:Array=[];
+                       for (var o:Object in parents) {
+                               if (o is Way) { a.push(o); }
+                       }
+                       return a;
+               }
+               
+               public function get parentRelations():Array {
+                       var a:Array=[];
+                       for (var o:Object in parents) {
+                               if (o is Relation) { a.push(o); }
+                       }
+                       return a;
+               }
+               
+               public function get parentObjects():Array {
+                       var a:Array=[];
+                       for (var o:Object in parents) { a.push(o); }
+                       return a;
+               }
+
+               // To be overridden
+
         public function getType():String {
             return '';
         }
index ac106ad1bb7ac131665216ab4d6437d36298036d..b8aceb050db0aa22cdf8e76af40b5dc54f719a3f 100644 (file)
@@ -4,7 +4,6 @@ package net.systemeD.halcyon.connection {
         private var _lat:Number;
         private var _latproj:Number;
         private var _lon:Number;
-        private var _ways:Array = new Array();
 
         public function Node(id:Number, version:uint, tags:Object, lat:Number, lon:Number) {
             super(id, version, tags);
@@ -53,21 +52,6 @@ package net.systemeD.halcyon.connection {
                    return 180/Math.PI * (2 * Math.atan(Math.exp(a*Math.PI/180)) - Math.PI/2);
                }
                
-               public function get ways():Array {
-                   return _ways;
-               }
-
-        public function registerAddedToWay(way:Way):void {
-            if ( _ways.indexOf(way) < 0 )
-                _ways.push(way);
-        }
-        
-        public function registerRemovedFromWay(way:Way):void {
-            var i:int = _ways.indexOf(way);
-            if ( i >= 0 )
-                _ways.splice(i, 1);
-        }
-        
                public override function getType():String {
                        return 'node';
                }
index 02fccdc005e5f24b17a3c281d3908af660b35be8..475a95ef00303cd7e33aa294dfad9287966055ab 100644 (file)
@@ -7,6 +7,7 @@ package net.systemeD.halcyon.connection {
         public function Relation(id:Number, version:uint, tags:Object, members:Array) {
             super(id, version, tags);
             this.members = members;
+                       for each (var member:RelationMember in members) { member.entity.addParent(this); }
         }
 
         public function get length():uint {
@@ -18,20 +19,24 @@ package net.systemeD.halcyon.connection {
         }
 
         public function setMember(index:uint, member:RelationMember):void {
-            members.splice(index, 1, member);
+                       member.entity.addParent(this);
+                       members.splice(index, 1, member);
         }
 
         public function insertMember(index:uint, member:RelationMember):void {
+                       member.entity.addParent(this);
             members.splice(index, 0, member);
         }
 
         public function appendMember(member:RelationMember):uint {
+                       member.entity.addParent(this);
             members.push(member);
             return members.length;
         }
 
         public function removeMember(index:uint):void {
-            members.splice(index, 1);
+            var removed:Array=members.splice(index, 1);
+                       removed[0].entity.removeParent(this);
         }
 
                public override function getType():String {
index ef5ee400e224cd7fe7e4b6eaf727734372b67a6a..56dad5f6f8066222c25aa42bf334b09c75a12b85 100644 (file)
@@ -7,8 +7,7 @@ package net.systemeD.halcyon.connection {
         public function Way(id:Number, version:uint, tags:Object, nodes:Array) {
             super(id, version, tags);
             this.nodes = nodes;
-            for each(var node:Node in nodes)
-                node.registerAddedToWay(this);
+                       for each (var node:Node in nodes) { node.addParent(this); }
         }
 
         public function get length():uint {
@@ -20,19 +19,19 @@ package net.systemeD.halcyon.connection {
         }
 
         public function insertNode(index:uint, node:Node):void {
+                       node.addParent(this);
             nodes.splice(index, 0, node);
-            node.registerAddedToWay(this);
         }
 
         public function appendNode(node:Node):uint {
+                       node.addParent(this);
             nodes.push(node);
-            node.registerAddedToWay(this);
             return nodes.length;
         }
 
         public function removeNode(index:uint):void {
-            var splicedNodes:Array = nodes.splice(index, 1);
-            splicedNodes[0].registerRemovedFromWay(this);
+            var removed:Array=nodes.splice(index, 1);
+                       removed[0].removeParent(this);
         }
 
         public override function toString():String {