Skip to content

feat: add deep-array value-equality helpers in sdk-core#149

Open
OmarAlJarrah wants to merge 1 commit into
mainfrom
feat/deep-array-value-equality
Open

feat: add deep-array value-equality helpers in sdk-core#149
OmarAlJarrah wants to merge 1 commit into
mainfrom
feat/deep-array-value-equality

Conversation

@OmarAlJarrah

Copy link
Copy Markdown
Member

Summary

Adds ValueEquality to org.dexpace.sdk.core.util — content-deep contentEquals / contentHashCode helpers for value types that hold array-typed fields.

java.util.Objects.equals/hashCode compare arrays by identity, so two structurally-equal ByteArray fields (or any array-typed field) are reported unequal and hash differently. A value type with array fields therefore cannot rely on Objects.equals for a correct equals/hashCode. These helpers compare by content instead.

Behavior

  • contentEquals(a, b) / contentHashCode(value), both @JvmStatic and null-safe (two nulls equal; null hashes to 0).
  • Primitive arrays (ByteArray, IntArray, DoubleArray, …) compared by element value.
  • Object arrays compared with Arrays.deepEquals / deepHashCode, so nested and arbitrarily-deep multi-dimensional arrays compare structurally, and object-array elements use their own equals.
  • Distinct array kinds (object vs primitive, IntArray vs LongArray) are not equal even with matching values, mirroring the JVM's distinct array types.
  • Non-array values fall back to ordinary equals/hashCode.
  • The two methods are mutually consistent: whenever 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

ValueEqualityTest covers 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)

./gradlew :sdk-core:test :sdk-core:ktlintCheck :sdk-core:detekt :sdk-core:apiDump --no-daemon
./gradlew :sdk-core:apiCheck --no-daemon

Both BUILD SUCCESSFUL. The regenerated sdk-core/api/sdk-core.api snapshot is committed.

Closes #49

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.
@OmarAlJarrah

Copy link
Copy Markdown
Member Author

Adds a ValueEquality utility in sdk-core with contentEquals and contentHashCode static helpers that give deep, structural comparison for value types holding arrays, dispatching object arrays through Arrays.deepEquals/deepHashCode, each primitive-array kind through Kotlin's content helpers, and falling back to plain equals/hashCode otherwise. Looks good to merge.

The dispatch covers all eight primitive-array types plus object arrays, with equals/hashCode as the fallback, and the two helpers stay consistent with each other so equal values hash equally. The @JvmStatic surface is Java-friendly and the regenerated sdk-core.api snapshot matches the new public API. Test coverage is thorough: nulls, identity, scalars, every primitive-array kind, object arrays with null elements, nested/multi-dimensional arrays, custom element types, and cross-kind mismatches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a deep-array contentEquals/contentHashCode value-equality utility

1 participant