MultiTreeNode extends TreeNode<T> { public boolean hasSubtrees(Collection<? extends MultiTreeNode<T>> subtrees) { if (isLeaf() || areAllNulls(subtrees)) { return false; } for (MultiTreeNode<T> subtree : subtrees) { if (!this.hasSubtree(subtree)) { return false; } } return true; } MultiTreeNode(T data); boolean addSubtrees(Collection<? extends MultiTreeNode<T>> subtrees); Collection<? extends MultiTreeNode<T>> siblings(); boolean hasSubtrees(Collection<? extends MultiTreeNode<T>> subtrees); boolean dropSubtrees(Collection<? extends MultiTreeNode<T>> subtrees);  }
@Test public void testHasSubtrees() { String messageContains = "The current tree node was expected to contain all of the subtrees, but actually was not"; Collection<MultiTreeNode<String>> mSubtreesContain1 = new ArrayList<>(2); mSubtreesContain1.add((MultiTreeNode<String>) node2); mSubtreesContain1.add((MultiTreeNode<String>) node9); assertTrue(messageContains, ((MultiTreeNode<String>) root).hasSubtrees(mSubtreesContain1)); Collection<MultiTreeNode<String>> mSubtreesContain2 = new ArrayList<>(3); mSubtreesContain2.add((MultiTreeNode<String>) node3); mSubtreesContain2.add((MultiTreeNode<String>) node7); mSubtreesContain2.add((MultiTreeNode<String>) node8); assertTrue(messageContains, ((MultiTreeNode<String>) node2).hasSubtrees(mSubtreesContain2)); List<MultiTreeNode<String>> mSubtreesContain3 = Collections.singletonList((MultiTreeNode<String>) node6); assertTrue(messageContains, ((MultiTreeNode<String>) node5).hasSubtrees(mSubtreesContain3)); String messageNotContains = "The current tree node was expected to contain all of the subtrees, but actually was not"; Collection<MultiTreeNode<String>> mSubtreesNotContain1 = new ArrayList<>(4); mSubtreesNotContain1.add((MultiTreeNode<String>) node3); mSubtreesNotContain1.add((MultiTreeNode<String>) node7); mSubtreesNotContain1.add((MultiTreeNode<String>) node8); mSubtreesNotContain1.add((MultiTreeNode<String>) node4); assertFalse(messageNotContains, ((MultiTreeNode<String>) root).hasSubtrees(mSubtreesNotContain1)); Collection<MultiTreeNode<String>> mSubtreesNotContain2 = new ArrayList<>(2); mSubtreesNotContain2.add((MultiTreeNode<String>) node5); mSubtreesNotContain2.add((MultiTreeNode<String>) anotherNode); assertFalse(messageNotContains, ((MultiTreeNode<String>) node3).hasSubtrees(mSubtreesNotContain2)); assertFalse(messageNotContains, ((MultiTreeNode<String>) node1).hasSubtrees(mSubtreesContain1)); assertFalse(messageNotContains, ((MultiTreeNode<String>) node2).hasSubtrees(null)); assertFalse(messageNotContains, ((MultiTreeNode<String>) node2).hasSubtrees(Collections.<MultiTreeNode<String>>emptyList())); assertFalse(messageNotContains, ((MultiTreeNode<String>) node2).hasSubtrees(Collections.<MultiTreeNode<String>>singletonList(null))); }