PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/google/maps/extras/arcgislink/json/OrderedObject.as

http://gmaps-utility-library-flash.googlecode.com/
ActionScript | 105 lines | 56 code | 16 blank | 33 comment | 8 complexity | 9569a2e74d2955965ddae9225ee03862 MD5 | raw file
  1/* ---------------------------------------------------------------------------
  2Author: Manish Jethani (manish.jethani@gmail.com)
  3Version: 0.1
  4Date: December 19, 2008
  5
  6http://manishjethani.com/guaranteeing-enumeration-order
  7
  8Copyright (c) 2008 Manish Jethani
  9
 10Permission is hereby granted, free of charge, to any person obtaining a copy
 11of this software and associated documentation files (the "Software"), to deal
 12in the Software without restriction, including without limitation the rights
 13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 14copies of the Software, and to permit persons to whom the Software is
 15furnished to do so, subject to the following conditions:
 16
 17The above copyright notice and this permission notice shall be included in
 18all copies or substantial portions of the Software.
 19
 20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 26THE SOFTWARE.
 27--------------------------------------------------------------------------- */
 28
 29//package com.manishjethani.code.flash.utils
 30// used in JSONDecoder.as
 31package com.google.maps.extras.arcgislink.json
 32{
 33
 34import flash.utils.Proxy;
 35import flash.utils.flash_proxy;
 36/**
 37 * Modified from http://manishjethani.com/guaranteeing-enumeration-order This class provides support of for..in loop of object properties in order of insertion.
 38 */ 
 39dynamic public class OrderedObject extends Proxy
 40{
 41	private var content:Object = {};
 42	private var contentByIndex:Array = [];
 43
 44	override flash_proxy function deleteProperty(name:*):Boolean
 45	{
 46		var wasDeleted:Boolean = delete content[name];
 47		
 48		if (wasDeleted) {
 49			var n:int = contentByIndex.length;
 50
 51			for (var i:int = 0; i < n; i++) {
 52				if (contentByIndex[i] == name) {
 53					contentByIndex.splice(i, 1);
 54					break;
 55				}
 56			}
 57		}
 58		
 59		return wasDeleted;
 60	}
 61
 62	override flash_proxy function getProperty(name:*):*
 63	{
 64		return content[name];
 65	}
 66
 67	override flash_proxy function hasProperty(name:*):Boolean
 68	{
 69		return content.hasOwnProperty(name);
 70	}
 71
 72	override flash_proxy function nextName(index:int):String
 73	{
 74		return contentByIndex[index - 1];
 75	}
 76
 77	override flash_proxy function nextNameIndex(index:int):int
 78	{
 79		if (index < contentByIndex.length)
 80			return index + 1;
 81		else
 82			return 0;
 83	}
 84
 85	override flash_proxy function nextValue(index:int):*
 86	{
 87		return content[contentByIndex[index - 1]];
 88	}
 89
 90	override flash_proxy function setProperty(name:*, value:*):void
 91	{
 92		content[name] = value;
 93
 94		for each (var p:* in contentByIndex) {
 95			if (p == name)
 96				// If the property already exists, don't change the order.
 97				return;
 98		}
 99
100		contentByIndex.push(name);
101	}
102}
103
104}
105