/** * Utility method for {@link #testVariableNames()}. * @param section section to explore. * @param count number of expected variable names. */ private void assertVariableNames(ConfigurationSection section, int count) { // No expected variables, make sure that the section reflects that. if(count == 0) { assert !section.hasVariables(); assert section.isEmpty(); assert !section.variableNames().hasNext(); } // Makes sure that the section contains exactly var1, var2..., var<count>. // We have to go through a set here: the order in which we'll iterate over the variable names is unreliable. else { Iterator<String> names; Set<String> expectedNames; assert section.hasVariables(); assert !section.isEmpty(); // Populates a set will all the expected variable names. expectedNames = new HashSet<String>(count); for(int i = 0; i < count; i++) expectedNames.add("var" + i); // Makes sure that we can remove all of the section's variables, and that none remains afterward. names = section.variableNames(); while(names.hasNext()) assert expectedNames.remove(names.next()); assert expectedNames.isEmpty(); } }
/** * Utility method for {@link #testSectionNames()}. * @param section section to explore. * @param count number of expected section names. */ private void assertSectionNames(ConfigurationSection section, int count) { // No expected variables, make sure that the section reflects that. if(count == 0) { assert !section.hasSections(); assert section.isEmpty(); assert !section.sectionNames().hasNext(); } // Makes sure that the section contains exactly sect1, sect2..., sect<count>. // We have to go through a set here: the order in which we'll iterate over the variable names is unreliable. else { Iterator<String> names; Set<String> expectedNames; assert section.hasSections(); assert !section.isEmpty(); // Populates a set will all the expected variable names. expectedNames = new HashSet<String>(count); for(int i = 0; i < count; i++) expectedNames.add("sect" + i); // Makes sure that we can remove all of the section's sub-sections, and that none remains afterward. names = section.sectionNames(); while(names.hasNext()) assert expectedNames.remove(names.next()); assert expectedNames.isEmpty(); } }
/** * 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; } }