Changeset 61

Show
Ignore:
Timestamp:
04/27/06 15:39:01 (2 years ago)
Author:
Jan-Klaas Kollhof
Message:

changed hash() to id() because that is what it is doing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/jsolait/jsolait.js

    r60 r61  
    6464                         __bases__: bases, 
    6565                         __id__: '@' + classID, 
    66                          __hash__: function(){ 
    67                             return this.__id__; 
    68                          }, 
    6966                         __str__ : function(){ 
    7067                            return "[class %s]".format(this.__name__); 
     
    111108        } 
    112109    } 
    113     //make sure all jsolait objects have a hash method 
    114     if(proto.__hash__ === undefined){ 
    115         proto.__hash__=function(){ 
    116             if(this.__id__ === undefined){ 
    117                 this.__id__ = '@' + (Class.__idcount__++); 
    118             } 
     110    //make sure all jsolait objects have a id method 
     111    if(proto.__id__ === undefined){ 
     112        proto.__id__=function(){ 
     113            this.__id__ = '@' + (Class.__idcount__++); 
    119114            return this.__id__; 
    120115        }; 
     
    270265 
    271266/** 
    272     Returns a hash value for an object. 
    273     The same object will always return the same hash.  
    274     Most objects are hashable. The following steps are taken to find a hash value: 
     267    Returns a unique id for an object. 
     268    The same object will always return the same id.  
     269    Most objects are id-able. The following steps are taken to find an id. 
    275270    If the obj has an __id__ property that id will be returned. (all jsolait classes have an __id__ property) 
    276     If the obj has a __hash__ method the return value of that method will be returned.(all jsolait objects have a __hash__ method which sets an __id__ property) 
     271    If the obj has a __id__ method the return value of that method will be returned.(all jsolait objects have a __id__ method which sets an __id__ property) 
    277272    If the obj is a String  the string prefixed with $ is returned. 
    278273    If the obj is a Number the number prefixed with a # is returned as a string. 
    279     All other objects are not safely hashable and an exception is thrown unless forceId is true. In that case the object will get a unique __id__ property applied which is returned. 
    280      
    281     @param obj The object to hash. 
    282     @param forceId=false if true it forces hash() to set an __id__ property onto a non hashable object, making it hashable. 
    283     @return A String containing a hash value for the obj. 
    284 **/ 
    285 hash = function(obj, forceId){ 
    286     if(obj.__id__ != null){ 
    287         return obj.__id__; 
    288     }else  if(obj.__hash__){ 
    289         return obj.__hash__(); 
    290     }else if(obj instanceof String || typeof obj == 'string'){ 
    291         return '$' + obj; 
    292     }else if(obj instanceof Number || typeof obj == 'number'){ 
    293         return '#' + obj; 
    294     }else if(forceId){ 
    295         obj.__id__ = '@' + (Class.__idcount__++); 
    296         return obj.__id__; 
    297     }else{ 
    298         throw new jsolait.Exception('Objec cannot be hashed: %s'.format(obj)); 
     274    All other objects are not safely id-able and an exception is thrown unless forceId is true. In that case the object will get a unique __id__ property applied which is returned. 
     275     
     276    @param obj The object to get the id for. 
     277    @param forceId=false if true it forces id() to set an __id__ property onto a non id-able object, making it id-able. 
     278    @return A String containing a id value for the obj. 
     279**/ 
     280id = function(obj, forceId){ 
     281    switch(typeof obj.__id__){ 
     282         
     283        case "undefined": 
     284            if(obj instanceof String || typeof obj == 'string'){ 
     285                return '$' + obj; 
     286            }else if(obj instanceof Number || typeof obj == 'number'){ 
     287                return '#' + obj; 
     288            }else if(forceId){ 
     289                obj.__id__ = '@' + (Class.__idcount__++); 
     290                return obj.__id__; 
     291            }else{ 
     292                throw new jsolait.Exception('Objec cannot be IDed: %s'.format(obj)); 
     293            } 
     294         
     295        case "function": 
     296            return obj.__id__(); 
     297         
     298        default: //string 
     299            return obj.__id__; 
    299300    } 
    300301}; 
  • trunk/jsolait/lib/jsonrpc.js

    r59 r61  
    471471        publ._sendRequest=function(method, params, callback){ 
    472472            var r = new mod.PendingRequest(callback); 
    473             this._pendingRequests[hash(r)] = r; 
    474             var data = mod.marshall({method:method, params:params, id:hash(r)}); 
     473            this._pendingRequests[id(r)] = r; 
     474            var data = mod.marshall({method:method, params:params, id:id(r)}); 
    475475            this._sendMessage(data); 
    476476            return r; 
  • trunk/jsolait/lib/sets.js

    r57 r61  
    4646    /** 
    4747        The Set class. 
    48         All Items added to a set must be hashable using jsolait's hash() function. 
     48        All Items added to a set must be id-able using jsolait's id() function. 
    4949    **/ 
    5050    mod.Set=Class(function(publ, supr){ 
     
    7878        **/ 
    7979        publ.add=function(item){ 
    80             this.items[hash(item)] = item; 
     80            this.items[id(item)] = item; 
    8181            return item; 
    8282        }; 
     
    9191        **/ 
    9292        publ.remove=function(item){ 
    93             var h = hash(item); 
     93            var h = id(item); 
    9494            if(this.items[h] === undefined){ 
    9595                throw new mod.ItemNotFoundInSet(this, item); 
     
    107107        **/ 
    108108        publ.discard=function(item){ 
    109             var h = hash(item); 
     109            var h = id(item); 
    110110            item = this.items[h]; 
    111111            delete this.items[h]; 
     
    119119        **/ 
    120120        publ.contains=function(item){ 
    121             return (this.items[hash(item)] !== undefined); 
     121            return (this.items[id(item)] !== undefined); 
    122122        }; 
    123123        /** 
  • trunk/test/test_core.js

    r50 r61  
    5050         
    5151         
    52         testing.assertEquals("hash()", hash("a"), "$a"); 
    53         testing.assertEquals("hash()", hash("$a"), "$$a"); 
    54         testing.assertEquals("hash()", hash(123), "#123"); 
    55         testing.assertEquals("hash()", hash(mod), mod.__id__); 
     52        testing.assertEquals("id()", id("a"), "$a"); 
     53        testing.assertEquals("id()", id("$a"), "$$a"); 
     54        testing.assertEquals("id()", id(123), "#123"); 
     55        testing.assertEquals("id()", id(mod), mod.__id__); 
    5656         
    5757        logger.log("testing core functions ...");