Ext.namespace("Aquent.Common");
Aquent.Common.Hashtable = function()
{
    this.clear = hashtable_clear;
    this.containsKey = hashtable_containsKey;
    this.containsValue = hashtable_containsValue;
    this.get = hashtable_get;
    this.isEmpty = hashtable_isEmpty;
    this.keys = hashtable_keys;
    this.put = hashtable_put;
    this.remove = hashtable_remove;
    this.size = hashtable_size;
    this.toString = hashtable_toString;
    this.values = hashtable_values;

    var items = {};
    var count = 0;
	var testObject = {};


    /*=======Private methods for internal use only========*/

    function hashtable_clear(){
        items = {};
        count = 0;
    }

    function hashtable_containsKey(key){
        if (testObject[key]) {
			return false;
	    }
		return (items[key] != null);
    }

    function hashtable_containsValue(value){
        var a = getValueArray();
        for (var htIndex = 0; htIndex < a.length; htIndex++) {
            if (a[htIndex] == value) {
                return true;
            }
        }
        return false;
    }

    function hashtable_get(key){
        if(key in items){
			return items[key];	//	object
		}
		return undefined;
    }

    function hashtable_isEmpty(){
        return (count == 0);
    }

    function hashtable_keys(){
        return getKeyArray();
    }

    function hashtable_put(key, value){
        if (key == null || value == null) {
            throw "NullPointerException {" + key + "},{" + value + "}";
        }else{
            var alreadyExists = (key in items);
		    items[key] = value;
		    if(!alreadyExists){
			    count++;
		    }
        }
    }

    function hashtable_remove(key){
		if(key in items && !testObject[key]){
			delete items[key];
			count--;
			return true;
		}
		return false;
    }

    function hashtable_size(){
       return count;
    }

    function hashtable_toString(){
        var result = '';
        var arrKeys = getKeyArray();
        for (var i = 0; i < arrKeys.length; i++)
        {
            if (TI.Util.isNullOrUndefined(items[arrKeys[i]]))
                result += "{" + arrKeys[i] + "},{" + items[arrKeys[i]] + "}\n";
        }
        return result;
    }

    function hashtable_values(){
        return getValueArray();
    }

    function getValueArray() {
        var a = [];
        for(var p in items){
		    if(!testObject[p]){
			    a.push(items[p]);
		    }
	    }
	    return a;
    }
    function getKeyArray() {
        var a = [];
        for(var p in items){
		    if(!testObject[p]){
			    a.push(p);
		    }
	    }
	    return a;
    }
};
