Assume we have the following class:
public class SingleElementRefType
{
protected JAXBElement<SequenceType> sequence;
// ...
}
It contains the sequence
field of type JAXBElement
.
JAXBElement
is a third-party class (standard API, actually), which is in essence a pure value class, but it for some reason does not implement hashCode
and equals
methods.
From my point of view, these methods are absolutely reasonable there.
I would like to implement equals
and hashCode
methods for SingleElementRefType
as well as SequenceType
so that I could do a deep comparison of this values. But JAXBElement
stands in the way.
Since I can't extend JAXBElement
, my idea was to integrate hashCode
and equals
into the aggregating class (SingleElementRefType
here):
JAXBElement<SequenceType> theSequence;
theSequence = this.getSequence();
final QName theSequenceName = theSequence.getName();
currentHashCode = ((currentHashCode* 37) +
((theSequenceName!= null)?theSequenceName.hashCode(): 0));
final Object theSequenceValue = theSequence.getValue();
currentHashCode = ((currentHashCode* 37) +
((theSequenceValue!= null)?theSequenceValue.hashCode(): 0));
But then I had second thoughts if I'm not breaking some convention or rule here.
Are there any dangers of implementing hashCode
and equals
for third-party classes in my aggregating classes?
Update: for certain reasons my code may not have further runtime dependencies. So I can't use Guava or commons-lang
here.