/** * Deletes the specified section. * <p> * Note that this method is very inefficient and should only be called when strictly necessary. * </p> * @param section section to remove. * @return <code>true</code> if the specified section was removed, <code>false</code> if it didn't exist. */ public boolean removeSection(ConfigurationSection section) { String name; Iterator<String> sectionNames; sectionNames = sectionNames(); // Goes through each key / value pair and checks whether we've found the sectioon // we were looking for. while(sectionNames.hasNext()) { name = sectionNames.next(); // If we have, remove it and break. if(getSection(name).equals(section)) { removeSection(name); return true; } } return false; }
@Test public void testSectionNames() { ConfigurationSection section; ConfigurationSection buffer; section = new ConfigurationSection(); // Create 10 variables. buffer = null; for(int i = 0; i < 10; i++) buffer = section.addSection("sect" + i); assertSectionNames(section, 10); // Removes the last section by value, makes sure we're in a coherent state. assert section.removeSection(buffer); assertSectionNames(section, 9); // Removes the last section by name, makes sure we're in a coherent state. assert section.removeSection("sect8") != null; assertSectionNames(section, 8); for(int i = 0; i < 8; i++) assert section.removeSection("sect" + i) != null; assertSectionNames(section, 0); } }
/** * Prunes dead branches from the specified configuration tree. * @param explorer used to backtrack through the configuration tree. */ private void prune(BufferedConfigurationExplorer explorer) { ConfigurationSection current; ConfigurationSection parent; // If we're at the root level, nothing to prune. if(!explorer.hasSections()) return; current = explorer.popSection(); // Look for branches to prune until we've either found a non-empty one // or reached the root of the three. while(current.isEmpty() && current != root) { // Gets the current section's parent and prune. parent = explorer.hasSections() ? explorer.popSection() : root; parent.removeSection(current); current = parent; } }
/** * Tests various section related method. */ @Test public void testSections() { ConfigurationSection section; ConfigurationSection buffer; section = new ConfigurationSection(); // Makes sure we can add a section. buffer = section.addSection("sect"); assert buffer != null; assert section.hasSections(); // Makes sure that adding the same section twice yields the same instance. assert section.addSection("sect") == buffer; assert section.hasSections(); // Makes sure we can remove a section by name. assert section.removeSection("sect") == buffer; assert section.getSection("sect") == null; assert !section.hasSections(); // Makes sure we can remove a section by value. buffer = section.addSection("sect"); assert section.removeSection(buffer); assert buffer.getSection("sect") == null; assert !section.hasSections(); assert !section.removeSection(buffer); }