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.activemq.management; 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: 1.3 $ 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 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 this.parent = parent; 109 } 110 111 protected void appendFieldDescription(StringBuffer buffer) { 112 buffer.append(" count: "); 113 buffer.append(Long.toString(counter.get())); 114 super.appendFieldDescription(buffer); 115 } 116 117 /** 118 * @return the average time period that elapses between counter increments 119 * since the last reset. 120 */ 121 public double getPeriod() { 122 double count = counter.get(); 123 if (count == 0) { 124 return 0; 125 } 126 double time = System.currentTimeMillis() - getStartTime(); 127 return time / (count * 1000.0); 128 } 129 130 /** 131 * @return the number of times per second that the counter is incrementing 132 * since the last reset. 133 */ 134 public double getFrequency() { 135 double count = counter.get(); 136 double time = System.currentTimeMillis() - getStartTime(); 137 return count * 1000.0 / time; 138 } 139 140 }