1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.statistic;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26
27
28 /***
29 * Base for statistic Accumulation.
30 *
31 * Created on 13.12.2004
32 * Contributing Author(s):
33 *
34 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
35 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
36 *
37 * @author vlads
38 * @version $Revision: 1.3 $ ($Author: vlads $) $Date: 2004/12/15 08:00:40 $
39 */
40 public class AccumulationItem implements Accumulation {
41
42 private long count;
43
44
45
46 private double sum;
47
48
49
50 private double squareSum;
51 private double max = Double.NEGATIVE_INFINITY;
52 private double min = Double.POSITIVE_INFINITY;
53
54 private Map attributes;
55
56 public AccumulationItem () {
57 this.attributes = new HashMap();
58 }
59
60 public String getName() {
61 return "n/a";
62 }
63
64 public void reset() {
65 this.count = 0;
66 this.sum = 0;
67 this.squareSum = 0;
68 this.max = Double.NEGATIVE_INFINITY;
69 this.min = Double.POSITIVE_INFINITY;
70 }
71
72 /***
73 * Add the number to the dataset.
74 */
75 public void add(double num) {
76 this.count++;
77 this.sum += num;
78 this.squareSum += num * num;
79 if (num > this.max) {
80 this.max = num;
81 }
82 if (num < this.min) {
83 this.min = num;
84 }
85 }
86
87 /***
88 * Add the number Object to the dataset.
89 */
90 public void add(Object num) {
91 if (num instanceof Double) {
92 add(((Double)num).doubleValue());
93 } else if (num instanceof Long) {
94 add(((Long)num).doubleValue());
95 } else {
96 throw new Error("Unexpected internal logic error in AccumulationItem");
97 }
98 }
99
100
101
102
103 public long getCount() {
104 return this.count;
105 }
106
107
108
109
110 public double getTotal() {
111 return this.sum;
112 }
113
114
115
116
117 public double getMin() {
118 return this.min;
119 }
120
121
122
123
124 public double getMax() {
125 return this.max;
126 }
127
128
129
130
131 public double getMean() {
132 if (this.count == 0) {
133 return 0;
134 }
135 return this.sum / this.count;
136 }
137
138
139
140
141 public double getStandardDeviation() {
142 if (this.count == 0) {
143 return Double.NaN;
144 }
145 double mean = getMean();
146 return Math.sqrt( this.squareSum/this.count - mean*mean );
147 }
148
149 public Object getAttribute(String name) {
150 return this.attributes.get(name);
151 }
152
153 public void setAttribute(String name, Object value) {
154 if (value == null) {
155 this.attributes.remove(name);
156 } else {
157 this.attributes.put(name, value);
158 }
159 }
160
161 }