001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.monitoring.statistics;
016    
017    /**
018     * @author Rajesh Thiagarajan
019     * @author Brian Wing Shun Chan
020     */
021    public class AverageStatistics extends BaseStatistics {
022    
023            public AverageStatistics(String name) {
024                    super(name);
025    
026                    _countStatistics = new CountStatistics(name);
027            }
028    
029            public void addDuration(long duration) {
030                    _countStatistics.incrementCount();
031    
032                    setLastTime(duration);
033    
034                    if (getMaxTime() < duration) {
035                            setMaxTime(duration);
036                    }
037                    else if ((getMinTime() == 0) || (getMinTime() > duration)) {
038                            setMinTime(duration);
039                    }
040    
041                    if (_averageTime == 0) {
042                            _averageTime = duration;
043                    }
044                    else {
045                            long span = 0;
046    
047                            if (_countStatistics.getCount() < getLowerBound()) {
048                                    span = getLowerBound();
049                            }
050                            else if (_countStatistics.getCount() > getUpperBound()) {
051                                    span = getUpperBound();
052                            }
053                            else {
054                                    span = _countStatistics.getCount();
055                            }
056    
057                            _averageTime = (_averageTime * span + duration) / (span + 1);
058                    }
059    
060                    setLastSampleTime(System.currentTimeMillis());
061            }
062    
063            public long getAverageTime() {
064                    return _averageTime;
065            }
066    
067            public long getCount() {
068                    return _countStatistics.getCount();
069            }
070    
071            @Override
072            public void reset() {
073                    super.reset();
074    
075                    _averageTime = 0;
076            }
077    
078            private long _averageTime;
079            private CountStatistics _countStatistics;
080    
081    }