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
019import java.util.concurrent.atomic.AtomicLong;
020
021import javax.management.j2ee.statistics.CountStatistic;
022
023/**
024 * A count statistic implementation
025 * 
026 * 
027 */
028public class CountStatisticImpl extends StatisticImpl implements CountStatistic {
029
030    private final AtomicLong counter = new AtomicLong(0);
031    private CountStatisticImpl parent;
032
033    public CountStatisticImpl(CountStatisticImpl parent, String name, String description) {
034        this(name, description);
035        this.parent = parent;
036    }
037
038    public CountStatisticImpl(String name, String description) {
039        this(name, "count", description);
040    }
041
042    public CountStatisticImpl(String name, String unit, String description) {
043        super(name, unit, description);
044    }
045
046    public void reset() {
047        if (isDoReset()) {
048            super.reset();
049            counter.set(0);
050        }
051    }
052
053    public long getCount() {
054        return counter.get();
055    }
056
057    public void setCount(long count) {
058        if (isEnabled()) {
059            counter.set(count);
060        }
061    }
062
063    public void add(long amount) {
064        if (isEnabled()) {
065            counter.addAndGet(amount);
066            updateSampleTime();
067            if (parent != null) {
068                parent.add(amount);
069            }
070        }
071    }
072
073    public void increment() {
074        if (isEnabled()) {
075            counter.incrementAndGet();
076            updateSampleTime();
077            if (parent != null) {
078                parent.increment();
079            }
080        }
081    }
082
083    public void subtract(long amount) {
084        if (isEnabled()) {
085            counter.addAndGet(-amount);
086            updateSampleTime();
087            if (parent != null) {
088                parent.subtract(amount);
089            }
090        }
091    }
092
093    public void decrement() {
094        if (isEnabled()) {
095            counter.decrementAndGet();
096            updateSampleTime();
097            if (parent != null) {
098                parent.decrement();
099            }
100        }
101    }
102
103    public CountStatisticImpl getParent() {
104        return parent;
105    }
106
107    public void setParent(CountStatisticImpl parent) {
108        if (this.parent != null) {
109            this.parent.subtract(this.getCount());
110        }
111        this.parent = parent;
112    }
113
114    protected void appendFieldDescription(StringBuffer buffer) {
115        buffer.append(" count: ");
116        buffer.append(Long.toString(counter.get()));
117        super.appendFieldDescription(buffer);
118    }
119
120    /**
121     * @return the average time period that elapses between counter increments
122     *         since the last reset.
123     */
124    public double getPeriod() {
125        double count = counter.get();
126        if (count == 0) {
127            return 0;
128        }
129        double time = System.currentTimeMillis() - getStartTime();
130        return time / (count * 1000.0);
131    }
132
133    /**
134     * @return the number of times per second that the counter is incrementing
135     *         since the last reset.
136     */
137    public double getFrequency() {
138        double count = counter.get();
139        double time = System.currentTimeMillis() - getStartTime();
140        return count * 1000.0 / time;
141    }
142
143}