| | | | Browse by category |
Question
How should I implement an object identifier for my IlpObject?
Answer
The identifier is an Object
that can be used to identify and retrieve an IlpObject
. It must beunique (even across data sources).
The identifier is provided when the IlpObject
is created. It can be either a String
or another type of Java object.
If the identifier is a String
, the code which creates an IlpObject
and assigns an identifier to it must ensure that the identifier is unique.
If the identifier is another type of object, it must satisfy the following constraints:
- The identifier must be convertible to
String
, through anIlpTypeConverter
installed in theIlpContext
. TheIlpDefaultTypeConverter
uses theObject.toString()
method to convert objects of unknown types toString
. TheString
that results from this conversion must be unique.
- It must be possible to create or retrieve the identifier
Object
, given the correspondingString
, by using theIlpTypeConverter
installed in theIlpContext
. TheIlpDefaultTypeConverter
uses a constructor with a singleString
argument to create instances of unknown types. - The identifier
Object
itself must not be an instance ofIlpObject
.
Please note the following recommendations:
- Ideally the identifier should be implemented as a simple object in order to perform a fast comparison. For the sake of simplicity, it is recommended to use simple objects, such as
Number
orString
, which already satisfy the constraints listed above. - If you are using your own Java class as identifier, the
"public boolean equals(Object obj)"
method of the identifier class should be overridden to perform an efficient comparison of identifier instances. The"public int hashcode()"
method should also be overriden and must be consistent with the"public boolean equals(Object obj)"
implementation. - An identifier generator can be used to generate the object identifiers in a consistent manner. A predefined identifier generator is available in
ilog.cpl.util.IlpIDGenerator
.
Note that once the proper implementation for the IlpTypeConverter
is created (normally you do this by subclassing IlpDefaultTypeConverter
), you can register it with the IlpContext
implementation as follows:
IlpDefaultContext context = ...;
context.addService(IlpTypeConverter.class, new CustomTypeConverter(context));