public MutableTree getChild(final List<String> path) throws IllegalArgumentException { Preconditions.checkArgument(!path.isEmpty()); String directChildName = path.get(0); MutableTree child = childTrees.get(directChildName); if (child == null) { throw new IllegalArgumentException(String.format("No child named %s exists: %s", directChildName, childTrees.keySet())); } if (path.size() == 1) { return child; } return child.getChild(path.subList(1, path.size())); }
public MutableTree getChild(final List<String> path) throws IllegalArgumentException { Preconditions.checkArgument(!path.isEmpty()); String directChildName = path.get(0); MutableTree child = childTrees.get(directChildName); if (child == null) { throw new IllegalArgumentException(String.format("No child named %s exists: %s", directChildName, childTrees.keySet())); } if (path.size() == 1) { return child; } return child.getChild(path.subList(1, path.size())); }
public MutableTree getChild(String path) throws IllegalArgumentException { return getChild(NodeRef.split(path)); }
public MutableTree getChild(String path) throws IllegalArgumentException { return getChild(NodeRef.split(path)); }
public void setChild(final List<String> parentPath, final Node node) { MutableTree parent; MutableTree child; if (parentPath.isEmpty()) { parent = this; } else { parent = getChild(parentPath); } child = parent.childTrees.get(node.getName()); if (child == null) { child = new MutableTree(node); parent.childTrees.put(node.getName(), child); } else { child.setNode(node); } }
public void setChild(final List<String> parentPath, final Node node) { MutableTree parent; MutableTree child; if (parentPath.isEmpty()) { parent = this; } else { parent = getChild(parentPath); } child = parent.childTrees.get(node.getName()); if (child == null) { child = new MutableTree(node); parent.childTrees.put(node.getName(), child); } else { child.setNode(node); } }
@Test public void testGetChild() { assertNode(root.getChild("roads"), id("a1"), null, "roads"); assertNode(root.getChild("roads/highways"), id("a2"), id("d1"), "highways"); assertNode(root.getChild("roads/streets"), id("a3"), id("d2"), "streets"); assertNode(root.getChild("roads").getChild("highways"), id("a2"), id("d1"), "highways"); assertNode(root.getChild("buildings"), id("a4"), null, "buildings"); assertNode(root.getChild("buildings/stores"), id("a5"), id("d3"), "stores"); assertNode(root.getChild("buildings/unknown"), id("a6"), id("d4"), "unknown"); assertNode(root.getChild("buildings").getChild("unknown"), id("a6"), id("d4"), "unknown"); }
@Test public void testRemoveNested() { assertNotNull(root.getChild("roads/highways")); assertNotNull(root.getChild("roads/streets")); assertNull(root.removeChild("nonExistent")); assertNotNull(root.removeChild("roads/streets")); assertNotNull(root.getChild("roads")); assertNotNull(root.getChild("roads/highways")); ex.expect(IllegalArgumentException.class); ex.expectMessage("No child named streets exists"); root.getChild("roads/streets"); }
@Test public void testRemoveNested() { assertNotNull(root.getChild("roads/highways")); assertNotNull(root.getChild("roads/streets")); assertNull(root.removeChild("nonExistent")); assertNotNull(root.removeChild("roads/streets")); assertNotNull(root.getChild("roads")); assertNotNull(root.getChild("roads/highways")); ex.expect(IllegalArgumentException.class); ex.expectMessage("No child named streets exists"); root.getChild("roads/streets"); }
@Test public void testGetChild() { assertNode(root.getChild("roads"), id("a1"), null, "roads"); assertNode(root.getChild("roads/highways"), id("a2"), id("d1"), "highways"); assertNode(root.getChild("roads/streets"), id("a3"), id("d2"), "streets"); assertNode(root.getChild("roads").getChild("highways"), id("a2"), id("d1"), "highways"); assertNode(root.getChild("buildings"), id("a4"), null, "buildings"); assertNode(root.getChild("buildings/stores"), id("a5"), id("d3"), "stores"); assertNode(root.getChild("buildings/unknown"), id("a6"), id("d4"), "unknown"); assertNode(root.getChild("buildings").getChild("unknown"), id("a6"), id("d4"), "unknown"); }
@Test public void testGetChildPreconditions() { try { assertNode(root.getChild(""), NULL, null, ROOT); fail("expected IAE on empty path"); } catch (IllegalArgumentException e) { assertTrue(true); } try { assertNull(root.getChild("notAChild")); fail("expected IAE on invalid child name"); } catch (IllegalArgumentException e) { assertTrue(true); } try { assertNull(root.getChild("not/a/child")); fail("expected IAE on invalid child name"); } catch (IllegalArgumentException e) { assertTrue(true); } }
@Test public void testGetChildPreconditions() { try { assertNode(root.getChild(""), NULL, null, ROOT); fail("expected IAE on empty path"); } catch (IllegalArgumentException e) { assertTrue(true); } try { assertNull(root.getChild("notAChild")); fail("expected IAE on invalid child name"); } catch (IllegalArgumentException e) { assertTrue(true); } try { assertNull(root.getChild("not/a/child")); fail("expected IAE on invalid child name"); } catch (IllegalArgumentException e) { assertTrue(true); } }
@Test public void testSet() { Node node = treeNode("roads", id("a11"), id("d1")); root.setChild("", node); assertEquals(node, root.getChild("roads").getNode()); node = treeNode("stores", id("a51"), id("d3")); root.setChild("buildings", node); assertEquals(node, root.getChild("buildings/stores").getNode()); }
@Test public void testSet() { Node node = treeNode("roads", id("a11"), id("d1")); root.setChild("", node); assertEquals(node, root.getChild("roads").getNode()); node = treeNode("stores", id("a51"), id("d3")); root.setChild("buildings", node); assertEquals(node, root.getChild("buildings/stores").getNode()); }