You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by le...@apache.org on 2005/03/08 15:51:43 UTC

svn commit: r156533 - in excalibur/trunk/framework: api/src/java/org/apache/avalon/framework/configuration/ impl/src/java/org/apache/avalon/framework/configuration/ impl/src/test/org/apache/avalon/framework/configuration/test/

Author: leif
Date: Tue Mar  8 06:51:40 2005
New Revision: 156533

URL: http://svn.apache.org/viewcvs?view=rev&rev=156533
Log:
Add support for double values in Configurations.  Float was supported, but sometimes more precision is required.  Long was already implemented for integers.

Modified:
    excalibur/trunk/framework/api/src/java/org/apache/avalon/framework/configuration/Configuration.java
    excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java
    excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java
    excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/MutableConfiguration.java
    excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationTestCase.java

Modified: excalibur/trunk/framework/api/src/java/org/apache/avalon/framework/configuration/Configuration.java
URL: http://svn.apache.org/viewcvs/excalibur/trunk/framework/api/src/java/org/apache/avalon/framework/configuration/Configuration.java?view=diff&r1=156532&r2=156533
==============================================================================
--- excalibur/trunk/framework/api/src/java/org/apache/avalon/framework/configuration/Configuration.java (original)
+++ excalibur/trunk/framework/api/src/java/org/apache/avalon/framework/configuration/Configuration.java Tue Mar  8 06:51:40 2005
@@ -285,6 +285,17 @@
     float getAttributeAsFloat( String paramName ) throws ConfigurationException;
 
     /**
+     * Return the <code>double</code> value of the specified parameter contained
+     * in this node.
+     *
+     * @param paramName The name of the parameter you ask the value of.
+     * @return double value of attribute
+     * @throws ConfigurationException If no parameter with that name exists.
+     *                                   or if conversion to <code>double</code> fails.
+     */
+    double getAttributeAsDouble( String paramName ) throws ConfigurationException;
+
+    /**
      * Return the <code>boolean</code> value of the specified parameter contained
      * in this node.
      *
@@ -321,6 +332,14 @@
     float getValueAsFloat() throws ConfigurationException;
 
     /**
+     * Return the <code>double</code> value of the node.
+     *
+     * @return the value of the node.
+     * @throws ConfigurationException If conversion to <code>double</code> fails.
+     */
+    double getValueAsDouble() throws ConfigurationException;
+
+    /**
      * Return the <code>boolean</code> value of the node.
      *
      * @return the value of the node.
@@ -381,6 +400,17 @@
     float getValueAsFloat( float defaultValue );
 
     /**
+     * Returns the value of the configuration element as a <code>double</code>.
+     * If the configuration value is not set, the default value will be
+     * used.
+     *
+     * @param defaultValue The default value desired.
+     * @return float value of the <code>Configuration</code>, or default
+     *          if none specified.
+     */
+    double getValueAsDouble( double defaultValue );
+
+    /**
      * Returns the value of the configuration element as a <code>boolean</code>.
      * If the configuration value is not set, the default value will be
      * used.
@@ -442,6 +472,19 @@
      *          the value is not set.
      */
     float getAttributeAsFloat( String name, float defaultValue );
+
+    /**
+     * Returns the value of the attribute specified by its name as a
+     * <code>double</code>, or the default value if no attribute by
+     * that name exists or is empty.
+     *
+     * @param name The name of the attribute you ask the value of.
+     * @param defaultValue The default value desired.
+     * @return float value of attribute. It will return the default
+     *          value if the named attribute does not exist, or if
+     *          the value is not set.
+     */
+    double getAttributeAsDouble( String name, double defaultValue );
 
     /**
      * Returns the value of the attribute specified by its name as a

Modified: excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java
URL: http://svn.apache.org/viewcvs/excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java?view=diff&r1=156532&r2=156533
==============================================================================
--- excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java (original)
+++ excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/AbstractConfiguration.java Tue Mar  8 06:51:40 2005
@@ -205,6 +205,48 @@
     }
 
     /**
+     * Returns the value of the configuration element as a <code>double</code>.
+     *
+     * @throws ConfigurationException if an error occurs
+     * @return the value
+     */
+    public double getValueAsDouble()
+        throws ConfigurationException
+    {
+        final String value = getValue().trim();
+        try
+        {
+            return Double.parseDouble( value );
+        }
+        catch( final Exception nfe )
+        {
+            final String message =
+                "Cannot parse the value \"" + value
+                + "\" as a double in the configuration element \""
+                + getName() + "\" at " + getLocation();
+            throw new ConfigurationException( message );
+        }
+    }
+
+    /**
+     * Returns the value of the configuration element as a <code>double</code>.
+     *
+     * @param defaultValue the default value to return if value malformed or empty
+     * @return the value
+     */
+    public double getValueAsDouble( final double defaultValue )
+    {
+        try
+        {
+            return getValueAsDouble();
+        }
+        catch( final ConfigurationException ce )
+        {
+            return ( defaultValue );
+        }
+    }
+
+    /**
      * Returns the value of the configuration element as a <code>boolean</code>.
      *
      * @throws ConfigurationException if an error occurs
@@ -443,6 +485,52 @@
         try
         {
             return getAttributeAsFloat( name );
+        }
+        catch( final ConfigurationException ce )
+        {
+            return defaultValue;
+        }
+    }
+
+    /**
+     * Returns the value of the attribute specified by its name as a
+     * <code>double</code>.
+     *
+     * @param name the name of the attribute
+     * @throws ConfigurationException if an error occurs
+     * @return the value
+     */
+    public double getAttributeAsDouble( final String name )
+        throws ConfigurationException
+    {
+        final String value = getAttribute( name );
+        try
+        {
+            return Double.parseDouble( value );
+        }
+        catch( final Exception e )
+        {
+            final String message =
+                "Cannot parse the value \"" + value
+                + "\" as a double in the attribute \""
+                + name + "\" at " + getLocation();
+            throw new ConfigurationException( message );
+        }
+    }
+
+    /**
+     * Returns the value of the attribute specified by its name as a
+     * <code>double</code>.
+     *
+     * @param name the name of the attribute
+     * @param defaultValue the default value to return if value malformed or empty
+     * @return the value
+     */
+    public double getAttributeAsDouble( final String name, final double defaultValue )
+    {
+        try
+        {
+            return getAttributeAsDouble( name );
         }
         catch( final ConfigurationException ce )
         {

Modified: excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java
URL: http://svn.apache.org/viewcvs/excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java?view=diff&r1=156532&r2=156533
==============================================================================
--- excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java (original)
+++ excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/DefaultConfiguration.java Tue Mar  8 06:51:40 2005
@@ -397,6 +397,16 @@
     }    
     
     /**
+     * Set the value of this <code>Configuration</code> object to the specified double.
+     *
+     * @param value a <code>double</code> value
+     */
+    public void setValue( final double value )
+    {
+        setValue( String.valueOf( value ) );
+    }    
+    
+    /**
      * Set the value of the specified attribute to the specified string.
      *
      * @param name name of the attribute to set
@@ -463,6 +473,17 @@
      * @param value an <code>float</code> value
      */
     public void setAttribute( final String name, final float value )
+    {
+        setAttribute( name, String.valueOf( value ) );
+    }
+    
+    /**
+     * Set the value of the specified attribute to the specified double.
+     *
+     * @param name name of the attribute to set
+     * @param value an <code>double</code> value
+     */
+    public void setAttribute( final String name, final double value )
     {
         setAttribute( name, String.valueOf( value ) );
     }

Modified: excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/MutableConfiguration.java
URL: http://svn.apache.org/viewcvs/excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/MutableConfiguration.java?view=diff&r1=156532&r2=156533
==============================================================================
--- excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/MutableConfiguration.java (original)
+++ excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/configuration/MutableConfiguration.java Tue Mar  8 06:51:40 2005
@@ -60,6 +60,13 @@
     public void setValue( final float value );
     
     /**
+     * Set the value of this <code>Configuration</code> object to the specified double.
+     *
+     * @param value a <code>double</code> value
+     */
+    public void setValue( final double value );
+    
+    /**
      * Set the value of the specified attribute to the specified string.
      *
      * @param name name of the attribute to set
@@ -98,6 +105,14 @@
      * @param value an <code>float</code> value
      */
     public void setAttribute( final String name, final float value );
+    
+    /**
+     * Set the value of the specified attribute to the specified double.
+     *
+     * @param name name of the attribute to set
+     * @param value an <code>double</code> value
+     */
+    public void setAttribute( final String name, final double value );
     
     /**
      * Add a child <code>Configuration</code> to this configuration element.

Modified: excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationTestCase.java
URL: http://svn.apache.org/viewcvs/excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationTestCase.java?view=diff&r1=156532&r2=156533
==============================================================================
--- excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationTestCase.java (original)
+++ excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/configuration/test/DefaultConfigurationTestCase.java Tue Mar  8 06:51:40 2005
@@ -155,13 +155,15 @@
         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( "float", 1.2345679f );
+        config.setAttribute( "double", 1.2345678901234567 );
         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( 1.2345679f, config.getAttributeAsFloat("float"), 0 );
+        assertEquals( 1.2345678901234567, config.getAttributeAsDouble("double"), 0 );
         assertEquals( "string", config.getAttribute("string") );
         assertEquals( "true", config.getAttribute("boolean") );
         



---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org