Object thisInstance = this.instance;
Object thatInstance = that.instance;
//test the default equality first so we might skip some work:
if (preferInstanceEquality && thisInstance != null && thatInstance != null) return thisInstance.equals(thatInstance);
MarshalledValueByteStream thisRaw = this.raw;
MarshalledValueByteStream thatRaw = that.raw;
if (thisRaw != null && thatRaw != null) return this.raw.equals(that.raw);
if (thisInstance != null && thatInstance != null) return thisInstance.equals(thatInstance);
// if conversion of one representation to the other is necessary, then see which we prefer converting.
if (preferInstanceEquality) {
if (thisInstance == null) {
thisInstance = this.deserialize();
}
if (thatInstance == null) {
thatInstance = that.deserialize();
}
return thisInstance.equals(thatInstance);
} else {
if (thisRaw == null) {
thisRaw = this.serialize();
}
if (thatRaw == null) {
thatRaw = that.serialize();
}
return Arrays.equals(thisRaw.getRaw(), thatRaw.getRaw());
}
}