001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.management;
018
019public class SizeStatisticImpl extends StatisticImpl {
020
021    private long count;
022    private long maxSize;
023    private long minSize;
024    private long totalSize;
025    private SizeStatisticImpl parent;
026
027    public SizeStatisticImpl(String name, String description) {
028        this(name, "bytes", description);
029    }
030
031    public SizeStatisticImpl(SizeStatisticImpl parent, String name, String description) {
032        this(name, description);
033        this.parent = parent;
034    }
035
036    public SizeStatisticImpl(String name, String unit, String description) {
037        super(name, unit, description);
038    }
039
040    @Override
041    public synchronized void reset() {
042        if (isDoReset()) {
043            super.reset();
044            count = 0;
045            maxSize = 0;
046            minSize = 0;
047            totalSize = 0;
048        }
049    }
050
051    public synchronized long getCount() {
052        return count;
053    }
054
055    public synchronized void addSize(long size) {
056        count++;
057        totalSize += size;
058        if (size > maxSize) {
059            maxSize = size;
060        }
061        if (size < minSize || minSize == 0) {
062            minSize = size;
063        }
064        updateSampleTime();
065        if (parent != null) {
066            parent.addSize(size);
067        }
068    }
069
070    /**
071     * @return the maximum size of any step
072     */
073    public long getMaxSize() {
074        return maxSize;
075    }
076
077    /**
078     * @return the minimum size of any step
079     */
080    public synchronized long getMinSize() {
081        return minSize;
082    }
083
084    /**
085     * @return the total size of all the steps added together
086     */
087    public synchronized long getTotalSize() {
088        return totalSize;
089    }
090
091    /**
092     * @return the average size calculated by dividing the total size by the
093     *         number of counts
094     */
095    public synchronized double getAverageSize() {
096        if (count == 0) {
097            return 0;
098        }
099        double d = totalSize;
100        return d / count;
101    }
102
103    /**
104     * @return the average size calculated by dividing the total size by the
105     *         number of counts but excluding the minimum and maximum sizes.
106     */
107    public synchronized double getAverageSizeExcludingMinMax() {
108        if (count <= 2) {
109            return 0;
110        }
111        double d = totalSize - minSize - maxSize;
112        return d / (count - 2);
113    }
114
115    /**
116     * @return the average number of steps per second
117     */
118    public double getAveragePerSecond() {
119        double d = 1000;
120        double averageSize = getAverageSize();
121        if (averageSize == 0) {
122            return 0;
123        }
124        return d / averageSize;
125    }
126
127    /**
128     * @return the average number of steps per second excluding the min & max
129     *         values
130     */
131    public double getAveragePerSecondExcludingMinMax() {
132        double d = 1000;
133        double average = getAverageSizeExcludingMinMax();
134        if (average == 0) {
135            return 0;
136        }
137        return d / average;
138    }
139
140    public SizeStatisticImpl getParent() {
141        return parent;
142    }
143
144    public void setParent(SizeStatisticImpl parent) {
145        this.parent = parent;
146    }
147
148    @Override
149    protected synchronized void appendFieldDescription(StringBuffer buffer) {
150        buffer.append(" count: ");
151        buffer.append(Long.toString(count));
152        buffer.append(" maxSize: ");
153        buffer.append(Long.toString(maxSize));
154        buffer.append(" minSize: ");
155        buffer.append(Long.toString(minSize));
156        buffer.append(" totalSize: ");
157        buffer.append(Long.toString(totalSize));
158        buffer.append(" averageSize: ");
159        buffer.append(Double.toString(getAverageSize()));
160        buffer.append(" averageTimeExMinMax: ");
161        buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
162        buffer.append(" averagePerSecond: ");
163        buffer.append(Double.toString(getAveragePerSecond()));
164        buffer.append(" averagePerSecondExMinMax: ");
165        buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
166        super.appendFieldDescription(buffer);
167    }
168}