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 volatile long maxSize;
023    private volatile 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 maximum size of any step
096     */
097    public void setMaxSize(long size) {
098        maxSize = size;
099    }
100
101    /**
102     * @return the minimum size of any step
103     */
104    public long getMinSize() {
105        return minSize;
106    }
107
108    /**
109     * @return the maximum size of any step
110     */
111    public void setMinSize(long size) {
112        minSize = size;
113    }
114
115    /**
116     * @return the total size of all the steps added together
117     */
118    public synchronized long getTotalSize() {
119        return totalSize;
120    }
121
122    public synchronized void setCount(long count) {
123        this.count = count;
124    }
125
126    /**
127     * @return the average size calculated by dividing the total size by the
128     *         number of counts
129     */
130    public synchronized double getAverageSize() {
131        if (count == 0) {
132            return 0;
133        }
134        double d = totalSize;
135        return d / count;
136    }
137
138    /**
139     * @return the average size calculated by dividing the total size by the
140     *         number of counts but excluding the minimum and maximum sizes.
141     */
142    public synchronized double getAverageSizeExcludingMinMax() {
143        if (count <= 2) {
144            return 0;
145        }
146        double d = totalSize - minSize - maxSize;
147        return d / (count - 2);
148    }
149
150    /**
151     * @return the average number of steps per second
152     */
153    public double getAveragePerSecond() {
154        double d = 1000;
155        double averageSize = getAverageSize();
156        if (averageSize == 0) {
157            return 0;
158        }
159        return d / averageSize;
160    }
161
162    /**
163     * @return the average number of steps per second excluding the min & max
164     *         values
165     */
166    public double getAveragePerSecondExcludingMinMax() {
167        double d = 1000;
168        double average = getAverageSizeExcludingMinMax();
169        if (average == 0) {
170            return 0;
171        }
172        return d / average;
173    }
174
175    public SizeStatisticImpl getParent() {
176        return parent;
177    }
178
179    public void setParent(SizeStatisticImpl parent) {
180        this.parent = parent;
181    }
182
183    @Override
184    protected synchronized void appendFieldDescription(StringBuffer buffer) {
185        buffer.append(" count: ");
186        buffer.append(Long.toString(count));
187        buffer.append(" maxSize: ");
188        buffer.append(Long.toString(maxSize));
189        buffer.append(" minSize: ");
190        buffer.append(Long.toString(minSize));
191        buffer.append(" totalSize: ");
192        buffer.append(Long.toString(totalSize));
193        buffer.append(" averageSize: ");
194        buffer.append(Double.toString(getAverageSize()));
195        buffer.append(" averageTimeExMinMax: ");
196        buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
197        buffer.append(" averagePerSecond: ");
198        buffer.append(Double.toString(getAveragePerSecond()));
199        buffer.append(" averagePerSecondExMinMax: ");
200        buffer.append(Double.toString(getAveragePerSecondExcludingMinMax()));
201        super.appendFieldDescription(buffer);
202    }
203}