package { /*@Author - Gaurav Pandey (gaurav@caa.columbia.edu) @Description - Part of a Flex-Collection API*/ import flash.utils.Dictionary; import mx.collections.ArrayCollection; /*Hybrid Version of Set and Dictionary as it does not allow duplicates but saves data as key, value pair*/ public dynamic final class MyDictionary extends Dictionary { public function MyDictionary(weakKeys:Boolean=false) { super(weakKeys); } public function insertKey(key : Object, value : Object) : void { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary"); if(key == null) throw new ArgumentError("Key is Nothing"); /*Debatable whether value assigned to a key can be kept as null or not*/ /*if(value == null) throw new ArgumentError("Value is Nothing");*/ if(this[key] != null) throw new ArgumentError("Key already exists"); this[key] = value; } public function getSize() : int { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary"); var count : int = 0; for(var x : Object in this) { count++; } return count; } public function removeKey(key : Object) : void { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary"); if(key == null) throw new ArgumentError("Key is null"); if(this[key] != null) { trace("Value does exist"); var obj : Object = this[key]; this[key] = null; delete this[key]; } else { trace("Value does not exist"); } } public function clear() : void { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary") for(var x : Object in this) { this[x] = null; delete this[x]; } } public function containsKey(key : Object) : Boolean { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary"); if(key == null) throw new ArgumentError("Key passed is null"); if(this[key] != null) return true; return false; } public function containsValue(value : Object) : Boolean { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary"); if(value == null) throw new ArgumentError("Value passed is null"); for(var x : Object in this) { if(this[x] != null) { if(this[x] == value) return true; } } return false; } public function toString() : String { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary"); var sb : String = ""; for(var x : Object in this) { if(this[x] != null) { sb += "key: " + x + " value: " + this[x] + "\n"; } } return sb; } public function getKeys() : Array { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary"); var arrC : ArrayCollection = new ArrayCollection(); for(var x : Object in this) { arrC.addItem(x); } return arrC.toArray().sort(); } public function getValues() : Array { if(this == null) throw new ReferenceError("Object instance is null"); if(!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not a MyDictionary"); var arrC : ArrayCollection = new ArrayCollection(); for(var x : Object in this) { if(this[x] != null) arrC.addItem(x); } return arrC.toArray().sort(); } } }