You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2004/04/24 23:43:26 UTC

cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate BeanListUnivariateImpl.java BeanListUnivariateImplTest.java

psteitz     2004/04/24 14:43:26

  Modified:    math/src/test/org/apache/commons/math/stat/univariate
                        BeanListUnivariateImpl.java
                        BeanListUnivariateImplTest.java
  Log:
  Implemented addValue(double) using DynaBeans.
  
  Revision  Changes    Path
  1.2       +22 -10    jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/BeanListUnivariateImpl.java
  
  Index: BeanListUnivariateImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/BeanListUnivariateImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BeanListUnivariateImpl.java	20 Apr 2004 21:02:35 -0000	1.1
  +++ BeanListUnivariateImpl.java	24 Apr 2004 21:43:26 -0000	1.2
  @@ -19,6 +19,9 @@
   import java.util.List;
   
   import org.apache.commons.beanutils.PropertyUtils;
  +import org.apache.commons.beanutils.DynaBean;
  +import org.apache.commons.beanutils.BasicDynaClass;
  +import org.apache.commons.beanutils.DynaProperty;
   import org.apache.commons.math.MathException;
   import org.apache.commons.math.util.NumberTransformer;
   
  @@ -101,16 +104,25 @@
   	}
   
   	/**
  -	  * @see org.apache.commons.math.stat.Univariate#addValue(double)
  +	  *  Creates a {@link org.apache.commons.beanutils.DynaBean} with a 
  +	  *  {@link org.apache.commons.beanutils.DynaProperty} named 
  +	  *  <code>propertyName,</code> sets the value of the property to <code>v</code>
  +	  *  and adds the DynaBean to the underlying list.
  +	  *
   	  */
  -	public void addValue(double v) {
  -		String msg =
  -			"The BeanListUnivariateImpl does not accept values "
  -				+ "through the addValue method.  Because elements of this list "
  -				+ "are JavaBeans, one must be sure to set the 'propertyName' "
  -				+ "property and add new Beans to the underlying list via the "
  -				+ "addBean(Object bean) method";
  -		throw new UnsupportedOperationException(msg);
  +	public void addValue(double v)  {
  +	    DynaProperty[] props = new DynaProperty[] {
  +	            new DynaProperty(propertyName, Double.class)
  +	    };
  +	    BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
  +	    DynaBean dynaBean = null;
  +	    try {
  +	        dynaBean = dynaClass.newInstance();
  +	    } catch (Exception ex) {              // InstantiationException, IllegalAccessException
  +	        throw new RuntimeException(ex);   // should never happen
  +	    }
  +		dynaBean.set(propertyName, new Double(v));
  +		addObject(dynaBean);
   	}
   
   	/**
  
  
  
  1.2       +26 -12    jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/BeanListUnivariateImplTest.java
  
  Index: BeanListUnivariateImplTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/stat/univariate/BeanListUnivariateImplTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BeanListUnivariateImplTest.java	12 Apr 2004 02:27:50 -0000	1.1
  +++ BeanListUnivariateImplTest.java	24 Apr 2004 21:43:26 -0000	1.2
  @@ -22,8 +22,9 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  -import org.apache.commons.math.beans.*;
  -import org.apache.commons.math.stat.multivariate.*;
  +import org.apache.commons.math.stat.StatUtils;
  +import org.apache.commons.math.beans.VitalStats;
  +import org.apache.commons.math.beans.Patient;
   
   /**
    * Test cases for the {@link BeanListUnivariateImpl} class.
  @@ -69,22 +70,22 @@
       }
       
       /** test stats */
  -    public void testStats() {
  -    	
  -        DescriptiveStatistics u = new BeanListUnivariateImpl( patientList ); 
  -
  +    public void testStats() {	
  +        DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
  +        double[] values = {35d, 23d, 42d};
           assertEquals("total count",3,u.getN(),tolerance);
  -
  +        assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
  +        assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
  +        assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
  +        assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);       
           u.clear();
           assertEquals("total count",0,u.getN(),tolerance);    
  -    }     
  +    }   
       
       public void testPropStats() {
   
           DescriptiveStatistics heartU = new BeanListUnivariateImpl( patientList,
  -                                          "vitalStats.heartRate" );
  -
  -        
  +                                          "vitalStats.heartRate" );       
   
           assertEquals( "Mean heart rate unexpected", 93.333, 
                         heartU.getMean(), 0.001 );
  @@ -106,6 +107,19 @@
           String expected = "property";
           u.setPropertyName(expected);
           assertEquals(expected, u.getPropertyName());
  +    }
  +    
  +    public void testAddValue() {
  +        DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
  +        u.addValue(10);
  +        double[] values = {35d, 23d, 42d, 10d};
  +        assertEquals("total count",4,u.getN(),tolerance);
  +        assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
  +        assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
  +        assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
  +        assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);       
  +        u.clear();
  +        assertEquals("total count",0,u.getN(),tolerance);      
       }
   }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org