package CPX.Portals.Publisher.Components.Algo { /*@Author - Gaurav Pandey @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 insertKeyValue(keyValueCombo:Object):void { if (this == null) throw new ReferenceError("Object instance is null"); if (!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not of MyDictionary type"); if (keyValueCombo == 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[keyValueCombo] != null) throw new ArgumentError("Key already exists"); this[keyValueCombo]=keyValueCombo; } 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 of MyDictionary type"); 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 insertArray(arr:Array):void { if (this == null) throw new ReferenceError("Object instance is null"); if (!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not of MyDictionary type"); if (arr == null) throw new ReferenceError("Argument instance is null"); if (arr.length > 0) for each (var x:Object in arr) if (this[x] != null) { //trace("Value for key:" + x + " already exists"); } else this[x]=x; } 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 of MyDictionary type"); 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 of MyDictionary type"); if (key == null) throw new ArgumentError("Key is null"); if (this[key] != null) { //trace("Value for key: " + key + " does exist"); var obj:Object=this[key]; this[key]=null; delete this[key]; } else { trace("Value for key: " + key + " 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 of MyDictionary type") 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 of MyDictionary type"); 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 of MyDictionary type"); 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():Object { if (this == null) throw new ReferenceError("Object instance is null"); if (!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not of MyDictionary type"); 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(asArray:Boolean=true):Object { if (this == null) throw new ReferenceError("Object instance is null"); if (!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not of MyDictionary type"); var arrC:ArrayCollection=new ArrayCollection(); for (var x:Object in this) arrC.addItem(x); if (asArray) return arrC.toArray().sort(); return arrC; } public function getValues(asArray:Boolean=true):Object { if (this == null) throw new ReferenceError("Object instance is null"); if (!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not of MyDictionary type"); var arrC:ArrayCollection=new ArrayCollection(); for (var x:Object in this) if (this[x] != null) arrC.addItem(this[x]); if (asArray) return arrC.toArray().sort(); return arrC; } public function clone():MyDictionary { if (this == null) throw new ReferenceError("Object instance is null"); if (!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not of MyDictionary type"); var dict:MyDictionary=new MyDictionary(); for (var x:Object in this) dict.insertKey(x, this[x]); return dict; } public function getValue(key : Object):Object { if (this == null) throw new ReferenceError("Object instance is null"); if (!(this is MyDictionary)) throw new MDIllegalObjectException("Object referenced is not of MyDictionary type"); if(this[key] != null) return this[key]; return null; } } }