/*
 * Copyright 2014-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.hazelcast.repository.query;

import com.hazelcast.query.impl.getters.ReflectionHelper;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Map.Entry;


/**
 * <P>Implement a limited form of custom comparison between entries.
 * The fields used for the comparison and the ascending/descending
 * can be specified at run time.
 * </P>
 */
public class HazelcastPropertyComparator implements Comparator<Entry<?, ?>>, Serializable {
    private static final long serialVersionUID = 1L;

    private String attributeName;
    private int direction;

    public HazelcastPropertyComparator(String attributeName, boolean ascending) {
        this.attributeName = attributeName;
        this.direction = (ascending ? 1 : -1);
    }

    /**
     * <P>Use Hazelcast's {@code ReflectionHelper} to extract a field in an
     * entry, and use this is in the comparison.
     * </P>
     *
     * @param o1     An entry in a map
     * @param o2     Another entry in the map
     * @return       Comparison result
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public int compare(Entry<?, ?> o1, Entry<?, ?> o2) {

        try {
            Comparable o1Field = ReflectionHelper.extractValue(o1.getValue(), this.attributeName);
            Comparable o2Field = ReflectionHelper.extractValue(o2.getValue(), this.attributeName);

            if (o1Field != null) {
                return this.direction * o1Field.compareTo(o2Field);
            } else {
                if (o2Field != null) {
                    return -1 * this.direction * o2Field.compareTo(null);
                }
            }

        } catch (Exception ignore) {
            return 0;
        }

        return 0;
    }

}

@@ -1814,26 +1814,22 @@
         
-Comparable
+Object
  o1Field
@@ -1913,18 +1913,14 @@
     
-Comparable
+Object
  o2F
@@ -2013,25 +2013,25 @@
 if (o1Field 
-!
+=
 = null) %7B%0A  
@@ -2069,37 +2069,8 @@
 tion
- * o1Field.compareTo(o2Field)
 ;%0A  
@@ -2084,20 +2084,9 @@
    %7D
- else %7B%0A    
+%0A
     
@@ -2109,9 +2109,9 @@
 eld 
-!
+=
 = nu
@@ -2128,28 +2128,24 @@
             
-    
 return -1 * 
@@ -2162,53 +2162,201 @@
 tion
- * o2Field.compareTo(null);%0A                %7D
+;%0A            %7D%0A            if (o1Field instanceof Comparable && o2Field instanceof Comparable) %7B%0A                return this.direction * ((Comparable) o1Field).compareTo((Comparable) o2Field);
 %0A   

Adjust to HC 3.6 commit faa6428c28eba0b8147e38196fe3711c46773ac8