/** * Creates the levels higher than the given level * * @param boundablesOfALevel the level to build on * @param level the level of the Boundables, or -1 if the boundables are item boundables (that is, * below level 0) * @return the root, which may be a ParentNode or a LeafNode */ private AbstractNode createHigherLevels(List boundablesOfALevel, int level) { Assert.isTrue(!boundablesOfALevel.isEmpty()); List parentBoundables = createParentBoundables(boundablesOfALevel, level + 1); if (parentBoundables.size() == 1) { return (AbstractNode) parentBoundables.get(0); } return createHigherLevels(parentBoundables, level + 1); }
/** * Sorts the childBoundables then divides them into groups of size M, where M is the node * capacity. */ protected List createParentBoundables(List childBoundables, int newLevel) { Assert.isTrue(!childBoundables.isEmpty()); ArrayList parentBoundables = new ArrayList(); parentBoundables.add(createNode(newLevel)); ArrayList sortedChildBoundables = new ArrayList(childBoundables); Collections.sort(sortedChildBoundables, getComparator()); for (Iterator i = sortedChildBoundables.iterator(); i.hasNext(); ) { Boundable childBoundable = (Boundable) i.next(); if (lastNode(parentBoundables).getChildBoundables().size() == getNodeCapacity()) { parentBoundables.add(createNode(newLevel)); } lastNode(parentBoundables).addChildBoundable(childBoundable); } return parentBoundables; }