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.timer;
22  
23  /*
24   * Created on 19.11.2004
25   *
26   * Contributing Author(s):
27   *
28   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
29   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
30   */
31  /***
32   * A simple interface for measuring time intervals.
33   *
34   * @author vlads
35   * @version $Revision: 1.1 $ ($Author: vlads $)
36   */
37  public class StatisticsItem {
38  
39  	public long count;
40  
41  	public double time_min;
42  	public double time_max;
43  
44      private double time_total;
45  	private double time_total_squares;
46  
47  	public boolean ignoreFirst;
48  
49  	public StatisticsItem() {
50  	    reset();
51  	    this.ignoreFirst = false;
52  	}
53  
54  	public StatisticsItem(boolean ignoreFirst) {
55  	    reset();
56  	    this.ignoreFirst = ignoreFirst;
57  	}
58  
59  	public void reset() {
60  	    time_total = 0;
61  		time_min = 0;
62  		time_max = 0;
63  		count = 0;
64  		time_total_squares = 0;
65  	}
66  
67  	public long getCount() {
68  	    return count;
69  	}
70  
71  	public double getAvg() {
72  	    if (count == 0) {
73  	        return 0;
74  	    }
75  	    if (count == 1) {
76  	        return time_total;
77  	    }
78  
79  	    if (ignoreFirst) {
80  	        // If count >=2 ignore first item
81  	        return time_total/(count - 1);
82  	    } else {
83  	        return time_total/count;
84  	    }
85  	}
86  
87  	public void add(double millis) {
88  	    count ++;
89  
90  	    time_total += millis;
91  	    time_total_squares += millis * millis;
92  
93  		if (((time_min > millis) || (time_min == 0)) && (millis != 0)) {
94  			time_min = millis;
95  		}
96  		if (time_max < millis) {
97  			time_max = millis;
98  		}
99  	}
100 
101 	public double getStandardDeviation() {
102 	    if (count <= 1) {
103 	        return 0;
104 	    }
105 	    double num = time_total_squares - (time_total*time_total) / count;
106 	    double sd = Math.sqrt(num/(count - 1));
107 	    return sd;
108 	}
109 
110 }
111