fix: fix equal bug in some files#10574
Conversation
fix equal bug in some files. equals/hashCode 基于当前字段值。若对象在集合中时字段被修改,可能导致定位失败——这是这些类本就存在的可变性问题,本次修复仅保证契约一致性,未改变可变性。
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Code Review
This PR adds equals() and hashCode() implementations to 7 classes that implement Comparable but were missing proper equality methods.
Overall Assessment: ✅ Generally good, with minor suggestions
Changes Reviewed:
TraceContext.java- UsestimeStampfor equalityGrpcClientChannel.java- UsesclientIdfor equalityRemoteChannel.java- Usesidfor equalityPopCheckPoint.java- UsesstartOffsetfor equalityFileSegment.java- UsesbaseOffsetfor equalityGroupConsumeInfo- Usescount+diffTotalfor equalityTagCountBean- Usestagfor equality
Observations:
-
Consistency with
compareTo(): Most implementations align equality fields withcompareTo(), which is good practice. However, note thatTraceContextuses onlytimeStampwhilecompareTo()also usestimeStamp— this is consistent. -
Null handling:
GrpcClientChannelandTagCountBeanproperly handle null values inequals()andhashCode(). Good. -
Potential issue in
GroupConsumeInfo: ThecompareTo()usesdiffTotal - diffTotal(subtraction), butequals()uses field comparison. This is acceptable, but ensure the class is not used in sorted collections wherecompareTo()returning 0 should implyequals()returning true. -
Missing
Objects.equals(): Some implementations use manual null checks instead ofObjects.equals(). Consider using:return Objects.equals(this.tag, that.tag);
Suggestion: Consider adding unit tests to verify the equals()/hashCode() contracts (reflexive, symmetric, transitive, consistent).
Review by @RockteMQ-AI
Which Issue(s) This PR Fixes
Brief Description
compareTo 与 Object.equals 共存.
在 TreeSet/TreeMap 中,compareTo 返回 0 的两个对象会被视为"相等"但 equals() 返回 false,违反 Comparable 契约,可能导致集合中元素丢失或行为不一致。
修复:重写 equals/hashCode 与 compareTo 一致。
How Did You Test This Change?