1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }