Skip to content

Comparing objects? #180

Open
Open

Description

@cmadsen

Why is does (using java-object-diff:0.94)

DiffNode diff = ObjectDifferBuilder.buildDefault().compare(
			new Person(1, "Bob", "London"),
			new Person(2, "Peter", "Paris"));

result in diff.hasChanges()==false?

import static java.lang.System.*;
import static org.assertj.core.api.Assertions.*;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.junit.Test;

import de.danielbechler.diff.ObjectDifferBuilder;
import de.danielbechler.diff.node.DiffNode;

public class Compare {
	static public class Person {
		int id;
		String name;
		String city;

		public Person() {
		}

		Person(int id, String name, String city) {
			this.id = id;
			this.name = name;
			this.city = city;
		}

		@Override
		public boolean equals(Object obj) {
			if (!(obj instanceof Person)) {
				return false;
			}
			if (this == obj) {
				return true;
			}
			final Person otherObject = (Person) obj;
			return new EqualsBuilder().append(this.id, otherObject.id)
					.isEquals();
		}

		@Override
		public int hashCode() {
			return new HashCodeBuilder().append(id).toHashCode();
		}
	}

	@Test
	public void compareMaps() {
		DiffNode diff = ObjectDifferBuilder.buildDefault().compare(
				new Person(1, "Bob", "London"),
				new Person(2, "Peter", "Paris"));
		assertThat(diff.hasChanges()).isTrue();
	}
}

Activity

  1. denov commented on Feb 10, 2017

    @denov

    ha! i ran into this same pretty quick after using this. you're missing getters. i really wish you didn't have to provide them as i'm using this lib mostly with my DTOs that are SUPER simple and didn't have getters/setters.

  2. olasundell commented on Feb 13, 2017

    @olasundell

    That's why I use lombok, I really dislike all that boilerplate code needed.

  3. SQiShER commented on Feb 13, 2017

    @SQiShER
    Owner

    In this case I'm sure you guys will be happy about PR #182, which aims to replace the current Introspector with a version that can deal with simple fields. The PR is more or less feature-complete but requires some more testing to make sure it doesn't break anything when it becomes the new default. Feedback is welcome.

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