2020import java .util .ArrayList ;
2121import java .util .Arrays ;
2222import java .util .Collection ;
23+ import java .util .Iterator ;
2324import java .util .LinkedHashMap ;
2425import java .util .List ;
26+ import java .util .Map ;
2527import java .util .Map .Entry ;
2628import java .util .regex .Pattern ;
2729import java .util .stream .Collectors ;
5961 * @author Mark Paluch
6062 * @author Andreas Zink
6163 * @author Ziemowit Stolarczyk
64+ * @author Clément Petit
6265 */
6366public class Criteria implements CriteriaDefinition {
6467
@@ -976,9 +979,9 @@ private boolean isEqual(Object left, Object right) {
976979 return right == null ;
977980 }
978981
979- if (Pattern . class . isInstance ( left ) ) {
982+ if (left instanceof Pattern ) {
980983
981- if (!Pattern . class . isInstance (right )) {
984+ if (!(right instanceof Pattern )) {
982985 return false ;
983986 }
984987
@@ -989,6 +992,46 @@ private boolean isEqual(Object left, Object right) {
989992 && leftPattern .flags () == rightPattern .flags ();
990993 }
991994
995+ if (left instanceof Document ) {
996+ if (!(right instanceof Document )) {
997+ return false ;
998+ }
999+ Document leftDocument = (Document ) left ;
1000+ Document rightDocument = (Document ) right ;
1001+ Iterator leftIterator = leftDocument .entrySet ().iterator ();
1002+ Iterator rightIterator = rightDocument .entrySet ().iterator ();
1003+
1004+ while (leftIterator .hasNext () && rightIterator .hasNext ()) {
1005+ Map .Entry leftEntry = (Map .Entry )leftIterator .next ();
1006+ Map .Entry rightEntry = (Map .Entry )rightIterator .next ();
1007+ if (!isEqual (leftEntry .getKey (), rightEntry .getKey ())) {
1008+ return false ;
1009+ }
1010+ if (!isEqual (leftEntry .getValue (), rightEntry .getValue ())) {
1011+ return false ;
1012+ }
1013+ }
1014+ return !leftIterator .hasNext () && !rightIterator .hasNext ();
1015+ }
1016+
1017+ if (Collection .class .isAssignableFrom (left .getClass ())) {
1018+ if (!Collection .class .isAssignableFrom (right .getClass ())) {
1019+ return false ;
1020+ }
1021+
1022+ Collection leftCollection = (Collection ) left ;
1023+ Collection rightCollection = (Collection ) right ;
1024+ Iterator leftIterator = leftCollection .iterator ();
1025+ Iterator rightIterator = rightCollection .iterator ();
1026+
1027+ while (leftIterator .hasNext () && rightIterator .hasNext ()) {
1028+ if (!isEqual (leftIterator .next (), rightIterator .next ())) {
1029+ return false ;
1030+ }
1031+ }
1032+ return !leftIterator .hasNext () && !rightIterator .hasNext ();
1033+ }
1034+
9921035 return ObjectUtils .nullSafeEquals (left , right );
9931036 }
9941037
0 commit comments