/** * Creates a subsection wit the specified name in the section. * <p> * If a subsection with the specified name already exists, it will be returned. * </p> * @param name name of the new section. * @return the subsection with the specified name. */ public ConfigurationSection addSection(String name) { ConfigurationSection section; // The section already exists, returns it. if((section = getSection(name)) != null) return section; // Creates the new section. sections.put(name, section = new ConfigurationSection()); return section; }
/** * Move to the specified section. * @param name name of the current section's subsection in which to move. * @param create if <code>true</code> and <code>name</code> doesn't exist, it will be created. * @return <code>true</code> if we could move to <code>name</code>, <code>false</code> otherwise. */ public boolean moveTo(String name, boolean create) { ConfigurationSection buffer; // Buffer for the subsection. // Checks whether the requested subsection exists. if((buffer = section.getSection(name)) == null) { // If it doesn't exist, either return false or create it depending on // parameters. if(create) { section = section.addSection(name); return true; } return false; } section = buffer; return true; } }
/** * Retrieves the value of the specified variable as a boolean. * <p> * If the variable isn't set, this method will set it to <code>defaultValue</code> before * returning it. If this happens, a configuration {@link ConfigurationEvent event} will * be sent to all registered LISTENERS. * </p> * @param name name of the variable to retrieve. * @param defaultValue value to use if <code>name</code> is not set. * @return the specified variable's value. * @see #setVariable(String,boolean) * @see #getBooleanVariable(String) */ public boolean getVariable(String name, boolean defaultValue) { return ConfigurationSection.getBooleanValue(getVariable(name, ConfigurationSection.getValue(defaultValue))); }
/** * Recursively explores the specified section and invokes the specified builder's callback methods. * @param builder object that will receive building events. * @param root section to explore. * @throws ConfigurationException if any error occurs. */ private synchronized void build(ConfigurationBuilder builder, ConfigurationSection root) throws ConfigurationException { Iterator<String> enumeration; // Enumeration on the section's variables, then subsections. String name; // Name of the current variable, then section. ConfigurationSection section; // Current section. // Explores the section's variables. enumeration = root.variableNames(); while(enumeration.hasNext()) builder.addVariable(name = enumeration.next(), root.getVariable(name)); // Explores the section's subsections. enumeration = root.sectionNames(); while(enumeration.hasNext()) { name = enumeration.next(); section = root.getSection(name); // We only go through subsections if contain either variables or subsections of their own. if(section.hasSections() || section.hasVariables()) { builder.startSection(name); build(builder, section); builder.endSection(name); } } }
/** * 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); }
/** * Tests the {@link ConfigurationSection#removeVariable(String)} method. * @param value value to which the variable should be set before being removed. */ @Test(dataProvider = "setVariable") public void testRemoveVariable(String value) { ConfigurationSection section; section = new ConfigurationSection(); assert section.setVariable("var", value); assertVariable(section, "var", value); assert value == null ? section.removeVariable("var") == null : section.removeVariable("var").equals(value); }
/** * 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); } }
/** * Retrieves the value of the specified variable as a {@link ValueList}. * <p> * If the variable isn't set, this method will set it to <code>defaultValue</code> before * returning it. If this happens, a configuration {@link ConfigurationEvent event} will * be sent to all registered LISTENERS. * </p> * @param name name of the variable to retrieve. * @param defaultValue value to use if variable <code>name</code> is not set. * @param separator separator to use for <code>defaultValue</code> if variable <code>name</code> is not set. * @return the specified variable's value. * @see #setVariable(String,List,String) * @see #getListVariable(String,String) */ public ValueList getVariable(String name, List<String> defaultValue, String separator) { return ConfigurationSection.getListValue(getVariable(name, ConfigurationSection.getValue(defaultValue, separator)), separator); }
/** * Retrieves the value of the specified variable as a double. * <p> * If the variable isn't set, this method will set it to <code>defaultValue</code> before * returning it. If this happens, a configuration {@link ConfigurationEvent event} will * be sent to all registered LISTENERS. * </p> * @param name name of the variable to retrieve. * @param defaultValue value to use if <code>name</code> is not set. * @return the specified variable's value. * @throws NumberFormatException if the variable's value cannot be cast to a double. * @see #setVariable(String,double) * @see #getDoubleVariable(String) */ public double getVariable(String name, double defaultValue) { return ConfigurationSection.getDoubleValue(getVariable(name, ConfigurationSection.getValue(defaultValue))); }
/** * Retrieves the value of the specified variable as a long. * <p> * If the variable isn't set, this method will set it to <code>defaultValue</code> before * returning it. If this happens, a configuration {@link ConfigurationEvent event} will * be sent to all registered LISTENERS. * </p> * @param name name of the variable to retrieve. * @param defaultValue value to use if <code>name</code> is not set. * @return the specified variable's value. * @throws NumberFormatException if the variable's value cannot be cast to a long. * @see #setVariable(String,long) * @see #getLongVariable(String) */ public long getVariable(String name, long defaultValue) { return ConfigurationSection.getLongValue(getVariable(name, ConfigurationSection.getValue(defaultValue))); }
/** * Retrieves the value of the specified variable as an integer. * <p> * If the variable isn't set, this method will set it to <code>defaultValue</code> before * returning it. If this happens, a configuration {@link ConfigurationEvent event} will * be sent to all registered LISTENERS. * </p> * @param name name of the variable to retrieve. * @param defaultValue value to use if <code>name</code> is not set. * @return the specified variable's value. * @throws NumberFormatException if the variable's value cannot be cast to an integer. * @see #setVariable(String,int) * @see #getIntegerVariable(String) */ public int getVariable(String name, int defaultValue) { return ConfigurationSection.getIntegerValue(getVariable(name, ConfigurationSection.getValue(defaultValue))); }
/** * Retrieves the value of the specified variable as a float. * <p> * If the variable isn't set, this method will set it to <code>defaultValue</code> before * returning it. If this happens, a configuration {@link ConfigurationEvent event} will * be sent to all registered LISTENERS. * </p> * @param name name of the variable to retrieve. * @param defaultValue value to use if <code>name</code> is not set. * @return the specified variable's value. * @throws NumberFormatException if the variable's value cannot be cast to a float. * @see #setVariable(String,float) * @see #getFloatVariable(String) */ public float getVariable(String name, float defaultValue) { return ConfigurationSection.getFloatValue(getVariable(name, ConfigurationSection.getValue(defaultValue))); }
/** * Sets the value of the specified variable. * <p> * This method will return <code>false</code> if it didn't modify <code>name</code>'s value. This, however, is not a * way of indicating that the call failed: <code>false</code> is only ever returned if the previous value is equal * to the new value. * </p> * <p> * If the value of the specified variable is actually modified, an {@link ConfigurationEvent event} will be passed * to all LISTENERS. * </p> * @param name fully qualified name of the variable to set. * @param value new value for the variable. * @return <code>true</code> if this call resulted in a modification of the variable's value, * <code>false</code> otherwise. * @see #getDoubleVariable(String) * @see #getVariable(String,double) */ public boolean setVariable(String name, double value) {return setVariable(name, ConfigurationSection.getValue(value));}
/** * Tests the {@link ConfigurationSection#setVariable(String, String)} method. * @param first first value to which the test variable should be set. * @param second second value to which the test variable should be set. * @param expected expected return value of the second call to * {@link ConfigurationSection#setVariable(String, String)}. */ @Test(dataProvider = "setVariable") public void testSetVariable(String first, String second, boolean expected) { ConfigurationSection section; section = new ConfigurationSection(); assert section.setVariable("var", first); assertVariable(section, "var", first); assert expected == section.setVariable("var", second); assertVariable(section, "var", second); }
/** * Returns the new value for the modified variable as a long. * <p> * If the variable has been deleted, this method will return 0. * </p> * @return the new value for the modified variable. * @throws NumberFormatException if {@link #getValue()} cannot be cast as a long. */ public long getLongValue() throws NumberFormatException { return ConfigurationSection.getLongValue(value); }
/** * Returns the new value for the modified variable cast as a boolean. * <p> * If the variable has been deleted, this method will return <code>false</code>. * </p> * @return the new value for the modified variable. */ public boolean getBooleanValue() { return ConfigurationSection.getBooleanValue(value); }
/** * Returns the new value for the modified variable cast as an integer. * <p> * If the variable has been deleted, this method will return 0. * </p> * @return the new value for the modified variable. * @throws NumberFormatException if {@link #getValue()} cannot be cast as an integer. */ public int getIntegerValue() throws NumberFormatException { return ConfigurationSection.getIntegerValue(value); }
/** * Returns the new value for the modified variable cast as a {@link ValueList}. * <p> * If the variable has been deleted, this method will return null. * </p> * @param separator string used to tokenise the variable's value. * @return the new value for the modified variable. */ public ValueList getListValue(String separator) { return ConfigurationSection.getListValue(value, separator); } }
/** * Returns the new value for the modified variable cast as a double. * <p> * If the variable has been deleted, this method will return 0. * </p> * @return the new value for the modified variable. * @throws NumberFormatException if {@link #getValue()} cannot be cast as a double. */ public double getDoubleValue() { return ConfigurationSection.getDoubleValue(value); }