Is there an unique identifier for each object?
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Henry_Leung
Posted On:   Monday, March 14, 2005 07:31 PM

Is there any way to get an unique "identifier", like a String or something, of an object such that: 1. different object's "identifier" must be different. 2. if 2 object's identifier is the same, they must be in fact the same object (that is, 2 references are in fact pointing to the same object) Object.hashcode() does not give me what I need because differnt objects can have same hashcode. Perhaps I could use the memory address (if I can retrieve it......) of an object?? Or I need to write a class of my own and implement this myself? Say, assign an unique ID to each instace when it is be created? Please advise.    More>>

Is there any way to get an unique "identifier", like a String or something, of an object such that:



1. different object's "identifier" must be different.

2. if 2 object's identifier is the same, they must be in fact the same object (that is, 2 references are in fact pointing to the same object)



Object.hashcode() does not give me what I need because differnt objects can have same hashcode.



Perhaps I could use the memory address (if I can retrieve it......) of an object??



Or I need to write a class of my own and implement this myself? Say, assign an unique ID to each instace when it is be created?



Please advise.

   <<Less

Re: Is there an unique identifier for each object?

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, March 17, 2005 07:39 AM

Google for references on unique identifier generation. I think it largely depends on clarifying your domain e.g. how wide of a net are you going to cast with your universe of identifiers, etc. All within one JVM, or across multiple JVM's? Is there going to be clustering, replication etc., where some physically distinct objects might actually need to be identified with the same equivalence class?



What order of magnitude is your total number of objects likely to grow to? Tens, hundreds, millions, billions?


How long are your objects going to last? Is the number of identified objects going to grow, without bound? Could the id's ever "cycle" e.g. start over after long enough time has passed so all the original low-numbered ones are long gone?


How fast are you going to be creating and identifying new objects? Will there be a reasonable time lag between requests for new id's, allowing a slower algorithm involving unique timestamps, or must they be generated very very quickly? Will you even be able to have a central id generator/repository, or would that be a bottleneck so you need a more distributed system, with inherent synchronization problems?


Will there be times you want to re-use id's after an object has been gone for long enough, or do you want their unique id to be forever "retired" after the corresponding object is long gone? Will you want to ever garbage collect among your id's, reap "holes", large gaps in your set of id's, for sets of objects that have been gone for long enough?


One example is just to have a table in your database, with a row for each object, and use the primary key as your unique id (maybe using a 'sequence' to generate the keys, whatever). But that depends on your domain, if you do have a database that all your collaborating agents (servers, JVM's, distributed objects, whatever) can reference in a synchronized manner, and if you can indeed manage to maintain a row for each of your objects, etc.


Email systems are another example. Because of the RFC requirements for headers on messages, they need to have "message-id" generating algorithms, which are supposed to be unique across the world of email messages, so look into how they are managed etc. (sendmail, qmail, etc.) Typically they use the hostname or IP address, plus a timestamp, etc. But of course then you must have a unique hostname and/or routable (not NAT'ted) IP address, reasonably synchronized clocks, correct timezone and daylight savings settings, etc.


Also re IP addresses, is your system going to be running long enough that you might have to upgrade to IPv6 addresses?


Microsoft also has their own unique id generation system for COM/DCOM objects, among their tools that come with Visual Studio, MSDN etc. That's where all those long id's come from, in the Windows "registry". I think theirs was modified from a prior system in OSF's DCE? or CORBA? I forget, but look it up.

Re: Is there an unique identifier for each object?

Posted By:   Cristiano_Sadun  
Posted On:   Thursday, March 17, 2005 03:51 AM

Within a single JVM, object references satisfy your requirements.


Apart that, Sun's JVMs do have an id for objects, which happens to be returned by Object's toString() method (and thus any subclass the doesn't override it).


In a distributed environment, the object id is usually handled differently.

Re: Is there an unique identifier for each object?

Posted By:   Cristiano_Sadun  
Posted On:   Thursday, March 17, 2005 03:51 AM

Within a single JVM, object references satisfy your requirements.


Apart that, Sun's JVMs do have an id for objects, which happens to be returned by Object's toString() method (and thus any subclass the doesn't override it).


In a distributed environment, the object id is usually handled differently.

Re: Is there an unique identifier for each object?

Posted By:   Anonymous  
Posted On:   Wednesday, March 16, 2005 01:57 AM

Object.hashCode() does work for most of the time. Better (and slower) is computing a MD5 value. (Ant´s modified selector could give you a hint). But you should think about your scenario. Most of the time I simply use object1.equals(object2) ...
About | Sitemap | Contact