/** * Sets the specified variable to the specified value. * <p> * If <code>value</code> is either <code>null</code> or an empty string, * the call will be equivalent to {@link #removeVariable(String)}. * </p> * @param name name of the variable to set. * @param value value for the variable. * @return <code>true</code> if the variable's value was changed as a result of this call, <code>false</code> * otherwise. */ public boolean setVariable(String name, String value) { // If the specified value is empty, deletes the variable. if(value == null || value.trim().equals("")) { // If the variable wasn't set, we haven't changed its value. if(getVariable(name) == null) return false; // Otherwise, deletes it and returns true. removeVariable(name); return true; } // Compares the variable's new and old values. String buffer; buffer = variables.put(name, value); return buffer == null || !buffer.equals(value); }
/** * 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 variable from the configuration. * <p> * If the variable was set, a configuration {@link ConfigurationEvent event} will be passed to * all registered LISTENERS. * </p> * @param name name of the variable to remove. * @return the variable's old value, or <code>null</code> if it wasn't set. */ public synchronized String removeVariable(String name) { BufferedConfigurationExplorer explorer; // Used to navigate to the variable's parent section. String buffer; // Buffer for the variable's name trimmed of section information. // If the variable's 'path' doesn't exist, return null. if((buffer = moveToParent(explorer = new BufferedConfigurationExplorer(root), name, false)) == null) return null; // If the variable was actually set, triggers an event. if((buffer = explorer.getSection().removeVariable(buffer)) != null) { prune(explorer); triggerEvent(new ConfigurationEvent(this, name, null)); } return buffer; }
/** * 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); }