public Model findLastModelInPath(final Model rootModel, Model... pathItems) { if (null != pathItems && pathItems.length > 0) { Model head = rootModel; if (!head.equals(pathItems[0])) return null; for (int i = 1; i < pathItems.length; i++) { Set<Model> children = head.getChildren(); for (Model node : children) { if (node.equals(pathItems[i])) { head = node; continue; } } } return head; } return null; }
/** * @param model * @param copyOfRange * @throws Exception */ public void addLastModel(final Model rootModel, Model lastModel, Model[] pathItems) throws Exception { if (null != pathItems && pathItems.length > 0) { if (!rootModel.equals(pathItems[0])) { throw new Exception("Invalid root item"); } Model head = findLastModelInPath(rootModel, pathItems); if (null != head) { lastModel.setParent(head); head.getChildren().add(lastModel); return; } } return; }