Skip to content

Commit 8992fc6

Browse files
committed
Removing BeanListUnivariate example from test cases. Improving ListUnivariate Serialization Example.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141257 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3227063 commit 8992fc6

4 files changed

Lines changed: 176 additions & 13 deletions

File tree

src/test/org/apache/commons/math/stat/univariate/BeanListUnivariateImpl.java renamed to src/experimental/org/apache/commons/math/stat/univariate/BeanListUnivariateImpl.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.apache.commons.math.stat.univariate;
1717

18+
import java.io.Serializable;
1819
import java.lang.reflect.InvocationTargetException;
20+
import java.util.ArrayList;
1921
import java.util.List;
2022

2123
import org.apache.commons.beanutils.PropertyUtils;
@@ -30,15 +32,25 @@
3032
* univariate statistics for a List of Java Beans by property. This
3133
* implementation uses beanutils' PropertyUtils to get a simple, nested,
3234
* indexed, mapped, or combined property from an element of a List.
33-
* @version $Revision: 1.2 $ $Date: 2004/04/24 21:43:26 $
35+
* @version $Revision: 1.1 $ $Date: 2004/06/01 21:28:06 $
3436
*/
35-
public class BeanListUnivariateImpl extends ListUnivariateImpl {
37+
public class BeanListUnivariateImpl extends ListUnivariateImpl implements Serializable {
3638

39+
/** Serializable version identifier */
40+
static final long serialVersionUID = -6428201899045406285L;
41+
3742
/**
3843
* propertyName of the property to get from the bean
3944
*/
4045
private String propertyName;
4146

47+
/**
48+
* No argument Constructor
49+
*/
50+
public BeanListUnivariateImpl(){
51+
this(new ArrayList());
52+
}
53+
4254
/**
4355
* Construct a BeanListUnivariate with specified
4456
* backing list

src/test/org/apache/commons/math/stat/univariate/BeanListUnivariateImplTest.java renamed to src/experimental/org/apache/commons/math/stat/univariate/BeanListUnivariateImplTest.java

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,34 @@
2222
import junit.framework.TestCase;
2323
import junit.framework.TestSuite;
2424

25+
import org.apache.commons.math.TestUtils;
2526
import org.apache.commons.math.stat.StatUtils;
26-
import org.apache.commons.math.beans.VitalStats;
27-
import org.apache.commons.math.beans.Patient;
2827

2928
/**
3029
* Test cases for the {@link BeanListUnivariateImpl} class.
3130
*
32-
* @version $Revision: 1.2 $ $Date: 2004/04/24 21:43:26 $
31+
* @version $Revision: 1.1 $ $Date: 2004/06/01 21:28:06 $
3332
*/
3433

3534
public final class BeanListUnivariateImplTest extends TestCase {
3635

36+
private double one = 1;
37+
private float two = 2;
38+
private int three = 3;
39+
private double mean = 2;
40+
private double sumSq = 18;
41+
private double sum = 8;
42+
private double var = 0.666666666666666666667;
43+
private double std = Math.sqrt(var);
44+
private double n = 4;
45+
private double min = 1;
46+
private double max = 3;
47+
private double skewness = 0;
48+
private double kurtosis = 0.5;
49+
private double tolerance = 10E-15;
50+
51+
3752
private List patientList = null;
38-
private double tolerance = Double.MIN_VALUE;
3953

4054
public BeanListUnivariateImplTest(String name) {
4155
super(name);
@@ -121,5 +135,93 @@ public void testAddValue() {
121135
u.clear();
122136
assertEquals("total count",0,u.getN(),tolerance);
123137
}
138+
139+
/** test stats */
140+
public void testSerialization() {
141+
142+
double[] values = {35d, 23d, 42d};
143+
144+
DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" );
145+
assertEquals("total count",3,u.getN(),tolerance);
146+
assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
147+
assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
148+
assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
149+
assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);
150+
151+
152+
DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
153+
assertEquals("total count",3,u2.getN(),tolerance);
154+
assertEquals("mean", StatUtils.mean(values), u2.getMean(), tolerance);
155+
assertEquals("min", StatUtils.min(values), u2.getMin(), tolerance);
156+
assertEquals("max", StatUtils.max(values), u2.getMax(), tolerance);
157+
assertEquals("var", StatUtils.variance(values), u2.getVariance(), tolerance);
158+
159+
u.clear();
160+
assertEquals("total count",0,u.getN(),tolerance);
161+
162+
u2.clear();
163+
assertEquals("total count",0,u2.getN(),tolerance);
164+
165+
}
166+
167+
public class VitalStats {
168+
169+
private Double heartrate;
170+
private Double temperature;
171+
172+
public VitalStats() {
173+
}
174+
175+
public VitalStats(Double heartrate, Double temperature) {
176+
setHeartRate( heartrate );
177+
setTemperature( temperature );
178+
}
179+
180+
public Double getHeartRate() {
181+
return heartrate;
182+
}
183+
184+
public void setHeartRate(Double heartrate) {
185+
this.heartrate = heartrate;
186+
}
187+
188+
public Double getTemperature() {
189+
return temperature;
190+
}
191+
192+
public void setTemperature(Double temperature) {
193+
this.temperature = temperature;
194+
}
195+
}
196+
197+
public class Patient {
198+
199+
private VitalStats vitalStats;
200+
private Integer age;
201+
202+
public Patient() {
203+
}
204+
205+
public Patient(VitalStats vitalStats, Integer age) {
206+
setVitalStats( vitalStats );
207+
setAge( age );
208+
}
209+
210+
public VitalStats getVitalStats() {
211+
return( vitalStats );
212+
}
213+
214+
public void setVitalStats(VitalStats vitalStats) {
215+
this.vitalStats = vitalStats;
216+
}
217+
218+
public Integer getAge() {
219+
return age;
220+
}
221+
222+
public void setAge(Integer age) {
223+
this.age = age;
224+
}
225+
}
124226
}
125227

src/test/org/apache/commons/math/stat/univariate/ListUnivariateImpl.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@
1515
*/
1616
package org.apache.commons.math.stat.univariate;
1717

18+
import java.io.Serializable;
19+
import java.util.ArrayList;
1820
import java.util.List;
1921

2022
import org.apache.commons.math.MathException;
2123
import org.apache.commons.math.stat.univariate.UnivariateStatistic;
22-
import org.apache.commons.math.stat.univariate.AbstractDescriptiveStatistics;
24+
import org.apache.commons.math.stat.univariate.DescriptiveStatistics;
2325
import org.apache.commons.math.util.DefaultTransformer;
2426
import org.apache.commons.math.util.NumberTransformer;
2527

2628
/**
27-
* @version $Revision: 1.3 $ $Date: 2004/05/23 00:33:41 $
29+
* @version $Revision: 1.4 $ $Date: 2004/06/01 21:28:06 $
2830
*/
29-
public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
31+
public class ListUnivariateImpl extends DescriptiveStatistics implements Serializable {
3032

33+
/** Serializable version identifier */
34+
static final long serialVersionUID = -8837442489133392138L;
35+
3136
/**
3237
* Holds a reference to a list - GENERICs are going to make
3338
* out lives easier here as we could only accept List<Number>
@@ -40,6 +45,13 @@ public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
4045
/** hold the window size **/
4146
protected int windowSize = DescriptiveStatistics.INFINITE_WINDOW;
4247

48+
/**
49+
* No argument Constructor
50+
*/
51+
public ListUnivariateImpl(){
52+
this(new ArrayList());
53+
}
54+
4355
/**
4456
* Construct a ListUnivariate with a specific List.
4557
* @param list The list that will back this DescriptiveStatistics
@@ -196,8 +208,8 @@ public synchronized void setWindowSize(int windowSize) {
196208
}
197209
}
198210

199-
public int getWindowSize() {
200-
return windowSize;
201-
}
211+
public int getWindowSize() {
212+
return windowSize;
213+
}
202214

203215
}

src/test/org/apache/commons/math/stat/univariate/ListUnivariateImplTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21+
import org.apache.commons.math.TestUtils;
22+
2123
import junit.framework.Test;
2224
import junit.framework.TestCase;
2325
import junit.framework.TestSuite;
2426

2527
/**
2628
* Test cases for the {@link Univariate} class.
2729
*
28-
* @version $Revision: 1.2 $ $Date: 2004/05/23 00:56:15 $
30+
* @version $Revision: 1.3 $ $Date: 2004/06/01 21:28:06 $
2931
*/
3032

3133
public final class ListUnivariateImplTest extends TestCase {
34+
3235
private double one = 1;
3336
private float two = 2;
3437
private int three = 3;
@@ -134,6 +137,40 @@ public void testProductAndGeometricMean() throws Exception {
134137

135138

136139
}
140+
141+
/** test stats */
142+
public void testSerialization() {
143+
144+
DescriptiveStatistics u = null;
145+
146+
try {
147+
u = DescriptiveStatistics.newInstance(ListUnivariateImpl.class);
148+
} catch (InstantiationException e) {
149+
fail(e.getMessage());
150+
} catch (IllegalAccessException e) {
151+
fail(e.getMessage());
152+
}
153+
154+
assertEquals("total count",0,u.getN(),tolerance);
155+
u.addValue(one);
156+
u.addValue(two);
157+
158+
DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
159+
160+
u2.addValue(two);
161+
u2.addValue(three);
162+
163+
assertEquals("N",n,u2.getN(),tolerance);
164+
assertEquals("sum",sum,u2.getSum(),tolerance);
165+
assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
166+
assertEquals("var",var,u2.getVariance(),tolerance);
167+
assertEquals("std",std,u2.getStandardDeviation(),tolerance);
168+
assertEquals("mean",mean,u2.getMean(),tolerance);
169+
assertEquals("min",min,u2.getMin(),tolerance);
170+
assertEquals("max",max,u2.getMax(),tolerance);
137171

172+
u2.clear();
173+
assertEquals("total count",0,u2.getN(),tolerance);
174+
}
138175
}
139176

0 commit comments

Comments
 (0)