How best to compare attributes in an operation advisor?
Hi all (and Ralph in particular :) ),
Accepted answer
Hi Cliff,
I think this is one of the many questions that has the ominous answer "It depends".
I don't think there is a general answer that works for all. My experience (and my Java knowledge is also very limited in some areas).
1. Comparing the object usually only works if these Objects implement the necessary interfaces. I rarely use that.I usually at least make sure the object is not null and I can cast to what I expect.
2. If you have an IItemHandle, IItem or IAuditable you can try to get away with the UUID in the handle e.g. com.ibm.team.repository.common.IItemHandle.getItemId() and maybe the stateID com.ibm.team.repository.common.IItemHandle.getStateId() This would be for all kinds of stuff such as contributors. I have to confess that I only found that out recently and have not really used it that often. To be sure you can resolve the item and get the ID or value or what ever.
3. Strings, Dates etc often requires some kind of comparison/format change etc. so even if equals() works might need further considerations and tests
4. Enumerations, you can compare the literals by ID instead by the values only if the enumeration is the same.
5. Often you have different domains e.g. a user name as string and the contributor, or two different enumerations then you have to resolve and compare the specific values or have a mapping to help with that.
Comments
1 vote
One other answer
If you know the type of the attributes is IContributor, you can simply cast the object instance from getValue() to that type:
IAttribute attribute1 = ...;
IContributor contributor1 = (IContributor) attribute1.getValue();
IAttribute attribute2 = ...;
IContributor contributor2 = (IContributor) attribute2.getValue();
if (contributor1.sameItemId(contributor2)) {
// do magic
}