View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.statistic;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  //import org.apache.log4j.Logger;
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       * used for mean
45       */
46      private double sum;
47      /*
48       * used for Standard Deviation
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     /* (non-Javadoc)
101      * @see net.sf.jour.statistic.Accumulation#getCount()
102      */
103     public long getCount() {
104         return this.count;
105     }
106 
107     /* (non-Javadoc)
108      * @see net.sf.jour.statistic.Accumulation#getTotal()
109      */
110     public double getTotal() {
111         return this.sum;
112     }
113 
114     /* (non-Javadoc)
115      * @see net.sf.jour.statistic.Accumulation#getMin()
116      */
117     public double getMin() {
118         return this.min;
119     }
120 
121     /* (non-Javadoc)
122      * @see net.sf.jour.statistic.Accumulation#getMax()
123      */
124     public double getMax() {
125         return this.max;
126     }
127 
128     /* (non-Javadoc)
129      * @see net.sf.jour.statistic.Accumulation#getMean()
130      */
131     public double getMean() {
132         if (this.count == 0) {
133 	        return 0;
134 	    }
135         return this.sum / this.count;
136     }
137 
138     /* (non-Javadoc)
139      * @see net.sf.jour.statistic.Accumulation#getStandardDeviation()
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 }