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 */
017 package org.apache.servicemix.jbi.monitoring.stats;
018
019 import java.util.concurrent.atomic.AtomicLong;
020
021 import javax.management.j2ee.statistics.CountStatistic;
022
023 /**
024 * A count statistic implementation
025 *
026 * @version $Revision: 564607 $
027 */
028 public 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 super.reset();
048 counter.set(0);
049 }
050
051 public long getCount() {
052 return counter.get();
053 }
054
055 public void setCount(long count) {
056 counter.set(count);
057 }
058
059 public void add(long amount) {
060 counter.addAndGet(amount);
061 updateSampleTime();
062 if (parent != null) {
063 parent.add(amount);
064 }
065 }
066
067 public void increment() {
068 counter.incrementAndGet();
069 updateSampleTime();
070 if (parent != null) {
071 parent.increment();
072 }
073 }
074
075 public void subtract(long amount) {
076 counter.addAndGet(-amount);
077 updateSampleTime();
078 if (parent != null) {
079 parent.subtract(amount);
080 }
081 }
082
083 public void decrement() {
084 counter.decrementAndGet();
085 updateSampleTime();
086 if (parent != null) {
087 parent.decrement();
088 }
089 }
090
091 public CountStatisticImpl getParent() {
092 return parent;
093 }
094
095 public void setParent(CountStatisticImpl parent) {
096 this.parent = parent;
097 }
098
099 protected void appendFieldDescription(StringBuffer buffer) {
100 buffer.append(" count: ");
101 buffer.append(Long.toString(counter.get()));
102 super.appendFieldDescription(buffer);
103 }
104
105 /**
106 * @return the average time period that elapses between counter increments since the last reset.
107 */
108 public double getPeriod() {
109 double count = counter.get();
110 if (count == 0) {
111 return 0;
112 }
113 double time = System.currentTimeMillis() - getStartTime();
114 return time / (count * 1000.0);
115 }
116
117 /**
118 * @return the number of times per second that the counter is incrementing since the last reset.
119 */
120 public double getFrequency() {
121 double count = counter.get();
122 double time = System.currentTimeMillis() - getStartTime();
123 return count * 1000.0 / time;
124 }
125
126 }