From 033f44e802052e13c49a521bc178e2736b7f760f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 26 Jun 2012 13:30:00 +0200 Subject: [PATCH] =?UTF-8?q?DATAMONGO-470=20-=20Implemented=20equals(?= =?UTF-8?q?=E2=80=A6)=20and=20hashCode()=20for=20Query=20and=20Criteria.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/mongodb/core/query/Criteria.java | 70 +++++++++++++++++-- .../data/mongodb/core/query/Query.java | 47 +++++++++++++ 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java index 4ce58f14a..14e0c1655 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java @@ -15,6 +15,8 @@ */ package org.springframework.data.mongodb.core.query; +import static org.springframework.util.ObjectUtils.*; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -29,6 +31,7 @@ import org.springframework.data.mongodb.core.geo.Circle; import org.springframework.data.mongodb.core.geo.Point; import org.springframework.data.mongodb.core.geo.Shape; import org.springframework.util.Assert; +import org.springframework.util.ObjectUtils; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; @@ -429,11 +432,9 @@ public class Criteria implements CriteriaDefinition { } /* - * (non-Javadoc) - * - * @see org.springframework.datastore.document.mongodb.query.Criteria# - * getCriteriaObject(java.lang.String) - */ + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.query.CriteriaDefinition#getCriteriaObject() + */ public DBObject getCriteriaObject() { if (this.criteriaChain.size() == 1) { return criteriaChain.get(0).getSingleCriteriaObject(); @@ -496,4 +497,63 @@ public class Criteria implements CriteriaDefinition { } } + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + + if (obj == null || !getClass().equals(obj.getClass())) { + return false; + } + + Criteria that = (Criteria) obj; + + boolean keyEqual = this.key == null ? that.key == null : this.key.equals(that.key); + boolean criteriaEqual = this.criteria.equals(that.criteria); + boolean valueEqual = isEqual(this.isValue, that.isValue); + + return keyEqual && criteriaEqual && valueEqual; + } + + /** + * Checks the given objects for equality. Handles {@link Pattern} and arrays correctly. + * + * @param left + * @param right + * @return + */ + private boolean isEqual(Object left, Object right) { + + if (left == null) { + return right == null; + } + + if (left instanceof Pattern) { + return right instanceof Pattern ? ((Pattern) left).pattern().equals(((Pattern) right).pattern()) : false; + } + + return ObjectUtils.nullSafeEquals(left, right); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + + int result = 17; + + result += nullSafeHashCode(key); + result += criteria.hashCode(); + result += nullSafeHashCode(isValue); + + return result; + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java index 58798acaf..b24015bf2 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java @@ -16,6 +16,7 @@ package org.springframework.data.mongodb.core.query; import static org.springframework.data.mongodb.core.SerializationUtils.*; +import static org.springframework.util.ObjectUtils.*; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -152,4 +153,50 @@ public class Query { return String.format("Query: %s, Fields: %s, Sort: %s", serializeToJsonSafely(getQueryObject()), serializeToJsonSafely(getFieldsObject()), serializeToJsonSafely(getSortObject())); } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + + if (obj == null || !getClass().equals(obj.getClass())) { + return false; + } + + Query that = (Query) obj; + + boolean criteriaEqual = this.criteria.equals(that.criteria); + boolean fieldsEqual = this.fieldSpec == null ? that.fieldSpec == null : this.fieldSpec.equals(that.fieldSpec); + boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort); + boolean hintEqual = this.hint == null ? that.hint == null : this.hint.equals(that.hint); + boolean skipEqual = this.skip == that.skip; + boolean limitEqual = this.limit == that.limit; + + return criteriaEqual && fieldsEqual && sortEqual && hintEqual && skipEqual && limitEqual; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + + int result = 17; + + result += 31 * criteria.hashCode(); + result += 31 * nullSafeHashCode(fieldSpec); + result += 31 * nullSafeHashCode(sort); + result += 31 * nullSafeHashCode(hint); + result += 31 * skip; + result += 31 * limit; + + return result; + } }