@Test public void testComparison() { assertThat(comparator.compare("/foo", "/foo/")).isNotEqualTo(0); assertThat(comparator.compare("/root/foo", "/root/foo/")).isNotEqualTo(0); ImmutableList<String> list = ImmutableList.of("/foo", "/bar"); List<String> copy = new ArrayList<>(list); Collections.sort(copy, comparator); assertThat(copy).containsExactly("/bar", "/foo"); list = ImmutableList.of("/foo", "/bar", "/baz"); copy = new ArrayList<>(list); Collections.sort(copy, comparator); assertThat(copy).containsExactly("/bar", "/baz", "/foo"); list = ImmutableList.of("/foo/bar", "/foo/baz", "/baz"); copy = new ArrayList<>(list); Collections.sort(copy, comparator); assertThat(copy).containsExactly("/baz", "/foo/bar", "/foo/baz"); list = ImmutableList.of("/", "/foo/bar", "/foo/baz", "/baz"); copy = new ArrayList<>(list); Collections.sort(copy, comparator); assertThat(copy).containsExactly("/", "/baz", "/foo/bar", "/foo/baz"); list = ImmutableList.of("/", "/foo", "/foo/", "/foo/bar", "/fooa"); copy = new ArrayList<>(list); Collections.shuffle(copy); Collections.sort(copy, comparator); assertThat(copy).containsExactly("/", "/foo", "/foo/", "/foo/bar", "/fooa"); }
@Override public int compare(String o1, String o2) { if (o1.equals(o2)) { return 0; } String prefix = Strings.commonPrefix(o1, o2); if (prefix.length() == 0) { return o1.compareTo(o2); } if (o1.length() == prefix.length()) { return -1; } return o1.compareTo(o2); }
PathComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1.equals(o2)) { return 0; } String prefix = Strings.commonPrefix(o1, o2); if (prefix.length() == 0) { return o1.compareTo(o2); } if (o1.length() == prefix.length()) { return -1; } return o1.compareTo(o2); } }
PathComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1.equals(o2)) { return 0; } String prefix = Strings.commonPrefix(o1, o2); if (prefix.length() == 0) { return o1.compareTo(o2); } if (o1.length() == prefix.length()) { return -1; } return o1.compareTo(o2); }  }
PathComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1.equals(o2)) { return 0; } String prefix = Strings.commonPrefix(o1, o2); if (prefix.length() == 0) { return o1.compareTo(o2); } if (o1.length() == prefix.length()) { return -1; } return o1.compareTo(o2); }  @Override int compare(String o1, String o2); }
PathComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1.equals(o2)) { return 0; } String prefix = Strings.commonPrefix(o1, o2); if (prefix.length() == 0) { return o1.compareTo(o2); } if (o1.length() == prefix.length()) { return -1; } return o1.compareTo(o2); }  @Override int compare(String o1, String o2);  }