public static boolean isSubElement(FormIndex parent, FormIndex child) { while (!parent.isTerminal() && !child.isTerminal()) { if (parent.getLocalIndex() != child.getLocalIndex()) { return false; } if (parent.getInstanceIndex() != child.getInstanceIndex()) { return false; } parent = parent.nextLevel; child = child.nextLevel; } // If we've gotten this far, at least one of the two is terminal if (!parent.isTerminal() && child.isTerminal()) { // can't be the parent if the child is earlier on return false; } else if (parent.getLocalIndex() != child.getLocalIndex()) { // Either they're at the same level, in which case only // identical indices should match, or they should have // the same root return false; } else if (parent.getInstanceIndex() != -1 && (parent.getInstanceIndex() != child.getInstanceIndex())) { return false; } // Barring all of these cases, it should be true. return true; }
/** * Trims any negative indices from the end of the passed in index. * * @param index * @return */ public static FormIndex trimNegativeIndices(FormIndex index) { if (!index.isTerminal()) { return new FormIndex(trimNegativeIndices(index.nextLevel), index); } else { if (index.getLocalIndex() < 0) { return null; } else { return index; } } }