Dedicated specificity comparator in PathPattern

The PathPattern compareTo method is now consistent with equals when
two patterns are of the same specificity but otherwise different.

Separately PathPattern now exposes a Comparator by specificity that
offers the current functionality of compareTo. This can be used for
actual sorting where we only care about specificity.
This commit is contained in:
Rossen Stoyanchev
2017-08-02 18:11:36 +02:00
parent 62fa20fd6f
commit 08dfce2cb5
7 changed files with 58 additions and 48 deletions

View File

@@ -137,7 +137,7 @@ public class PatternsRequestConditionTests {
}
@Test
public void compareEqualPatterns() throws Exception {
public void compareToConsistentWithEquals() throws Exception {
PatternsRequestCondition c1 = createPatternsCondition("/foo*");
PatternsRequestCondition c2 = createPatternsCondition("/foo*");
@@ -155,10 +155,17 @@ public class PatternsRequestConditionTests {
@Test
public void comparePatternSpecificity() throws Exception {
ServerWebExchange exchange = get("/foo").toExchange();
PatternsRequestCondition c1 = createPatternsCondition("/fo*");
PatternsRequestCondition c2 = createPatternsCondition("/foo");
assertEquals(1, c1.compareTo(c2, get("/foo").toExchange()));
assertEquals(1, c1.compareTo(c2, exchange));
c1 = createPatternsCondition("/fo*");
c2 = createPatternsCondition("/*oo");
assertEquals("Patterns are equally specific even if not the same", 0, c1.compareTo(c2, exchange));
}
@Test

View File

@@ -160,7 +160,7 @@ public class HandlerMethodMappingTests {
@Override
protected Comparator<String> getMappingComparator(ServerWebExchange exchange) {
return Comparator.comparing(o -> parser.parse(o));
return (o1, o2) -> PathPattern.SPECIFICITY_COMPARATOR.compare(parser.parse(o1), parser.parse(o2));
}
}