/** * Adds the specified variable to the current section. * @param name name of the variable. * @param value value of the variable. */ public void addVariable(String name, String value) { // If the variable's value was modified, trigger an event. if(currentSection.setVariable(name, value)) { if(sectionNames.empty()) triggerEvent(new ConfigurationEvent(Configuration.this, name, value)); else triggerEvent(new ConfigurationEvent(Configuration.this, sectionNames.peek() + name, 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); }
/** * 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); }
/** * Tests the {@link ConfigurationSection#variableNames()} method. */ @Test public void testVariableNames() { ConfigurationSection section; section = new ConfigurationSection(); // Create 10 variables. for(int i = 0; i < 10; i++) section.setVariable("var" + i, "value"); assertVariableNames(section, 10); section.setVariable("var9", ""); assertVariableNames(section, 9); section.setVariable("var8", null); assertVariableNames(section, 8); section.removeVariable("var7"); assertVariableNames(section, 7); for(int i = 0; i < 7; i++) section.removeVariable("var" + i); assertVariableNames(section, 0); }
/** * 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. Note that this doesn't * mean the call failed, but that <code>name</code>'s value was already equal to <code>value</code>. * </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 #getVariable(String) * @see #getVariable(String,String) */ public synchronized boolean setVariable(String name, String value) { ConfigurationExplorer explorer; // Used to navigate to the variable's parent section. String buffer; // Buffer for the variable's name trimmed of section information. // Moves to the parent section. buffer = moveToParent(explorer = new ConfigurationExplorer(root), name, true); // If the variable's value was actually modified, triggers an event. if(explorer.getSection().setVariable(buffer, value)) { triggerEvent(new ConfigurationEvent(this, name, value)); return true; } return false; }
/** * Retrieves the value of the specified variable. * <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,String) * @see #getVariable(String) */ public synchronized String getVariable(String name, String defaultValue) { ConfigurationExplorer explorer; // Used to navigate to the variable's parent section. String value; // Buffer for the variable's value. String buffer; // Buffer for the variable's name trimmed of section information. // Navigates to the parent section. We do not have to check for null values here, // as the section will be created if it doesn't exist. buffer = moveToParent(explorer = new ConfigurationExplorer(root), name, true); // If the variable isn't set, set it to defaultValue and triggers an event. if((value = explorer.getSection().getVariable(buffer)) == null) { explorer.getSection().setVariable(buffer, defaultValue); triggerEvent(new ConfigurationEvent(this, name, defaultValue)); return defaultValue; } return value; }