Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 63e9eaa

Browse files
committed
Fixed nasty RankedComparator bug (pointed out my Mira) where edge case values caused comparator malfunction
Change-Id: I85774e38449a96854bf88ab0889b05849157b586 Signed-off-by: Jakub Podlesak <[email protected]>
1 parent de2c2f9 commit 63e9eaa

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

core-common/src/main/java/org/glassfish/jersey/model/internal/RankedComparator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -66,7 +66,7 @@ public enum Order {
6666
*/
6767
DESCENDING(-1);
6868

69-
private int ordering;
69+
private final int ordering;
7070

7171
private Order(int ordering) {
7272
this.ordering = ordering;
@@ -85,10 +85,10 @@ public RankedComparator(final Order order) {
8585

8686
@Override
8787
public int compare(final RankedProvider<T> o1, final RankedProvider<T> o2) {
88-
return (getPriority(o1) - getPriority(o2)) * order.ordering;
88+
return ((getPriority(o1) > getPriority(o2)) ? order.ordering : -order.ordering);
8989
}
9090

91-
protected int getPriority(RankedProvider<T> rankedProvider) {
91+
protected int getPriority(final RankedProvider<T> rankedProvider) {
9292
return rankedProvider.getRank();
9393
}
9494
}

core-common/src/test/java/org/glassfish/jersey/process/internal/RankedComparatorTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -71,6 +71,8 @@ public void testPriorityComparator() {
7171
list.add(new RankedProvider<Object>(new F100()));
7272
list.add(new RankedProvider<Object>(new F200()));
7373
list.add(new RankedProvider<Object>(new F200()));
74+
list.add(new RankedProvider<Object>(new F_INT_MIN()));
75+
list.add(new RankedProvider<Object>(new F_INT_MAX()));
7476
Collections.sort(list, new RankedComparator<Object>(RankedComparator.Order.ASCENDING));
7577
int max = Integer.MIN_VALUE;
7678
for (RankedProvider<Object> o : list) {
@@ -112,4 +114,11 @@ private static class F300 {
112114
private static class F1000 {
113115
}
114116

117+
@Priority(Integer.MIN_VALUE)
118+
private static class F_INT_MIN {
119+
}
120+
121+
@Priority(Integer.MAX_VALUE)
122+
private static class F_INT_MAX {
123+
}
115124
}

0 commit comments

Comments
 (0)