Return-Path: Delivered-To: apmail-avalon-cvs-archive@www.apache.org Received: (qmail 26778 invoked from network); 9 Dec 2003 15:59:34 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 9 Dec 2003 15:59:34 -0000 Received: (qmail 38964 invoked by uid 500); 9 Dec 2003 15:59:27 -0000 Delivered-To: apmail-avalon-cvs-archive@avalon.apache.org Received: (qmail 38893 invoked by uid 500); 9 Dec 2003 15:59:26 -0000 Mailing-List: contact cvs-help@avalon.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list cvs@avalon.apache.org Received: (qmail 38880 invoked from network); 9 Dec 2003 15:59:26 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 9 Dec 2003 15:59:26 -0000 Received: (qmail 26657 invoked by uid 1449); 9 Dec 2003 15:59:31 -0000 Date: 9 Dec 2003 15:59:31 -0000 Message-ID: <20031209155931.26656.qmail@minotaur.apache.org> From: leosutic@apache.org To: avalon-cvs@apache.org Subject: cvs commit: avalon/framework/impl/src/java/org/apache/avalon/framework/configuration DefaultConfiguration.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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 getChild(), + * removeChild() and addChild(). + * + * @param config the Configuration 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 DefaultConfiguration instance. * @param name a String 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 Configuration object to the specified int. + * + * @param value a int value + */ + public void setValue( final int value ) + { + setValue( String.valueOf( value ) ); + } + + /** + * Set the value of this Configuration object to the specified long. + * + * @param value a long value + */ + public void setValue( final long value ) + { + setValue( String.valueOf( value ) ); + } /** + * Set the value of this Configuration object to the specified boolean. + * + * @param value a boolean value + */ + public void setValue( final boolean value ) + { + setValue( String.valueOf( value ) ); + } + + /** + * Set the value of this Configuration object to the specified float. + * + * @param value a float 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 int 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 long 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 boolean 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 float 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