DATAMONGO-2504 - Polishing.
Update equals/hashCode implementation to use the Spring Data form. Make fields final where possible. Use diamond syntax. Reorder methods. Reformat code. Extend tests. Original pull request: #848.
This commit is contained in:
@@ -15,8 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.query;
|
||||
|
||||
import java.util.Objects;
|
||||
import static org.springframework.util.ObjectUtils.*;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* A {@link Term} defines one or multiple words {@link Type#WORD} or phrases {@link Type#PHRASE} to be used in the
|
||||
@@ -91,6 +93,47 @@ public class Term {
|
||||
return negated ? negateRaw(formatted) : formatted;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof Term)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Term term = (Term) o;
|
||||
|
||||
return ObjectUtils.nullSafeEquals(negated, term.negated) && ObjectUtils.nullSafeEquals(type, term.type)
|
||||
&& ObjectUtils.nullSafeEquals(raw, term.raw);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 17;
|
||||
|
||||
result += ObjectUtils.nullSafeHashCode(type);
|
||||
result += ObjectUtils.nullSafeHashCode(raw);
|
||||
result += ObjectUtils.nullSafeHashCode(negated);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return getFormatted();
|
||||
@@ -103,19 +146,4 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ 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;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -35,7 +35,7 @@ import org.springframework.util.StringUtils;
|
||||
public class TextCriteria implements CriteriaDefinition {
|
||||
|
||||
private final List<Term> terms;
|
||||
private @Nullable String language;
|
||||
private final @Nullable String language;
|
||||
private @Nullable Boolean caseSensitive;
|
||||
private @Nullable Boolean diacriticSensitive;
|
||||
|
||||
@@ -52,7 +52,7 @@ public class TextCriteria implements CriteriaDefinition {
|
||||
private TextCriteria(@Nullable String language) {
|
||||
|
||||
this.language = language;
|
||||
this.terms = new ArrayList<Term>();
|
||||
this.terms = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,9 +232,47 @@ public class TextCriteria implements CriteriaDefinition {
|
||||
return new Document("$text", document);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof TextCriteria)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TextCriteria that = (TextCriteria) o;
|
||||
|
||||
return ObjectUtils.nullSafeEquals(terms, that.terms) && ObjectUtils.nullSafeEquals(language, that.language)
|
||||
&& ObjectUtils.nullSafeEquals(caseSensitive, that.caseSensitive)
|
||||
&& ObjectUtils.nullSafeEquals(diacriticSensitive, that.diacriticSensitive);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 17;
|
||||
|
||||
result += ObjectUtils.nullSafeHashCode(terms);
|
||||
result += ObjectUtils.nullSafeHashCode(language);
|
||||
result += ObjectUtils.nullSafeHashCode(caseSensitive);
|
||||
result += ObjectUtils.nullSafeHashCode(diacriticSensitive);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private String join(Iterable<Term> terms) {
|
||||
|
||||
List<String> result = new ArrayList<String>();
|
||||
List<String> result = new ArrayList<>();
|
||||
|
||||
for (Term term : terms) {
|
||||
if (term != null) {
|
||||
@@ -244,21 +282,4 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.springframework.data.mongodb.core.DocumentTestUtils;
|
||||
* Unit tests for {@link TextCriteria}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Daniel Debray
|
||||
*/
|
||||
public class TextCriteriaUnitTests {
|
||||
|
||||
@@ -33,6 +34,7 @@ public class TextCriteriaUnitTests {
|
||||
public void shouldNotHaveLanguageField() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage();
|
||||
|
||||
assertThat(criteria.getCriteriaObject()).isEqualTo(searchObject("{ }"));
|
||||
}
|
||||
|
||||
@@ -40,6 +42,7 @@ public class TextCriteriaUnitTests {
|
||||
public void shouldNotHaveLanguageForNonDefaultLanguageField() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forLanguage("spanish");
|
||||
|
||||
assertThat(criteria.getCriteriaObject()).isEqualTo(searchObject("{ \"$language\" : \"spanish\" }"));
|
||||
}
|
||||
|
||||
@@ -47,6 +50,7 @@ public class TextCriteriaUnitTests {
|
||||
public void shouldCreateSearchFieldForSingleTermCorrectly() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("cake");
|
||||
|
||||
assertThat(criteria.getCriteriaObject()).isEqualTo(searchObject("{ \"$search\" : \"cake\" }"));
|
||||
}
|
||||
|
||||
@@ -54,6 +58,7 @@ public class TextCriteriaUnitTests {
|
||||
public void shouldCreateSearchFieldCorrectlyForMultipleTermsCorrectly() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("bake", "coffee", "cake");
|
||||
|
||||
assertThat(criteria.getCriteriaObject()).isEqualTo(searchObject("{ \"$search\" : \"bake coffee cake\" }"));
|
||||
}
|
||||
|
||||
@@ -61,6 +66,7 @@ public class TextCriteriaUnitTests {
|
||||
public void shouldCreateSearchFieldForPhraseCorrectly() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingPhrase("coffee cake");
|
||||
|
||||
assertThat(DocumentTestUtils.getAsDocument(criteria.getCriteriaObject(), "$text"))
|
||||
.isEqualTo(new Document("$search", "\"coffee cake\""));
|
||||
}
|
||||
@@ -69,6 +75,7 @@ public class TextCriteriaUnitTests {
|
||||
public void shouldCreateNotFieldCorrectly() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().notMatching("cake");
|
||||
|
||||
assertThat(criteria.getCriteriaObject()).isEqualTo(searchObject("{ \"$search\" : \"-cake\" }"));
|
||||
}
|
||||
|
||||
@@ -76,6 +83,7 @@ public class TextCriteriaUnitTests {
|
||||
public void shouldCreateSearchFieldCorrectlyForNotMultipleTermsCorrectly() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().notMatchingAny("bake", "coffee", "cake");
|
||||
|
||||
assertThat(criteria.getCriteriaObject()).isEqualTo(searchObject("{ \"$search\" : \"-bake -coffee -cake\" }"));
|
||||
}
|
||||
|
||||
@@ -83,6 +91,7 @@ public class TextCriteriaUnitTests {
|
||||
public void shouldCreateSearchFieldForNotPhraseCorrectly() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().notMatchingPhrase("coffee cake");
|
||||
|
||||
assertThat(DocumentTestUtils.getAsDocument(criteria.getCriteriaObject(), "$text"))
|
||||
.isEqualTo(new Document("$search", "-\"coffee cake\""));
|
||||
}
|
||||
@@ -91,6 +100,7 @@ public class TextCriteriaUnitTests {
|
||||
public void caseSensitiveOperatorShouldBeSetCorrectly() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("coffee").caseSensitive(true);
|
||||
|
||||
assertThat(DocumentTestUtils.getAsDocument(criteria.getCriteriaObject(), "$text"))
|
||||
.isEqualTo(new Document("$search", "coffee").append("$caseSensitive", true));
|
||||
}
|
||||
@@ -99,15 +109,21 @@ public class TextCriteriaUnitTests {
|
||||
public void diacriticSensitiveOperatorShouldBeSetCorrectly() {
|
||||
|
||||
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("coffee").diacriticSensitive(true);
|
||||
|
||||
assertThat(DocumentTestUtils.getAsDocument(criteria.getCriteriaObject(), "$text"))
|
||||
.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);
|
||||
assertThat(criteriaOne).hasSameHashCodeAs(criteriaTwo);
|
||||
assertThat(criteriaOne).isNotEqualTo(criteriaTwo.diacriticSensitive(false));
|
||||
assertThat(criteriaOne.hashCode()).isNotEqualTo(criteriaTwo.diacriticSensitive(false).hashCode());
|
||||
}
|
||||
|
||||
private Document searchObject(String json) {
|
||||
|
||||
Reference in New Issue
Block a user