j.u.Objects follow-up: deepEquals(Object, Object)? (original) (raw)

Joseph D. Darcy Joe.Darcy at Sun.COM
Thu Oct 8 19:59:37 UTC 2009


Another piece of functionality requested in the j.u.Objects thread was a deepEquals(Object a, Object b.) method that "did the right thing" if the arguments happened to dynamically be arrays.

I've been thinking a bit how this might be implemented.

The array-ness of a and b would need to be determined, after any up-front null-checks

boolean aIsArray = a.getClass().isArray(); boolean bIsArray = b.getClass().isArray();

followed various case-analyses.

if (aIsArray && bIsArray) { Class aComponentType = a.getClass().getComponentType(); Class bComponentType = b.getClass().getComponentType(); if (aComponentType == bComponentType) { // long case analysis to cast and call Arrays.deepEquals if ComponentType is a reference type // or the matching Arrays.equals(primitiveComponent[], primitiveComponent[]) method if // aComponentType.isPrimitive(). } else return false; } else return a.equals(b);

Certainly a bit messy internally.

What are scenarios where this method would be used?

-Joe



More information about the core-libs-dev mailing list