/** * get the ancestor based on level, level = 0, means itself. if level is out of the boundary, just * return the toppest one * * @param level * @return it will never return null. */ public CourseComponent getAncestor(int level) { if (parent == null || level == 0) return this; IBlock root = parent; while (level != 0 && root.getParent() != null) { root = root.getParent(); level--; } return (CourseComponent) root; }
/** get ancestor with give blockType, starting from itself */ public CourseComponent getAncestor(EnumSet<BlockType> types) { if (types.contains(type)) return this; IBlock ancestor = parent; if (ancestor == null) return null; do { if (types.contains(ancestor.getType())) return (CourseComponent) ancestor; } while ((ancestor = ancestor.getParent()) != null); return null; }