PermissionEntry implements Comparable<PermissionEntry>, PermissionConstants { @Override public int compareTo(@NotNull PermissionEntry pe) { if (Objects.equal(path, pe.path)) { return Integer.compare(pe.index, index); } else { final int depth = PathUtils.getDepth(path); final int otherDepth = PathUtils.getDepth(pe.path); if (depth == otherDepth) { return path.compareTo(pe.path); } else { return (depth < otherDepth) ? 1 : -1; } } } PermissionEntry(@NotNull String path, boolean isAllow, int index,
                    @NotNull PrivilegeBits privilegeBits,
                    @NotNull RestrictionPattern restriction); @Override int compareTo(@NotNull PermissionEntry pe); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testCompareToEqualPath() { assertEquals(0, entry.compareTo(entry)); assertEquals(0, entry.compareTo(new PermissionEntry(path, entry.isAllow, index, entry.privilegeBits, entry.restriction))); PermissionEntry higherIndexEntry = new PermissionEntry(path, entry.isAllow, index + 1, entry.privilegeBits, entry.restriction); assertEquals(1, entry.compareTo(higherIndexEntry)); assertEquals(-1, higherIndexEntry.compareTo(entry)); }
@Test public void testCompareToDifferentPathSameDepth() { String sameDepthPath = "/another"; PermissionEntry anotherEntry = new PermissionEntry(sameDepthPath, entry.isAllow, index, entry.privilegeBits, entry.restriction); PermissionEntry anotherEntry2 = new PermissionEntry(sameDepthPath, !entry.isAllow, 3, PrivilegeBits.BUILT_IN.get(PrivilegeConstants.JCR_LOCK_MANAGEMENT), mock(RestrictionPattern.class)); assertEquals(path.compareTo(sameDepthPath), entry.compareTo(anotherEntry)); assertEquals(path.compareTo(sameDepthPath), entry.compareTo(anotherEntry2)); assertNotEquals(sameDepthPath.compareTo(path), entry.compareTo(anotherEntry)); assertNotEquals(sameDepthPath.compareTo(path), entry.compareTo(anotherEntry2)); }
@Test public void testCompareToDifferentPathHigherDepth() { String higherDepthPath = PathUtils.concat(path, "to", "higher", "depth"); PermissionEntry higherDepthEntry = new PermissionEntry(higherDepthPath, entry.isAllow, index, entry.privilegeBits, entry.restriction); assertEquals(1, entry.compareTo(higherDepthEntry)); }
@Test public void testCompareToDifferentPathLowerDepth() { String lowerDepthPath = PathUtils.getParentPath(path); PermissionEntry lowerDepthEntry = new PermissionEntry(lowerDepthPath, entry.isAllow, index, entry.privilegeBits, entry.restriction); assertEquals(-1, entry.compareTo(lowerDepthEntry)); }