1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.monitoring.statistics;
16  
17  /**
18   * <a href="AverageStatistics.java.html"><b><i>View Source</i></b></a>
19   *
20   * <a href="RollingAverageStatistics.java.html"><b><i>View Source</i></b></a>
21   *
22   * @author Rajesh Thiagarajan
23   * @author Brian Wing Shun Chan
24   */
25  public class AverageStatistics extends BaseStatistics {
26  
27      public AverageStatistics(String name) {
28          super(name);
29  
30          _countStatistics = new CountStatistics(name);
31      }
32  
33      public void addDuration(long duration) {
34          _countStatistics.incrementCount();
35  
36          setLastTime(duration);
37  
38          if (getMaxTime() < duration) {
39              setMaxTime(duration);
40          }
41          else if ((getMinTime() == 0) || (getMinTime() > duration)) {
42              setMinTime(duration);
43          }
44  
45          if (_averageTime == 0) {
46              _averageTime = duration;
47          }
48          else {
49              long span = 0;
50  
51              if (_countStatistics.getCount() < getLowerBound()) {
52                  span = getLowerBound();
53              }
54              else if ((_countStatistics.getCount() > getUpperBound())) {
55                  span = getUpperBound();
56              }
57              else {
58                  span = _countStatistics.getCount();
59              }
60  
61              _averageTime = (_averageTime * span + duration) / (span + 1);
62          }
63  
64          setLastSampleTime(System.currentTimeMillis());
65      }
66  
67      public long getAverageTime() {
68          return _averageTime;
69      }
70  
71      public long getCount() {
72          return _countStatistics.getCount();
73      }
74  
75      public void reset() {
76          super.reset();
77  
78          _averageTime = 0;
79      }
80  
81      private long _averageTime;
82      private CountStatistics _countStatistics;
83  
84  }