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     * Reset the total size to the new value
072     *
073     * @param size
074     */
075    public synchronized void setTotalSize(long size) {
076        count++;
077        totalSize = size;
078        if (size > maxSize) {
079            maxSize = size;
080        }
081        if (size < minSize || minSize == 0) {
082            minSize = size;
083        }
084        updateSampleTime();
085    }
086
087    /**
088     * @return the maximum size of any step
089     */
090    public long getMaxSize() {
091        return maxSize;
092    }
093
094    /**
095     * @return the minimum size of any step
096     */
097    public synchronized long getMinSize() {
098        return minSize;
099    }
100
101    /**
102     * @return the total size of all the steps added together
103     */
104    public synchronized long getTotalSize() {
105        return totalSize;
106    }
107
108    /**
109     * @return the average size calculated by dividing the total size by the
110     *         number of counts
111     */
112    public synchronized double getAverageSize() {
113        if (count == 0) {
114            return 0;
115        }
116        double d = totalSize;
117        return d / count;
118    }
119
120    /**
121     * @return the average size calculated by dividing the total size by the
122     *         number of counts but excluding the minimum and maximum sizes.
123     */
124    public synchronized double getAverageSizeExcludingMinMax() {
125        if (count <= 2) {
126            return 0;
127        }
128        double d = totalSize - minSize - maxSize;
129        return d / (count - 2);
130    }
131
132    /**
133     * @return the average number of steps per second
134     */
135    public double getAveragePerSecond() {
136        double d = 1000;
137        double averageSize = getAverageSize();
138        if (averageSize == 0) {
139            return 0;
140        }
141        return d / averageSize;
142    }
143
144    /**
145     * @return the average number of steps per second excluding the min & max
146     *         values
147     */
148    public double getAveragePerSecondExcludingMinMax() {
149        double d = 1000;
150        double average = getAverageSizeExcludingMinMax();
151        if (average == 0) {
152            return 0;
153        }
154        return d / average;
155    }
156
157    public SizeStatisticImpl getParent() {
158        return parent;
159    }
160
161    public void setParent(SizeStatisticImpl parent) {
162        this.parent = parent;
163    }
164
165    @Override
166    protected synchronized void appendFieldDescription(StringBuffer buffer) {
167        buffer.append(" count: ");
168        buffer.append(Long.toString(count));
169        buffer.append(" maxSize: ");
170        buffer.append(Long.toString(maxSize));
171        buffer.append(" minSize: ");
172        buffer.append(Long.toString(minSize));
173        buffer.append(" totalSize: ");
174        buffer.append(Long.toString(totalSize));
175        buffer.append(" averageSize: ");
176        buffer.append(Double.toString(getAverageSize()));
177        buffer.append(" averageTimeExMinMax: ");
178        buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
179        buffer.append(" averagePerSecond: ");
180        buffer.append(Double.toString(getAveragePerSecond()));
181        buffer.append(" averagePerSecondExMinMax: ");
182        buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
183        super.appendFieldDescription(buffer);
184    }
185}