Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the difference between two simple objects? #210

Open
franzisk opened this issue Jul 31, 2019 · 4 comments
Open

How to get the difference between two simple objects? #210

franzisk opened this issue Jul 31, 2019 · 4 comments

Comments

@franzisk
Copy link

franzisk commented Jul 31, 2019

Sorry but I didn't get the point here.

Let's say I have two simple objects and I need to know if there is a difference between them and if there is I need to know where and I also need to know the previous value of that field and the new value.

Let's get this as an example:

Person person1 = new Person();
person1.setName("Freddy");
person1.setAge(22);

Person person2 = new Person();
person2.setName("Christoph");
person2.setAge(22);

What I want to achieve here is:

  • it was changed: yes
  • the attribute changed was "name"
  • the previous value was "Freddy"
  • the current value is "Christoph"

Any help on that?

@kissss
Copy link

kissss commented Aug 16, 2019

You can do this

    Person person1 = new Person();
        person1.setName("Freddy");
        person1.setAge(22);
        Person person2 = new Person();
        person2.setName("Christoph");
        person2.setAge(22);

        DiffNode diff = ObjectDifferBuilder.startBuilding()
                .inclusion().exclude()
//                .propertyName("name")
                .and().build().compare(person1, person2);
        diff.visit((node, visit) -> {
            final Object baseValue = node.canonicalGet(person1);
            final Object workingValue = node.canonicalGet(person2);
            final String message = node.getPath() + " changed from " + baseValue + " to " + workingValue + " change state " + node.getState();
            System.out.println(message);
        });
        System.out.println(diff.hasChanges());

you should read https://java-object-diff.readthedocs.io/en/latest/user-guide/

@kissss
Copy link

kissss commented Aug 16, 2019

@franzisk

@luongbaotin7777
Copy link

@franzisk

You can do this

    Person person1 = new Person();
        person1.setName("Freddy");
        person1.setAge(22);
        Person person2 = new Person();
        person2.setName("Christoph");
        person2.setAge(22);

        DiffNode diff = ObjectDifferBuilder.startBuilding()
                .inclusion().exclude()
//                .propertyName("name")
                .and().build().compare(person1, person2);
        diff.visit((node, visit) -> {
            final Object baseValue = node.canonicalGet(person1);
            final Object workingValue = node.canonicalGet(person2);
            final String message = node.getPath() + " changed from " + baseValue + " to " + workingValue + " change state " + node.getState();
            System.out.println(message);
        });
        System.out.println(diff.hasChanges());

you should read https://java-object-diff.readthedocs.io/en/latest/user-guide/

hi Bro, how about for child Object into parent object changes? please help me, thank bro very much.!

@zaknafein83
Copy link

You can do this

    Person person1 = new Person();
        person1.setName("Freddy");
        person1.setAge(22);
        Person person2 = new Person();
        person2.setName("Christoph");
        person2.setAge(22);

        DiffNode diff = ObjectDifferBuilder.startBuilding()
                .inclusion().exclude()
//                .propertyName("name")
                .and().build().compare(person1, person2);
        diff.visit((node, visit) -> {
            final Object baseValue = node.canonicalGet(person1);
            final Object workingValue = node.canonicalGet(person2);
            final String message = node.getPath() + " changed from " + baseValue + " to " + workingValue + " change state " + node.getState();
            System.out.println(message);
        });
        System.out.println(diff.hasChanges());

you should read https://java-object-diff.readthedocs.io/en/latest/user-guide/

not work for me, i copy/paste your code.
Could be because i use lombok

@Data
@Accessors(fluent = true)
@Builder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants