Skip to content

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

Open

Description

@franzisk

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?

Activity

  1. kissss commented on Aug 16, 2019

    @kissss

    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/

  2. kissss commented on Aug 16, 2019

    @kissss
  3. luongbaotin7777 commented on Jan 13, 2021

    @luongbaotin7777

    @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.!

  4. zaknafein83 commented on Jul 27, 2022

    @zaknafein83

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions