Skip to content

Comparing objects? #180

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

Open
cmadsen opened this issue Feb 3, 2017 · 3 comments
Open

Comparing objects? #180

cmadsen opened this issue Feb 3, 2017 · 3 comments

Comments

@cmadsen
Copy link

cmadsen commented Feb 3, 2017

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();
	}
}
@denov
Copy link

denov commented Feb 10, 2017

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.

@olasundell
Copy link

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

@SQiShER
Copy link
Owner

SQiShER commented Feb 13, 2017

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants