Deep comparison of two objects' graphs

A

Albretch

I am trying to find a Java strategy to compare two objects and if
specified the object’s superclasses and/or subclasses must be
compared as well in the deep fashion, so that if there is a class
Field all the way down in the last level in the object graph
hierarchy the difference between the values is indicated along
with the reference path to the method.
 
P

Paul Lutus

Albretch said:
I am trying to find a Java strategy to compare two objects and if
specified the object?s superclasses and/or subclasses must be
compared as well in the deep fashion, so that if there is a class
Field all the way down in the last level in the object graph
hierarchy the difference between the values is indicated along
with the reference path to the method.

So perhaps a code post will get things rolling. Java can certainly do this
sort of evaluation.
 
M

Michael Borgwardt

Albretch said:
I am trying to find a Java strategy to compare two objects and if
specified the object’s superclasses and/or subclasses must be
compared as well in the deep fashion, so that if there is a class
Field all the way down in the last level in the object graph
hierarchy the difference between the values is indicated along
with the reference path to the method.

This should be done by implementing the equals() method in all
of the involved classes.
 
A

Albretch

Michael Borgwardt said:
This should be done by implementing the equals() method in all
of the involved classes.

This should be done by implementing the equals() method . . .

AM: Actually, no. Check out the javadocs:

/docs/api/java/lang/Object.html#equals(java.lang.Object)

and the JLS http://java.sun.com/docs/books/jls/

.. . .
4.3.2 The Class Object
.. . .
The methods equals and hashCode are very useful in hashtables such as
java.util.Hashtable. The method equals defines a notion of object
equality, which is based on value, not reference, comparison.

implementing the equals() method in all of the involved classes.

AM: Well, actually this is what I am trying to avoid/work around of.
Do you know of any libraries to do it?
 
A

Albretch

Paul Lutus said:
So perhaps a code post will get things rolling. Java can certainly do this
sort of evaluation.

AM: . . . Hmm! I expected for something like that to exist already.

Do you see the need/general usefulness for such a library?

I have been scratching some code here and there in my own projects.
Let me clean up my act and post the code/ideas here.

Paul (or anyone else with enough experiece, time, . . .) are you into
it?

Perhaps, yet another open source project could sprout out of it
 
M

Michael Borgwardt

Albretch said:
AM: Actually, no. Check out the javadocs:

Nothing new to me. I just don't see why you consider it an inappropriate solution.

AM: Well, actually this is what I am trying to avoid/work around of.
Do you know of any libraries to do it?

Why? Out of lazyness? It could be done via reflection, but would probably
be pretty slow, and it could cause all sorts of problems by not taking into
account class specific things.
 
A

Albretch

Michael Borgwardt said:
Why? Out of lazyness? It could be done via reflection, but would probably
be pretty slow, and it could cause all sorts of problems by not taking into
account class specific things.

. . . class specific things . . .
Like?
 
M

Michael Borgwardt

Albretch said:
. . . class specific things . . .
Like?

e.g. when a class has fields for internal computations or caching
that are not really a part of its state (as far as equality is concerned).

Or if there are fields whose values form an equivalence class.
 
A

Albretch

From: Michael Borgwardt ([email protected])
Subject: Re: Deep comparison of two objects' graphs

View this article only
Newsgroups: comp.lang.java.programmer
Date: 2004-09-09 10:27:35 PST


e.g. when a class has fields for internal computations or caching
that are not really a part of its state (as far as equality is concerned).

Or if there are fields whose values form an equivalence class.

Hmm! Correct me if I am wrong, but:

1._ doesn't the Field class have an isSynthetic() method for that?
2._ when you say:

"when a class has fields for internal computations or caching that
are not really a part of its state"

;do you mean:

the JVM tags some -object's- fields with processing data which does
not belong to the class proper?

Using reflection would give you an exact/reliable map of the class
and its objects' states.

Or?
 
M

Michael Borgwardt

Albretch said:
Hmm! Correct me if I am wrong, but:

1._ doesn't the Field class have an isSynthetic() method for that?

Only since 1.5, and that's not what I meant anyway. Interesting though.
It refers to "a synthetic field as defined by the Java Language
Specification", but I can't find anything like that in the online JLS
second edition...
2._ when you say:

"when a class has fields for internal computations or caching that
are not really a part of its state"

;do you mean:

the JVM tags some -object's- fields with processing data which does
not belong to the class proper?

Using reflection would give you an exact/reliable map of the class
and its objects' states.

No I meant that a class *as written by the programmer* can contain
fields that should not be considered part of its state since they
exist only for convenience or performance reasons, are maybe
initialized lazily.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top