You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by le...@apache.org on 2003/12/09 16:59:31 UTC
cvs commit: avalon/framework/impl/src/java/org/apache/avalon/framework/configuration DefaultConfiguration.java
leosutic 2003/12/09 07:59:31
Modified: framework/impl/src/test/org/apache/avalon/framework/configuration/test
DefaultConfigurationTestCase.java
framework/impl/src/java/org/apache/avalon/framework/configuration
DefaultConfiguration.java
Log:
Added a copy constructor to the DefaultConfiguration class, enabling one to
easily create a writeable copy of a Configuration.
Added convenience methods for setting attributes and value to int, long,
float and boolean.
Revision Changes Path
1.9 +52 -0 avalon/framework/impl/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationTestCase.java
Index: DefaultConfigurationTestCase.java
===================================================================
RCS file: /home/cvs/avalon/framework/impl/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationTestCase.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DefaultConfigurationTestCase.java 22 Mar 2003 10:59:16 -0000 1.8
+++ DefaultConfigurationTestCase.java 9 Dec 2003 15:59:31 -0000 1.9
@@ -53,6 +53,7 @@
import junit.framework.TestCase;
import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationUtil;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
/**
@@ -150,6 +151,57 @@
m_configuration.removeChild( child );
assertEquals( null, m_configuration.getChild( childName, false ) );
+ }
+
+ public void testCopying() throws Exception
+ {
+ DefaultConfiguration root = new DefaultConfiguration( "root", "0:0", "http://root", "root" );
+ root.setAttribute( "attr1", "1" );
+ root.setAttribute( "attr2", "2" );
+
+ DefaultConfiguration child1 = new DefaultConfiguration( "child1", "0:1", "http://root/child1", "child1" );
+ DefaultConfiguration child2 = new DefaultConfiguration( "child2", "0:2", "http://root/child2", "child2" );
+
+ root.addChild( child1 );
+ root.addChild( child2 );
+
+ root.makeReadOnly();
+
+ DefaultConfiguration modifiableRoot = new DefaultConfiguration( root );
+ assertTrue( ConfigurationUtil.equals( root, modifiableRoot ) );
+
+ modifiableRoot.setAttribute( "attr1", "0" );
+
+ assertEquals( "0", modifiableRoot.getAttribute( "attr1" ) );
+
+ DefaultConfiguration modifiableChild1 = new DefaultConfiguration( root.getChild("child1") );
+ modifiableChild1.setValue( "1" );
+
+ modifiableRoot.removeChild( modifiableRoot.getChild("child1") );
+ modifiableRoot.addChild( modifiableChild1 );
+
+ assertEquals( "1", modifiableRoot.getChild( "child1" ).getValue() );
+ }
+
+ public void testConvenienceSetters() throws Exception
+ {
+ DefaultConfiguration config = new DefaultConfiguration( "root", "0:0", "http://root", "root" );
+ config.setAttribute( "integer", 12 );
+ config.setAttribute( "long", 8000000000L );
+ config.setAttribute( "float", 1.23f );
+ config.setAttribute( "boolean", true );
+ config.setAttribute( "string", "string" );
+
+ assertEquals( "12", config.getAttribute("integer") );
+ assertEquals( "8000000000", config.getAttribute("long") );
+ assertEquals( 1.23, config.getAttributeAsFloat("float"), 0.01 );
+ assertEquals( "string", config.getAttribute("string") );
+ assertEquals( "true", config.getAttribute("boolean") );
+
+ assertEquals( 12, config.getAttributeAsInteger("integer") );
+ assertEquals( 8000000000L, config.getAttributeAsLong("long") );
+ assertEquals( "string", config.getAttribute("string") );
+ assertEquals( true, config.getAttributeAsBoolean("boolean") );
}
}
1.37 +101 -2 avalon/framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java
Index: DefaultConfiguration.java
===================================================================
RCS file: /home/cvs/avalon/framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- DefaultConfiguration.java 29 Aug 2003 18:49:36 -0000 1.36
+++ DefaultConfiguration.java 9 Dec 2003 15:59:31 -0000 1.37
@@ -83,6 +83,21 @@
private boolean m_readOnly;
/**
+ * Shallow copy constructor, suitable for craeting a writable clone of
+ * a read-only configuration. To modify children, use <code>getChild()</code>,
+ * <code>removeChild()</code> and <code>addChild()</code>.
+ *
+ * @param config the <code>Configuration</code> to copy
+ * @throws ConfigurationException if an error occurs when copying
+ */
+ public DefaultConfiguration( Configuration config ) throws ConfigurationException
+ {
+ this( config.getName(), config.getLocation(), config.getNamespace(),
+ ( (config instanceof AbstractConfiguration) ? ((AbstractConfiguration)config).getPrefix() : "") );
+ addAll( config );
+ }
+
+ /**
* Create a new <code>DefaultConfiguration</code> instance.
* @param name a <code>String</code> value
*/
@@ -111,7 +126,7 @@
* elements with a longer namespace string. Should not be null; use "" if no
* namespace.
* @since 4.1
- */
+ */
public DefaultConfiguration( final String name,
final String location,
final String ns,
@@ -379,8 +394,48 @@
m_value = value;
}
+
+ /**
+ * Set the value of this <code>Configuration</code> object to the specified int.
+ *
+ * @param value a <code>int</code> value
+ */
+ public void setValue( final int value )
+ {
+ setValue( String.valueOf( value ) );
+ }
+
+ /**
+ * Set the value of this <code>Configuration</code> object to the specified long.
+ *
+ * @param value a <code>long</code> value
+ */
+ public void setValue( final long value )
+ {
+ setValue( String.valueOf( value ) );
+ }
/**
+ * Set the value of this <code>Configuration</code> object to the specified boolean.
+ *
+ * @param value a <code>boolean</code> value
+ */
+ public void setValue( final boolean value )
+ {
+ setValue( String.valueOf( value ) );
+ }
+
+ /**
+ * Set the value of this <code>Configuration</code> object to the specified float.
+ *
+ * @param value a <code>float</code> value
+ */
+ public void setValue( final float value )
+ {
+ setValue( String.valueOf( value ) );
+ }
+
+ /**
* Set the value of the specified attribute to the specified string.
*
* @param name name of the attribute to set
@@ -395,6 +450,50 @@
m_attributes = new HashMap();
}
m_attributes.put( name, value );
+ }
+
+ /**
+ * Set the value of the specified attribute to the specified int.
+ *
+ * @param name name of the attribute to set
+ * @param value an <code>int</code> value
+ */
+ public void setAttribute( final String name, final int value )
+ {
+ setAttribute( name, String.valueOf( value ) );
+ }
+
+ /**
+ * Set the value of the specified attribute to the specified long.
+ *
+ * @param name name of the attribute to set
+ * @param value an <code>long</code> value
+ */
+ public void setAttribute( final String name, final long value )
+ {
+ setAttribute( name, String.valueOf( value ) );
+ }
+
+ /**
+ * Set the value of the specified attribute to the specified boolean.
+ *
+ * @param name name of the attribute to set
+ * @param value an <code>boolean</code> value
+ */
+ public void setAttribute( final String name, final boolean value )
+ {
+ setAttribute( name, String.valueOf( value ) );
+ }
+
+ /**
+ * Set the value of the specified attribute to the specified float.
+ *
+ * @param name name of the attribute to set
+ * @param value an <code>float</code> value
+ */
+ public void setAttribute( final String name, final float value )
+ {
+ setAttribute( name, String.valueOf( value ) );
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org