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 ;
5860 * @author Christoph Strobl
5961 * @author Mark Paluch
6062 * @author Andreas Zink
63+ * @author Clément Petit
6164 */
6265public class Criteria implements CriteriaDefinition {
6366
@@ -901,9 +904,9 @@ private boolean isEqual(Object left, Object right) {
901904 return right == null ;
902905 }
903906
904- if (Pattern . class . isInstance ( left ) ) {
907+ if (left instanceof Pattern ) {
905908
906- if (!Pattern . class . isInstance (right )) {
909+ if (!(right instanceof Pattern )) {
907910 return false ;
908911 }
909912
@@ -914,6 +917,46 @@ private boolean isEqual(Object left, Object right) {
914917 && leftPattern .flags () == rightPattern .flags ();
915918 }
916919
920+ if (left instanceof Document ) {
921+ if (!(right instanceof Document )) {
922+ return false ;
923+ }
924+ Document leftDocument = (Document ) left ;
925+ Document rightDocument = (Document ) right ;
926+ Iterator leftIterator = leftDocument .entrySet ().iterator ();
927+ Iterator rightIterator = rightDocument .entrySet ().iterator ();
928+
929+ while (leftIterator .hasNext () && rightIterator .hasNext ()) {
930+ Map .Entry leftEntry = (Map .Entry )leftIterator .next ();
931+ Map .Entry rightEntry = (Map .Entry )rightIterator .next ();
932+ if (!isEqual (leftEntry .getKey (), rightEntry .getKey ())) {
933+ return false ;
934+ }
935+ if (!isEqual (leftEntry .getValue (), rightEntry .getValue ())) {
936+ return false ;
937+ }
938+ }
939+ return !leftIterator .hasNext () && !rightIterator .hasNext ();
940+ }
941+
942+ if (Collection .class .isAssignableFrom (left .getClass ())) {
943+ if (!Collection .class .isAssignableFrom (right .getClass ())) {
944+ return false ;
945+ }
946+
947+ Collection leftCollection = (Collection ) left ;
948+ Collection rightCollection = (Collection ) right ;
949+ Iterator leftIterator = leftCollection .iterator ();
950+ Iterator rightIterator = rightCollection .iterator ();
951+
952+ while (leftIterator .hasNext () && rightIterator .hasNext ()) {
953+ if (!isEqual (leftIterator .next (), rightIterator .next ())) {
954+ return false ;
955+ }
956+ }
957+ return !leftIterator .hasNext () && !rightIterator .hasNext ();
958+ }
959+
917960 return ObjectUtils .nullSafeEquals (left , right );
918961 }
919962
0 commit comments