1

I am new in Java 8 and love the Compare Feature. But now I have special piece of code, nearly unreadable, in our old projects.

The program imports from another Database some fields for an Object to an table and if there is already a entry for it, it get´s a new version number.

The next part is selecting every entry for this customer and looking for entry´s with 2 versions, then i compares them and marks different fields and put the old entry of the older version behind it.

This is all done with many loops, an comperator and compareTo for 40 fields. Like I said, nearly unreadable. Is there a way in Java 8 like in this thread -> https://stackoverflow.com/questions/369512/how-to-compare-objects-by-multiple-fields but with marking of the difference?

Community
  • 1
  • 1
Lord_Pinhead
  • 127
  • 1
  • 10

2 Answers2

1

Why don't you use the codes in your link?

Comparator.comparing(p->p.firstName)
          .thenComparing(p->p.lastName)
          .thenComparingInt(p->p.age);

If you have accessor methods:

Comparator.comparing(Person::getFirstName)
          .thenComparing(Person::getLastName)
          .thenComparingInt(Person::getAge);
Sifeng
  • 713
  • 9
  • 23
  • Hi, back from vacation :) I think i did explain it wrong. I want to show which field was changed and mark it, if the comperator could show the changes, thats what i meant. – Lord_Pinhead Feb 04 '16 at 09:55
  • Maybe its more clear with the following Example: Person1.lastName = Bar Person2.lastName = Foo. When i compare it, i want to know that lastName has changed from Bar too Foo. One possible workaround is to use https://github.com/SQiShER/java-object-diff - it is relativly simple and i could implement it realy simple there. – Lord_Pinhead Feb 04 '16 at 15:56
  • you used hard code method. it is should be apply any other functions – Madhuka Dilhan Feb 05 '19 at 11:26
0

So, after some search that Java 8 has good compare, the best thing is to use an real simple Library called java-object-diff.

Fast Test, based on the Person Bean in the linked Question:

public class PersonTest {

    @Test
    public void test() {

        // Old Person
        Person person1 = new Person();
        person1.setFirstName("Foo");
        person1.setLastName("Bar");
        person1.setAge("100");

        // New Person
        Person person2 = new Person();
        person2.setFirstName("Foo");
        person2.setLastName("bar");
        person2.setAge("101");

        // Build the DiffNode
        DiffNode root = ObjectDifferBuilder.buildDefault().compare(person1, person2);
        assertTrue(root.hasChanges());

        // Lets compare and change the values
        root.visit(new DiffNode.Visitor() {
            @Override
            public void node(DiffNode node, Visit visit) {
                if (node.hasChanges() && !node.hasChildren()) {
                    node.canonicalSet(person2, node.canonicalGet(person2) + "*");
                }

            }
        });

        System.out.println(person1);
        System.out.println(person2);
    }
}

Printout:

Person [firstName=Foo, lastName=Bar, age=100]
Person [firstName=Foo, lastName=bar*, age=101*]

Throw it to a CSV File or XML via SuperCSV oder Jaxb and the customer can spot the changes. Or put person2 into a Map and export just changed etc. That was my goal. Sadly not Java 8 and more dependencies in the project, but better then some for loops which are unreadable :)

-- Lord_Pinhead

Lord_Pinhead
  • 127
  • 1
  • 10