0

I'm stuck with a problem of comparing objects deeply and highlighting the differences in a webpage. It has 4 domain classes, ServerTypes, Server, Components & Properties. All these are connected by beans.

Below are the code snippets of above domain classes.

  class ServerTypes {

    private List<Server> server;

    //getters&setters
}

    class Server {

    private List<Components> components; 
    //getters & setters
   } 

    class Components {

    private List<Properties> properties;
    //getters & setters

    }

    class Properties {
    private List<String> prop;
    //getters & setters

}

ServerTypes- > Server ->Components-> Properties

Beans depend on the above hierarchy. I've to loop through each property of the service class, extract the data and then compare with the ones present in a config file.

Comparison is done on all the objects of ServerTypes, Server, Components and Properties classes.

Now, coming to problem, I'm feeling difficulty in looping through each object and doing a deeper comparison and on top of this, I'm struggling to show the differences in webpage with this approach.

Is there any suggestion from talents here to do it in a sensible and easier way rather looping through each object and doing a crude comparison?

I've tried to present this in the best possible way I can. If it is still unclear, kindly let me know, I'm happy to edit the question for you.

Many Thanks in advance.

harshavmb
  • 3,404
  • 3
  • 21
  • 55

1 Answers1

0

(1) Object comparison

You could try javers:

I've never used it, but what I see looks good. I took an example from their site (here)

Diff diff = javers.compare(person1, person2);

List<ValueChange> changesByType = diff.getChangesByType(ValueChange.class);
for (ValueChange valueChange : changesByType) {
    System.out.println(valueChange);
}

Output:

ValueChange{globalId:'Person/#pets/0', property:'name', oldVal:'cat', newVal:'dog'} ValueChange{globalId:'Person/#pets/0', property:'age', oldVal:'1', newVal:'2'}

Another other answers on this: Is there a Java library that can "diff" two Objects?


(2) Json comparison

You could also convert your object to json in order to perform the comparison. Then, the diff becomes a simple text comparison.


(3) How to display

As for how to display it, I would take a look at how others are displaying online comparison:

Community
  • 1
  • 1
alexbt
  • 16,415
  • 6
  • 78
  • 87