/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.monitoring.internal.statistics;
/**
* @author Rajesh Thiagarajan
* @author Brian Wing Shun Chan
*/
public class AverageStatistics extends BaseStatistics {
public AverageStatistics(String name) {
super(name);
_countStatistics = new CountStatistics(name);
}
public void addDuration(long duration) {
_countStatistics.incrementCount();
setLastTime(duration);
if (getMaxTime() < duration) {
setMaxTime(duration);
}
else if ((getMinTime() == 0) || (getMinTime() > duration)) {
setMinTime(duration);
}
if (_averageTime == 0) {
_averageTime = duration;
}
else {
long span = 0;
if (_countStatistics.getCount() < getLowerBound()) {
span = getLowerBound();
}
else if (_countStatistics.getCount() > getUpperBound()) {
span = getUpperBound();
}
else {
span = _countStatistics.getCount();
}
_averageTime = (_averageTime * span + duration) / (span + 1);
}
setLastSampleTime(System.currentTimeMillis());
}
public long getAverageTime() {
return _averageTime;
}
public long getCount() {
return _countStatistics.getCount();
}
@Override
public void reset() {
super.reset();
_averageTime = 0;
}
private long _averageTime;
private final CountStatistics _countStatistics;
}