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 6c8f4aa67..d60d69a38 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 @@ -1,5 +1,5 @@ /* - * Copyright 2010-2011 the original author or authors. + * Copyright 2010-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. @@ -18,14 +18,15 @@ package org.springframework.data.mongodb.core.query; import java.util.HashMap; import java.util.Map; +import org.springframework.util.ObjectUtils; + import com.mongodb.BasicDBObject; import com.mongodb.DBObject; public class Field { - private Map criteria = new HashMap(); - - private Map slices = new HashMap(); + private final Map criteria = new HashMap(); + private final Map slices = new HashMap(); public Field include(String key) { criteria.put(key, Integer.valueOf(1)); @@ -50,11 +51,54 @@ public class Field { public DBObject getFieldsObject() { DBObject dbo = new BasicDBObject(); for (String k : criteria.keySet()) { - dbo.put(k, (criteria.get(k))); + dbo.put(k, criteria.get(k)); } for (String k : slices.keySet()) { - dbo.put(k, new BasicDBObject("$slice", (slices.get(k)))); + dbo.put(k, new BasicDBObject("$slice", slices.get(k))); } 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; + } + + return true; + } + + /* + * (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.slices); + + 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..8fa0efffc --- /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().include("foo"); + Field right = new Field().include("foo"); + + assertThat(left, is(right)); + assertThat(right, is(left)); + } + + @Test + public void differentObjectSetupCreatesEqualField() { + + Field left = new Field().include("foo"); + Field right = new Field().include("bar"); + + assertThat(left, is(not(right))); + assertThat(right, is(not(left))); + } +}