DATAMONGO-2504 - Add hashCode and equals to TextCriteria and Term.

Original pull request: #848.
This commit is contained in:
ddebray
2020-04-01 12:32:02 +02:00
committed by Mark Paluch
parent 318d552797
commit 3ab679bcc7
3 changed files with 41 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.core.query;
import java.util.Objects;
import org.springframework.lang.Nullable;
/**
@@ -102,4 +103,19 @@ public class Term {
protected String negateRaw(String raw) {
return "-" + raw;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Term)) return false;
Term term = (Term) o;
return negated == term.negated &&
type == term.type &&
Objects.equals(raw, term.raw);
}
@Override
public int hashCode() {
return Objects.hash(type, raw, negated);
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core.query;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.bson.Document;
import org.springframework.lang.Nullable;
@@ -243,4 +244,21 @@ public class TextCriteria implements CriteriaDefinition {
return StringUtils.collectionToDelimitedString(result, " ");
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TextCriteria)) return false;
TextCriteria that = (TextCriteria) o;
return Objects.equals(terms, that.terms) &&
Objects.equals(language, that.language) &&
Objects.equals(caseSensitive, that.caseSensitive) &&
Objects.equals(diacriticSensitive, that.diacriticSensitive);
}
@Override
public int hashCode() {
return Objects.hash(terms, language, caseSensitive, diacriticSensitive);
}
}

View File

@@ -103,6 +103,13 @@ public class TextCriteriaUnitTests {
.isEqualTo(new Document("$search", "coffee").append("$diacriticSensitive", true));
}
@Test // DATAMONGO-2504
public void twoIdenticalCriteriaShouldBeEqual() {
TextCriteria criteriaOne = TextCriteria.forDefaultLanguage().matching("coffee");
TextCriteria criteriaTwo = TextCriteria.forDefaultLanguage().matching("coffee");
assertThat(criteriaOne).isEqualTo(criteriaTwo);
}
private Document searchObject(String json) {
return new Document("$text", Document.parse(json));
}