From 38a86033be2b896447a1c9579f66bb025d8e2cb3 Mon Sep 17 00:00:00 2001 From: Philipp Schneider Date: Wed, 24 Apr 2013 12:39:48 +0200 Subject: [PATCH] =?UTF-8?q?DATAMONGO-663=20-=20Added=20equals(=E2=80=A6)?= =?UTF-8?q?=20and=20hashCode()=20to=20Field.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/mongodb/core/query/Field.java | 55 +++++++++++++++++++ .../mongodb/core/query/FieldUnitTests.java | 49 +++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java index d8dacc666..cafff20bb 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Field.java @@ -20,6 +20,7 @@ import java.util.Map; import java.util.Map.Entry; import org.springframework.util.Assert; +import org.springframework.util.ObjectUtils; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; @@ -103,4 +104,58 @@ public class Field { return dbo; } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object object) { + + if (this == object) { + return true; + } + + if (!(object instanceof Field)) { + return false; + } + + Field that = (Field) object; + + if (!this.criteria.equals(that.criteria)) { + return false; + } + + if (!this.slices.equals(that.slices)) { + return false; + } + + if (!this.elemMatchs.equals(that.elemMatchs)) { + return false; + } + + boolean samePositionKey = this.postionKey == null ? that.postionKey == null : this.postionKey + .equals(that.postionKey); + boolean samePositionValue = this.positionValue == that.positionValue; + + return samePositionKey && samePositionValue; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + + int result = 17; + + result += 31 * ObjectUtils.nullSafeHashCode(this.criteria); + result += 31 * ObjectUtils.nullSafeHashCode(this.elemMatchs); + result += 31 * ObjectUtils.nullSafeHashCode(this.slices); + result += 31 * ObjectUtils.nullSafeHashCode(this.postionKey); + result += 31 * ObjectUtils.nullSafeHashCode(this.positionValue); + + return result; + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java new file mode 100644 index 000000000..29085ea53 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/FieldUnitTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core.query; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Unit tests for {@link Field}. + * + * @author Oliver Gierke + */ +public class FieldUnitTests { + + @Test + public void sameObjectSetupCreatesEqualField() { + + Field left = new Field().elemMatch("key", Criteria.where("foo").is("bar")); + Field right = new Field().elemMatch("key", Criteria.where("foo").is("bar")); + + assertThat(left, is(right)); + assertThat(right, is(left)); + } + + @Test + public void differentObjectSetupCreatesEqualField() { + + Field left = new Field().elemMatch("key", Criteria.where("foo").is("bar")); + Field right = new Field().elemMatch("key", Criteria.where("foo").is("foo")); + + assertThat(left, is(not(right))); + assertThat(right, is(not(left))); + } +}