KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > math > StatCalculator


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/math/StatCalculator.java,v 1.2 2004/02/11 23:57:23 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.math;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  * This class serves as a way to calculate the median of a list of values. It
30  * is not threadsafe.
31  */

32 public class StatCalculator implements Serializable JavaDoc
33 {
34     List JavaDoc values = new ArrayList JavaDoc();
35     double sum = 0;
36     double sumOfSquares = 0;
37     double mean = 0;
38     double deviation = 0;
39     int count = 0;
40     
41     public void clear()
42     {
43         values.clear();
44         sum = 0;
45         sumOfSquares = 0;
46         mean = 0;
47         deviation = 0;
48         count = 0;
49     }
50
51     public void addValue(long newValue)
52     {
53         Number JavaDoc val = new Long JavaDoc(newValue);
54         addValue(val);
55     }
56
57     public void addValue(int newValue)
58     {
59         Number JavaDoc val = new Integer JavaDoc(newValue);
60         addValue(val);
61     }
62
63     public void addValue(float newValue)
64     {
65         Number JavaDoc val = new Float JavaDoc(newValue);
66         addValue(val);
67     }
68
69     public void addValue(double newValue)
70     {
71         Number JavaDoc val = new Double JavaDoc(newValue);
72         addValue(val);
73     }
74
75     public Number JavaDoc getMedian()
76     {
77         return (Number JavaDoc) values.get(values.size() / 2);
78     }
79     
80     public double getMean()
81     {
82         return mean;
83     }
84     
85     public double getStandardDeviation()
86     {
87         return deviation;
88     }
89     
90     public Number JavaDoc getMin()
91     {
92         return (Number JavaDoc)values.get(0);
93     }
94     
95     public Number JavaDoc getMax()
96     {
97         return (Number JavaDoc)values.get(count-1);
98     }
99     
100     public int getCount()
101     {
102         return count;
103     }
104
105     public void addValue(Number JavaDoc val)
106     {
107         int index = Collections.binarySearch(values, val);
108         if (index >= 0 && index < values.size())
109         {
110             values.add(index, val);
111         }
112         else if (index == values.size() || values.size() == 0)
113         {
114             values.add(val);
115         }
116         else
117         {;
118             values.add((index * (-1)) - 1, val);
119         }
120         count++;
121         double currentVal = val.doubleValue();
122         sum += currentVal;
123         sumOfSquares += currentVal * currentVal;
124         mean = sum / count;
125         deviation = Math.sqrt( (sumOfSquares / count) - (mean * mean) );
126     }
127     
128     public static class Test extends TestCase
129     {
130         StatCalculator calc;
131         
132         public Test(String JavaDoc name)
133         {
134             super(name);
135         }
136         
137         public void setUp()
138         {
139             calc = new StatCalculator();
140         }
141         
142         public void testCalculation()
143         {
144             calc.addValue(18);
145             calc.addValue(10);
146             calc.addValue(9);
147             calc.addValue(11);
148             calc.addValue(28);
149             calc.addValue(3);
150             calc.addValue(30);
151             calc.addValue(15);
152             calc.addValue(15);
153             calc.addValue(21);
154             assertEquals(16,(int)calc.getMean());
155             assertEquals(8.0622577F,(float)calc.getStandardDeviation(),0F);
156             assertEquals(30,calc.getMax().intValue());
157             assertEquals(3,calc.getMin().intValue());
158             assertEquals(15,calc.getMedian().intValue());
159         }
160     }
161 }
162
Popular Tags