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.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022
023import javax.management.j2ee.statistics.CountStatistic;
024
025/**
026 * A count statistic implementation
027 * 
028 * 
029 */
030public class PollCountStatisticImpl extends StatisticImpl implements CountStatistic {
031
032    private PollCountStatisticImpl parent;
033    private List<PollCountStatisticImpl> children;
034
035    public PollCountStatisticImpl(PollCountStatisticImpl parent, String name, String description) {
036        this(name, description);
037        setParent(parent);
038    }
039
040    public PollCountStatisticImpl(String name, String description) {
041        this(name, "count", description);
042    }
043
044    public PollCountStatisticImpl(String name, String unit, String description) {
045        super(name, unit, description);
046    }
047
048    public PollCountStatisticImpl getParent() {
049        return parent;
050    }
051
052    public void setParent(PollCountStatisticImpl parent) {
053        if (this.parent != null) {
054            this.parent.removeChild(this);
055        }
056        this.parent = parent;
057        if (this.parent != null) {
058            this.parent.addChild(this);
059        }
060    }
061
062    private synchronized void removeChild(PollCountStatisticImpl child) {
063        if (children != null) {
064            children.remove(child);
065        }
066    }
067
068    private synchronized void addChild(PollCountStatisticImpl child) {
069        if (children == null) {
070            children = new ArrayList<PollCountStatisticImpl>();
071        }
072        children.add(child);
073    }
074
075    public synchronized long getCount() {
076        if (children == null) {
077            return 0;
078        }
079        long count = 0;
080        for (Iterator<PollCountStatisticImpl> iter = children.iterator(); iter.hasNext();) {
081            PollCountStatisticImpl child = iter.next();
082            count += child.getCount();
083        }
084        return count;
085    }
086
087    protected void appendFieldDescription(StringBuffer buffer) {
088        buffer.append(" count: ");
089        buffer.append(Long.toString(getCount()));
090        super.appendFieldDescription(buffer);
091    }
092
093    /**
094     * @return the average time period that elapses between counter increments
095     *         since the last reset.
096     */
097    public double getPeriod() {
098        double count = getCount();
099        if (count == 0) {
100            return 0;
101        }
102        double time = System.currentTimeMillis() - getStartTime();
103        return time / (count * 1000.0);
104    }
105
106    /**
107     * @return the number of times per second that the counter is incrementing
108     *         since the last reset.
109     */
110    public double getFrequency() {
111        double count = getCount();
112        double time = System.currentTimeMillis() - getStartTime();
113        return count * 1000.0 / time;
114    }
115
116}