Comparing the state of a Java shared object

Can anyone suggest any way to compare two states of an object?

Is there any API to achieve this? Or, is only the answer the answer?

Please note, I don't need simple equals () / compare () methods as they are only for sorting. I need a whole object mapping that will give me a list of matched or unmatched values ​​in the result.

+3


source to share


5 answers


XMLUnit will work, it compares the list references recursively

, it also has the ability to exclude fields you don't want to compare.

 String expectedXML = "some xml";
 String actualXML = "some xml";

 DetailedDiff diff1 = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML));
 diff1.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
 System.out.println("Differences found: " + diff1.getAllDifferences().toString());

      

RecursiveElementNameAndTextQualifier

Compares all Element and Text nodes in two XML parts. Allows the use of elements of complex, deeply nested types that return in different orders, but have the same content that can be considered comparable.

Refer to this .



Also very soon I am going to post here here .

Generating XML

import com.thoughtworks.xstream.XStream;

 public static String writeToXml(Object objectToWrite) {
        XStream  xstream   = new XStream();
        return xstream.toXML(objectToWrite);
    }

      

Dependence

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
</dependency>

      

0


source


override hashCode and equal method in your class which will help you compare Java object.

see below links

link1



link2

link3

+2


source


Apache Commons Lang introduces an interface Diffable<T>

that an object can implement if it wants to be deeply comparable to other objects. Likewise Comparable<T>

, this interface requires that an object can strongly compare itself to another object of the same type and generate a list of differences.

A DiffBuilder

class exists to help you implement a method Diffable.diff()

.

There is currently no such speculative option, something really forgotten as I accidentally shut down LANG-637 without implementing it. By email Oh.

+2


source


Use the CompareToBuilder class from apache commons-lang. It uses reflection and gives some flexibility to define what to compare and what not. Here are some of them, but we hope that it will be clear:

CompareToBuilder.reflectionCompare(Object lhs, Object rhs, boolean compareTransients)
CompareToBuilder.reflectionCompare(Object lhs, Object rhs, String[] excludeFields) {
CompareToBuilder.reflectionCompare(Object lhs, Object rhs, boolean compareTransients, Class reflectUpToClass) 

      

Loop through the entire object graph using reflection to determine if the objects are equal.

+1


source


Well, you can use reflection to get the field values ​​for a class. And if the fields are the same and they are all associated with getters, you can compare the two objects if you know their type.

public void genericCompare(Object one, Object two) {
    Fields [] oneFields = one.getClass().getFields();
    Fields [] twoFields = two.getClass().getFields();
    ...
    /** assuming they have the same number of fields */
    for (int t = 0; t < oneFields.length; t++) {
        System.out.println("Object one \'" + oneFields[t].getName() + "\' has value " + oneFields[t].get(one));
        System.out.println("Object two \'" + twoFields[t].getName() + "\' has value " + twoFields[t].get(two));
    } 
} 

      

+1


source







All Articles