Skip to content

Commit

Permalink
Handle nested Pattern and Document in Criteria.equals(…).
Browse files Browse the repository at this point in the history
Closes #3414
Original pull request: #3615.
  • Loading branch information
petitcl authored and mp911de committed Apr 13, 2021
1 parent 5c153dc commit 3e1f95b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -58,6 +60,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Andreas Zink
* @author Clément Petit
*/
public class Criteria implements CriteriaDefinition {

Expand Down Expand Up @@ -901,9 +904,9 @@ private boolean isEqual(Object left, Object right) {
return right == null;
}

if (Pattern.class.isInstance(left)) {
if (left instanceof Pattern) {

if (!Pattern.class.isInstance(right)) {
if (!(right instanceof Pattern)) {
return false;
}

Expand All @@ -914,6 +917,46 @@ private boolean isEqual(Object left, Object right) {
&& leftPattern.flags() == rightPattern.flags();
}

if (left instanceof Document) {
if (!(right instanceof Document)) {
return false;
}
Document leftDocument = (Document) left;
Document rightDocument = (Document) right;
Iterator leftIterator = leftDocument.entrySet().iterator();
Iterator rightIterator = rightDocument.entrySet().iterator();

while (leftIterator.hasNext() && rightIterator.hasNext()) {
Map.Entry leftEntry = (Map.Entry)leftIterator.next();
Map.Entry rightEntry = (Map.Entry)rightIterator.next();
if (!isEqual(leftEntry.getKey(), rightEntry.getKey())) {
return false;
}
if (!isEqual(leftEntry.getValue(), rightEntry.getValue())) {
return false;
}
}
return !leftIterator.hasNext() && !rightIterator.hasNext();
}

if (Collection.class.isAssignableFrom(left.getClass())) {
if (!Collection.class.isAssignableFrom(right.getClass())) {
return false;
}

Collection leftCollection = (Collection) left;
Collection rightCollection = (Collection) right;
Iterator leftIterator = leftCollection.iterator();
Iterator rightIterator = rightCollection.iterator();

while (leftIterator.hasNext() && rightIterator.hasNext()) {
if (!isEqual(leftIterator.next(), rightIterator.next())) {
return false;
}
}
return !leftIterator.hasNext() && !rightIterator.hasNext();
}

return ObjectUtils.nullSafeEquals(left, right);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @author Thomas Darimont
* @author Christoph Strobl
* @author Andreas Zink
* @author Clément Petit
*/
public class CriteriaUnitTests {

Expand Down Expand Up @@ -310,9 +311,33 @@ public void shouldAppendBitsAnySetWithPositionListCorrectly() {
@Test // DATAMONGO-2002
public void shouldEqualForSamePattern() {

Criteria left = new Criteria("field").regex("foo");
Criteria right = new Criteria("field").regex("foo");

assertThat(left).isEqualTo(right);
}

@Test // DATAMONGO-2002
public void shouldEqualForSamePatternAndFlags() {

Criteria left = new Criteria("field").regex("foo", "iu");
Criteria right = new Criteria("field").regex("foo");

assertThat(left).isNotEqualTo(right);
}

@Test // GH-3414
public void shouldEqualForNestedPattern() {

Criteria left = new Criteria("a").orOperator(
new Criteria("foo").regex("value", "i"),
new Criteria("bar").regex("value")
);
Criteria right = new Criteria("a").orOperator(
new Criteria("foo").regex("value", "i"),
new Criteria("bar").regex("value")
);

assertThat(left).isEqualTo(right);
}
}

0 comments on commit 3e1f95b

Please sign in to comment.