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 = 0;
        if (comparable1 instanceof Comparable && comparable2 instanceof Comparable) {
            result = ((Comparable) comparable1).compareTo(comparable2);
        } else {
            // Use hash-code only for key based comparisons
            // since generally hash-codes are not meaningful for values
            if (iterationType == IterationType.KEY) {
                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);
            }
        };
    }

}

@@ -532,32 +532,48 @@
          return 
+compareIntegers(
 entry1.getKey().
@@ -574,34 +574,33 @@
 Key().hashCode()
- -
+,
  entry2.getKey()
@@ -602,32 +602,33 @@
 Key().hashCode()
+)
 ;%0A        %7D%0A%0A   
@@ -1119,32 +1119,33 @@
 reak;%0A        %7D%0A
+%0A
         int resu
@@ -1150,12 +1150,8 @@
 sult
- = 0
 ;%0A  
@@ -1339,416 +1339,1403 @@
     
-// Use 
+result = compareIntegers(comparable1.
 hash
--code only for key based comparisons
+Code(), comparable2.hashCode());%0A        %7D%0A%0A        if (result != 0) %7B%0A            return result;%0A        %7D%0A        return compareIntegers(entry1.getKey().hashCode(), entry2.getKey().hashCode());
 %0A    
+%7D%0A%0A
     
-    // since generally hash-codes are not meaningful for values
+/**%0A     * Compares two integers by considering their signs.%0A     *%0A     * Suppose that%0A     *      i1 = -500.000.000%0A     *      i2 = 2.000.000.000%0A     *%0A     * Normally %22i1 %3C i2%22, but if we use %22i1 - i2%22 for comparison%0A     * i1 - i2 = -500.000.000 - 2.000.000.000 and we may accept result as %22-2.500.000.000%22.%0A     * But the actual result is %221.794.967.296%22 because of overflow between%0A     * positive and negative integer bounds.
 %0A     
+*%0A
      
-  if (iterationType == IterationType.KEY) %7B%0A                result = comparable1.hashCode() - comparable2.hashCode();%0A            %7D%0A        %7D%0A%0A        if (result != 0) %7B%0A            return result;%0A        %7D%0A        return entry1.getKey().hashCode() - entry2.getKey().hashCode();
+* So, if we use %22i1 - i2%22 for comparison, since result is greater than 0,%0A     * %22i1%22 is accepted as bigger that %22i2%22. But in fact %22i1%22 is smaller than %22i2%22.%0A     * Therefore, %22i1 - i2%22 is not good way for comparison way between signed integers.%0A     *%0A     * @param i1 First number to compare with second one%0A     * @param i2 Second number to compare with first one%0A     * @return +1 if i1 %3E i2, -1 if i2 %3E i1, 0 if i1 and i2 are equals%0A     */%0A    private static int compareIntegers(int i1, int i2) %7B%0A        // i1 - i2 is not good way for comparison%0A        if (i1 %3E i2) %7B%0A            return +1;%0A        %7D else if (i2 %3E i1) %7B%0A            return -1;%0A        %7D else %7B%0A            return 0;%0A        %7D
 %0A   

fix for issue #3047