KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > management > PollCountStatisticImpl


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.management;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
24
25 /**
26  * A count statistic implementation
27  *
28  * @version $Revision$
29  */

30 public class PollCountStatisticImpl extends StatisticImpl implements CountStatistic JavaDoc {
31
32     private PollCountStatisticImpl parent;
33     private ArrayList JavaDoc children;
34
35     public PollCountStatisticImpl(PollCountStatisticImpl parent, String JavaDoc name, String JavaDoc description) {
36         this(name, description);
37         setParent(parent);
38     }
39
40     public PollCountStatisticImpl(String JavaDoc name, String JavaDoc description) {
41         this(name, "count", description);
42     }
43
44     public PollCountStatisticImpl(String JavaDoc name, String JavaDoc unit, String JavaDoc description) {
45         super(name, unit, description);
46     }
47
48     public PollCountStatisticImpl getParent() {
49         return parent;
50     }
51
52     public void setParent(PollCountStatisticImpl parent) {
53         if( this.parent !=null ) {
54             this.parent.removeChild(this);
55         }
56         this.parent = parent;
57         if( this.parent !=null ) {
58             this.parent.addChild(this);
59         }
60     }
61
62     synchronized private void removeChild(PollCountStatisticImpl child) {
63         if( children!=null )
64             children.remove(child);
65     }
66
67     synchronized private void addChild(PollCountStatisticImpl child) {
68         if( children==null )
69             children = new ArrayList JavaDoc();
70         children.add(child);
71     }
72
73     synchronized public long getCount() {
74         if ( children == null )
75             return 0;
76         long count=0;
77         for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
78             PollCountStatisticImpl child = (PollCountStatisticImpl) iter.next();
79             count += child.getCount();
80         }
81         return count;
82     }
83     
84     protected void appendFieldDescription(StringBuffer JavaDoc buffer) {
85         buffer.append(" count: ");
86         buffer.append(Long.toString(getCount()));
87         super.appendFieldDescription(buffer);
88     }
89     
90     /**
91      * @return the average time period that elapses between counter increments since the last reset.
92      */

93     public double getPeriod() {
94         double count = getCount();
95         if( count == 0 )
96             return 0;
97         double time = (System.currentTimeMillis() - getStartTime());
98         return (time/(count*1000.0));
99     }
100     
101     /**
102      * @return the number of times per second that the counter is incrementing since the last reset.
103      */

104     public double getFrequency() {
105         double count = getCount();
106         double time = (System.currentTimeMillis() - getStartTime());
107         return (count*1000.0/time);
108     }
109
110 }
111
Popular Tags