Tree extends HashMap<T, Tree<T>> implements Serializable { public List<T> getObjectsAtDepth(final int depth) { final List<T> list = new ArrayList<T>(); for (final Tree<T> t : this.getTreesAtDepth(depth)) { list.addAll(t.keySet()); } return list; } Tree(); @SafeVarargs  Tree(final T... children); @SafeVarargs  Tree(final Map.Entry<T, Tree<T>>... children); List<Tree<T>> getTreesAtDepth(final int depth); List<T> getObjectsAtDepth(final int depth); List<Tree<T>> getLeafTrees(); List<T> getLeafObjects(); boolean isLeaf(); void addTree(final Tree<T> tree); List<Tree<T>> splitParents();  }
@Test public void shouldProvideValidDepths() { Tree<String> tree = new Tree<String>(); tree.put("marko", new Tree<String>(TreeTest.createTree("a", new Tree<String>("a1", "a2")), TreeTest.createTree("b", new Tree<String>("b1", "b2", "b3")))); tree.put("josh", new Tree<String>("1", "2")); assertEquals(0, tree.getObjectsAtDepth(0).size()); assertEquals(2, tree.getObjectsAtDepth(1).size()); assertEquals(4, tree.getObjectsAtDepth(2).size()); assertEquals(5, tree.getObjectsAtDepth(3).size()); assertEquals(0, tree.getObjectsAtDepth(4).size()); assertEquals(0, tree.getObjectsAtDepth(5).size()); assertEquals(2, tree.get("josh").size()); assertEquals(0, tree.get("marko").get("b").get("b1").size()); assertEquals(3, tree.get("marko").get("b").size()); assertNull(tree.get("marko").get("c")); }