feat: add deep-array value-equality helpers in sdk-core#149
Merged
Conversation
Add ValueEquality.contentEquals / contentHashCode to org.dexpace.sdk.core.util for value types that hold array-typed fields. java.util.Objects.equals compares arrays by identity, so structurally-equal ByteArray (or any array) fields are wrongly reported unequal; these helpers compare by content instead. Both helpers handle primitive arrays, object arrays, nulls, and arbitrarily-deep nested / multi-dimensional arrays (via Arrays.deepEquals / deepHashCode), while falling back to ordinary equals/hashCode for non-array values. The two methods are mutually consistent: content-equal values always share a content hash.
…semantics contentEquals reimplemented java.util.Objects.deepEquals branch for branch, so delegate to it directly instead. Document that array comparison follows Arrays.equals/Double.equals semantics rather than ==: NaN compares equal and 0.0 != -0.0, for both primitive and boxed arrays. Note that contentHashCode mirrors Arrays.deepHashCode by hand and is kept in lockstep with contentEquals. Add tests for NaN and signed zero across primitive and boxed float/double arrays, plus nested arrays, mixed elements, and non-canonical NaN bit patterns.
… ValueEquality Expand the ValueEquality KDoc to answer the three questions a reader is most likely to have about it: - Why it is public with no in-tree caller: it is a deliberate, forward-looking primitive that hand-written (or later generated) value types target for their array-typed fields. - Why contentEquals is a thin wrapper over Objects.deepEquals: to pair symmetrically with contentHashCode, for which the JDK offers no Objects.deepHashCode counterpart, giving callers one contract-paired API. - That Kotlin unsigned arrays (UByteArray, etc.) are not recognized as arrays and fall through to identity-based equals/hashCode, with the asXxxArray() workaround for content semantics. Doc-only; no signature or behavior change (apiCheck unchanged).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
ValueEqualitytoorg.dexpace.sdk.core.util— content-deepcontentEquals/contentHashCodehelpers for value types that hold array-typed fields.java.util.Objects.equals/hashCodecompare arrays by identity, so two structurally-equalByteArrayfields (or any array-typed field) are reported unequal and hash differently. A value type with array fields therefore cannot rely onObjects.equalsfor a correctequals/hashCode. These helpers compare by content instead.Behavior
contentEquals(a, b)/contentHashCode(value), both@JvmStaticand null-safe (two nulls equal; null hashes to 0).ByteArray,IntArray,DoubleArray, …) compared by element value.Arrays.deepEquals/deepHashCode, so nested and arbitrarily-deep multi-dimensional arrays compare structurally, and object-array elements use their ownequals.IntArrayvsLongArray) are not equal even with matching values, mirroring the JVM's distinct array types.equals/hashCode.contentEquals(a, b)is true,contentHashCode(a) == contentHashCode(b).No core type needs it today; it is the hand-usable runtime primitive a DTO generator (or a hand-written value type) targets for array fields. No code generation is introduced.
Tests
ValueEqualityTestcovers nulls, identity shortcut, scalars, every primitive-array kind, object arrays (including null elements), nested object arrays, nested primitive arrays, deeply-nested multi-dimensional arrays, custom value-type elements, cross-kind mismatches, and equals/hashCode consistency.Gated build (module-scoped)
Both BUILD SUCCESSFUL. The regenerated
sdk-core/api/sdk-core.apisnapshot is committed.Closes #49