@Test public void testIsAncestor() { Assert.assertTrue(CategoryPath.ROOT.isAncestorOrSelf(CategoryPath.ROOT)); CategoryPath apple = new CategoryPath("/apple"); Assert.assertTrue(apple.isAncestorOrSelf(CategoryPath.ROOT)); Assert.assertFalse(CategoryPath.ROOT.isAncestorOrSelf(apple)); CategoryPath applesub = new CategoryPath("/apple/sub"); Assert.assertTrue(applesub.isAncestorOrSelf(apple)); Assert.assertTrue(applesub.isAncestorOrSelf(applesub)); CategoryPath notapple = new CategoryPath("/notapple"); Assert.assertFalse(applesub.isAncestorOrSelf(notapple)); CategoryPath app = new CategoryPath("/app"); Assert.assertFalse(applesub.isAncestorOrSelf(app)); }
public boolean isAncestorOrSelf(CategoryPath categoryPath) { if (categoryPath.isRoot()) { return true; } if (isRoot()) { return false; } String otherCategory = categoryPath.getPath(); String myCategory = getPath(); if (!myCategory.startsWith(otherCategory)) { return false; } if (myCategory.length() == otherCategory.length()) { return true; } return myCategory.charAt(otherCategory.length()) == DELIMITER_CHAR; }
CategoryPath implements Comparable<CategoryPath> { public boolean isAncestorOrSelf(CategoryPath categoryPath) { if (categoryPath.isRoot()) { return true; } if (isRoot()) { return false; } String otherCategory = categoryPath.getPath(); String myCategory = getPath(); if (!myCategory.startsWith(otherCategory)) { return false; } if (myCategory.length() == otherCategory.length()) { return true; } return myCategory.charAt(otherCategory.length()) == DELIMITER_CHAR; } }
CategoryPath implements Comparable<CategoryPath> { public boolean isAncestorOrSelf(CategoryPath categoryPath) { if (categoryPath.isRoot()) { return true; } if (isRoot()) { return false; } String otherCategory = categoryPath.getPath(); String myCategory = getPath(); if (!myCategory.startsWith(otherCategory)) { return false; } if (myCategory.length() == otherCategory.length()) { return true; } return myCategory.charAt(otherCategory.length()) == DELIMITER_CHAR; } CategoryPath(String path);  CategoryPath(CategoryPath parent, String name); }
CategoryPath implements Comparable<CategoryPath> { public boolean isAncestorOrSelf(CategoryPath categoryPath) { if (categoryPath.isRoot()) { return true; } if (isRoot()) { return false; } String otherCategory = categoryPath.getPath(); String myCategory = getPath(); if (!myCategory.startsWith(otherCategory)) { return false; } if (myCategory.length() == otherCategory.length()) { return true; } return myCategory.charAt(otherCategory.length()) == DELIMITER_CHAR; } CategoryPath(String path);  CategoryPath(CategoryPath parent, String name); boolean isRoot(); String getName(); String getPath(); CategoryPath getParent(); @Override boolean equals(Object obj); @Override int hashCode(); boolean isAncestorOrSelf(CategoryPath categoryPath); String[] getPathElements(); @Override int compareTo(CategoryPath otherPath); @Override String toString(); }
CategoryPath implements Comparable<CategoryPath> { public boolean isAncestorOrSelf(CategoryPath categoryPath) { if (categoryPath.isRoot()) { return true; } if (isRoot()) { return false; } String otherCategory = categoryPath.getPath(); String myCategory = getPath(); if (!myCategory.startsWith(otherCategory)) { return false; } if (myCategory.length() == otherCategory.length()) { return true; } return myCategory.charAt(otherCategory.length()) == DELIMITER_CHAR; } CategoryPath(String path);  CategoryPath(CategoryPath parent, String name); boolean isRoot(); String getName(); String getPath(); CategoryPath getParent(); @Override boolean equals(Object obj); @Override int hashCode(); boolean isAncestorOrSelf(CategoryPath categoryPath); String[] getPathElements(); @Override int compareTo(CategoryPath otherPath); @Override String toString(); static final char DELIMITER_CHAR; static final String DELIMITER_STRING; static final CategoryPath ROOT; }