/*
 * Copyright 2014-2015 Rudy De Busscher
 *
 * 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 be.rubus.web.jerry.ordering;


import java.io.Serializable;
import java.util.Comparator;

/**
 * Comparator which order instances according to the info specified in {see InvocationOrder}.
 * Ordening is done from low value to high value and those instances that doesn't have the annotation are placed at the end.
 */
public class InvocationOrderComparator<T> implements Comparator<T>, Serializable {
    private static final long serialVersionUID = 2287258656373616584L;

    public int compare(T nm1, T nm2) {
        if (hasPriority(nm1) && hasPriority(nm2)) {
            return isPriorityHigher(nm1.getClass().getAnnotation(InvocationOrder.class),
                    nm2.getClass().getAnnotation(InvocationOrder.class));
        }
        if (!hasPriority(nm1) && !hasPriority(nm2)) {
            return 0;
        }
        return hasPriority(nm1) ? -1 : 1;
    }

    private int isPriorityHigher(InvocationOrder priority1, InvocationOrder priority2) {
        if (priority1.value() == priority2.value()) {
            return 0;
        }

        return priority1.value() < priority2.value() ? -1 : 1;
    }

    private boolean hasPriority(Object nm) {
        return nm.getClass().isAnnotationPresent(InvocationOrder.class);
    }
}
@@ -659,16 +659,63 @@
 g;%0D%0A%0D%0A%0D%0A
+import be.rubus.web.jerry.utils.ProxyUtils;%0D%0A%0D%0A
 import j
@@ -736,16 +736,16 @@
 zable;%0D%0A
-
 import j
@@ -1266,45 +1266,75 @@
     
-return isPriorityHigher(nm1.get
+InvocationOrder priority1 = (InvocationOrder) getUnproxied
 Class(
+nm1
 ).ge
@@ -1367,17 +1367,17 @@
 r.class)
-,
+;
 %0D%0A      
@@ -1386,29 +1386,75 @@
     
-        nm2.get
+InvocationOrder priority2 = (InvocationOrder) getUnproxied
 Class(
+nm2
 ).ge
@@ -1487,16 +1487,75 @@
 r.class)
+;%0D%0A            return isPriorityHigher(priority1, priority2
 );%0D%0A    
@@ -2021,20 +2021,28 @@
 urn 
-nm.
 get
+Unproxied
 Class(
+nm
 ).is
@@ -2090,10 +2090,130 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    private Class getUnproxiedClass(Object nm) %7B%0D%0A        return ProxyUtils.getUnproxiedClass(nm.getClass());%0D%0A    %7D%0D
 %0A%7D

Fix InvocationOrder comparator - Support for proxies