package com.hazelcast.util;

import com.hazelcast.query.PagingPredicate;

import java.util.Comparator;
import java.util.Map;

/**
 * @author ali 10/12/13
 */
public final class SortingUtil {

    private SortingUtil() {
    }

    public static int compare(Comparator<Map.Entry> comparator, IterationType iterationType, Map.Entry entry1, Map.Entry entry2){
        if (comparator != null) {
            int result = comparator.compare(entry1, entry2);
            if (result != 0) {
                return result;
            }
            return entry1.getKey().hashCode() - entry2.getKey().hashCode();
        }

        Object comparable1;
        Object comparable2;
        switch (iterationType) {
            case KEY:
                comparable1 = entry1.getKey();
                comparable2 = entry2.getKey();
                break;
            case VALUE:
                comparable1 = entry1.getValue();
                comparable2 = entry2.getValue();
                break;
            default:
                comparable1 = entry1;
                comparable2 = entry2;
                break;
        }
        int result;
        if (comparable1 instanceof Comparable && comparable2 instanceof Comparable) {
            result = ((Comparable) comparable1).compareTo(comparable2);
        } else {
            result = comparable1.hashCode() - comparable2.hashCode();
        }

        if (result != 0) {
            return result;
        }
        return entry1.getKey().hashCode() - entry2.getKey().hashCode();
    }

    public static Comparator<Map.Entry> newComparator(final Comparator<Map.Entry> comparator, final IterationType iterationType){
        return new Comparator<Map.Entry>(){
            public int compare(Map.Entry entry1, Map.Entry entry2) {
                return SortingUtil.compare(comparator, iterationType, entry1, entry2);
            }
        };
    }

    public static Comparator<Map.Entry> newComparator(final PagingPredicate pagingPredicate){
        return new Comparator<Map.Entry>(){
            public int compare(Map.Entry entry1, Map.Entry entry2) {
                return SortingUtil.compare(pagingPredicate.getComparator(), pagingPredicate.getIterationType(), entry1, entry2);
            }
        };
    }

}

@@ -1125,26 +1125,30 @@
   int result
+ = 0
 ;%0A
-
         if (
@@ -1310,16 +1310,206 @@
  else %7B%0A
+            // Use hash-code only for key based comparisons%0A            // since generally hash-codes are not meaningful for values%0A            if (iterationType == IterationType.KEY) %7B%0A    
         
@@ -1562,32 +1562,46 @@
 le2.hashCode();%0A
+            %7D%0A
         %7D%0A%0A     

fix for issue #3047