UriPatten implements Comparable<UriPatten> { @Override public int compareTo(UriPatten otherUriPattern) { if (otherUriPattern == null) { return 1; } String[] a = URI_VARIABLE_PATTERN.split(patternString); String[] b = URI_VARIABLE_PATTERN.split(otherUriPattern.patternString); for (int i = 0; i < Math.min(a.length, b.length); i++) { int aLen = a[i].length(); int bLen = b[i].length(); if (aLen != bLen) { return bLen - aLen; } } if (a.length == b.length) { return otherUriPattern.patternString.compareTo(patternString); } else { return b.length - a.length; } } UriPatten(String uriPattern); boolean matches(String uri); Optional<Map<String, String>> match(String uri); @Override int hashCode(); @Override int compareTo(UriPatten otherUriPattern); @Override boolean equals(Object obj); @Override String toString();  }
@Test(dataProvider = "orderedUriPatterns") public void testOrdering(String a, String b) { UriPatten aPatten = new UriPatten(a); UriPatten bPatten = new UriPatten(b); int i = aPatten.compareTo(bPatten); int j = bPatten.compareTo(aPatten); Assert.assertTrue(i < 0, a + " should be more specific than " + b); Assert.assertTrue(j > 0, a + " should be more specific than " + b); }
@Test public void testInvariants() { UriPatten[] pattens = new UriPatten[]{ new UriPatten("/"), new UriPatten("/a"), new UriPatten("/ab"), new UriPatten("/a{b}"), new UriPatten("/{a}/{b}"), new UriPatten("/{a}"), new UriPatten("/{a}"), new UriPatten("/a/{b}"), new UriPatten("/ab/{b}"), new UriPatten("/{a}/b"), }; for (int x = 0; x < pattens.length; x++) { for (int y = x + 1; y < pattens.length; y++) { int xy = pattens[x].compareTo(pattens[y]); int yx = pattens[y].compareTo(pattens[x]); Assert.assertTrue(signum(xy) == -signum(yx)); for (int z = y + 1; z < pattens.length; z++) { int xz = pattens[x].compareTo(pattens[z]); int yz = pattens[y].compareTo(pattens[z]); if (xy > 0 && yz > 0) { Assert.assertTrue(xz > 0); } else if (yx > 0 && yz > 0) { Assert.assertTrue(yz > 0); } else if (xy == 0) { Assert.assertTrue(signum(xz) == signum(yz)); } } } } }