use of com.b2international.commons.collections.BackwardListIterator in project snow-owl by b2ihealthcare.
the class BranchPathUtils method topToBottomIterator.
/**
* Returns with a iterator of branch path for the given branch path argument.
* <br>The iterator traverse the branch path parentage from top to down, hence the first item of the iterator is always the
* {@link IBranchPath#MAIN_BRANCH MAIN branch} then its descendants.
* @param branchPath the branch path.
* @return an iterator for traversing the branch hierarchy from top to down.
*/
public static Iterator<IBranchPath> topToBottomIterator(final IBranchPath branchPath) {
if (isMain(checkNotNull(branchPath, "Branch path argument cannot be null."))) {
return singletonList(branchPath).iterator();
}
checkArgument(!NullBranchPath.INSTANCE.equals(branchPath), "Null branch path is not allowed.");
boolean hasParent = true;
IBranchPath currentPath = branchPath;
// list for storing branch path top most first, descendants then
final List<IBranchPath> $ = newArrayList(currentPath);
while (hasParent) {
final IBranchPath currentParent = currentPath.getParent();
$.add(currentParent);
if (isMain(currentParent)) {
hasParent = false;
} else {
currentPath = currentParent;
}
}
return new BackwardListIterator<IBranchPath>(unmodifiableList($));
}
Aggregations