From 8660933ab5a4059d9013b3fd0174a3577b321a37 Mon Sep 17 00:00:00 2001 From: Richard Fairhurst Date: Wed, 2 Sep 2009 10:33:54 +0000 Subject: [PATCH] generic parent object handling --- net/systemeD/halcyon/connection/Entity.as | 46 +++++++++++++++++++++ net/systemeD/halcyon/connection/Node.as | 16 ------- net/systemeD/halcyon/connection/Relation.as | 9 +++- net/systemeD/halcyon/connection/Way.as | 11 +++-- 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/net/systemeD/halcyon/connection/Entity.as b/net/systemeD/halcyon/connection/Entity.as index 6b52c647..7e5e8a6b 100644 --- a/net/systemeD/halcyon/connection/Entity.as +++ b/net/systemeD/halcyon/connection/Entity.as @@ -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 ''; } diff --git a/net/systemeD/halcyon/connection/Node.as b/net/systemeD/halcyon/connection/Node.as index ac106ad1..b8aceb05 100644 --- a/net/systemeD/halcyon/connection/Node.as +++ b/net/systemeD/halcyon/connection/Node.as @@ -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'; } diff --git a/net/systemeD/halcyon/connection/Relation.as b/net/systemeD/halcyon/connection/Relation.as index 02fccdc0..475a95ef 100644 --- a/net/systemeD/halcyon/connection/Relation.as +++ b/net/systemeD/halcyon/connection/Relation.as @@ -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 { diff --git a/net/systemeD/halcyon/connection/Way.as b/net/systemeD/halcyon/connection/Way.as index ef5ee400..56dad5f6 100644 --- a/net/systemeD/halcyon/connection/Way.as +++ b/net/systemeD/halcyon/connection/Way.as @@ -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 { -- 2.36.1