/** * 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 boolean events. */ @Test public void testBooleanEvent() { ConfigurationEvent event; // Makes sure the value passed to the constructor is properly returned. event = new ConfigurationEvent(conf, VARIABLE_NAME, Boolean.toString(BOOLEAN_VALUE)); assert BOOLEAN_VALUE == event.getBooleanValue(); // Makes sure unset values are returned as 0. event = new ConfigurationEvent(conf, VARIABLE_NAME, null); assert false == event.getBooleanValue(); }
/** * Tests integer events. */ @Test public void testIntegerEvent() { ConfigurationEvent event; // Makes sure the value passed to the constructor is properly returned. event = new ConfigurationEvent(conf, VARIABLE_NAME, Integer.toString(INTEGER_VALUE)); assert INTEGER_VALUE == event.getIntegerValue(); // Makes sure unset values are returned as 0. event = new ConfigurationEvent(conf, VARIABLE_NAME, null); assert 0 == event.getIntegerValue(); }
/** * Tests float events. */ @Test public void testFloatEvent() { ConfigurationEvent event; // Makes sure the value passed to the constructor is properly returned. event = new ConfigurationEvent(conf, VARIABLE_NAME, Float.toString(FLOAT_VALUE)); assert FLOAT_VALUE == event.getFloatValue(); // Makes sure unset values are returned as 0. event = new ConfigurationEvent(conf, VARIABLE_NAME, null); assert 0 == event.getFloatValue(); }
/** * Tests string events. */ @Test public void testStringEvents() { ConfigurationEvent event; // Makes sure the value passed to the constructor is properly returned. event = new ConfigurationEvent(conf, VARIABLE_NAME, STRING_VALUE); assert STRING_VALUE.equals(event.getValue()); // Makes sure unset values are returned as null. event = new ConfigurationEvent(conf, VARIABLE_NAME, null); assert null == event.getValue(); }
/** * Tests long events. */ @Test public void testLongEvent() { ConfigurationEvent event; // Makes sure the value passed to the constructor is properly returned. event = new ConfigurationEvent(conf, VARIABLE_NAME, Long.toString(LONG_VALUE)); assert LONG_VALUE == event.getLongValue(); // Makes sure unset values are returned as 0. event = new ConfigurationEvent(conf, VARIABLE_NAME, null); assert 0 == event.getLongValue(); }
/** * Tests double events. */ @Test public void testDoubleEvent() { ConfigurationEvent event; // Makes sure the value passed to the constructor is properly returned. event = new ConfigurationEvent(conf, VARIABLE_NAME, Double.toString(DOUBLE_VALUE)); assert DOUBLE_VALUE == event.getDoubleValue(); // Makes sure unset values are returned as 0. event = new ConfigurationEvent(conf, VARIABLE_NAME, null); assert 0 == event.getDoubleValue(); }
/** * Tests event creation. */ @Test public void testConstructor() { ConfigurationEvent event; // Makes sure the constructor initialises events properly. event = new ConfigurationEvent(conf, VARIABLE_NAME, STRING_VALUE); assert conf.equals(event.getConfiguration()); assert VARIABLE_NAME.equals(event.getVariable()); } }
/** * Tests list events. */ @Test public void testListEvents() { ConfigurationEvent event; // Makes sure the value passed to the constructor is properly returned. event = new ConfigurationEvent(conf, VARIABLE_NAME, ValueList.toString(LIST_VALUE, ";")); assert LIST_VALUE.equals(event.getListValue(";")); // Makes sure unset values are returned as null. event = new ConfigurationEvent(conf, VARIABLE_NAME, null); assert null == event.getListValue(";"); }
/** * 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; }
/** * 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; }
/** * 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; }