[ 
https://issues.apache.org/jira/browse/CXF-7765?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16517340#comment-16517340
 ] 

Shon Vella commented on CXF-7765:
---------------------------------

Suggested fix:

{{static int compareTemplates(URITemplate t1, URITemplate t2) {
    int l1 = t1.getLiteralChars().length();
    int l2 = t2.getLiteralChars().length();
    // descending order
    int result = l1 < l2 ? 1 : l1 > l2 ? -1 : 0;
    if (result == 0) {
        int g1 = t1.getVariables().size();
        int g2 = t2.getVariables().size();
        // descending order
        result = g1 < g2 ? 1 : g1 > g2 ? -1 : 0;
        if (result == 0) {
            int gCustom1 = t1.getCustomVariables().size();
            int gCustom2 = t2.getCustomVariables().size();
            result = gCustom1 < gCustom2 ? 1 : gCustom1 > gCustom2 ? -1 : 0;
            if (result == 0) {
                result = t1.getPatternValue().compareTo(t2.getPatternValue());
            }
        }
    }
    return result;
 } }}

> UrlTemplate.compareTemplates returns inconsistent results
> ---------------------------------------------------------
>
>                 Key: CXF-7765
>                 URL: https://issues.apache.org/jira/browse/CXF-7765
>             Project: CXF
>          Issue Type: Bug
>          Components: JAX-RS
>    Affects Versions: 3.1.15
>            Reporter: Shon Vella
>            Priority: Major
>
> When org.apache.cxf.jaxrs.model.URITemplate.compareTemplates() is passed two 
> templates with the same number of literal characters, it returns -1 
> regardless of the order the templates a passed in. I suppose this may be on 
> purpose, but the result is that if compareTemplates() is used as the basis of 
> a Comparator<URITemplate> that is used by java.util.Collections.sort() it can 
> result in:
> {{java.lang.IllegalArgumentException: Comparison method violates its general 
> contract!}}
> I would also expect that this would result in some degree if unpredictability 
> of the prioritization of when selecting the appropriate JAX-RS method to call 
> for a give request.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to