001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.activemq.management;
019    
020    import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
021    
022    /**
023     * A count statistic implementation
024     *
025     * @version $Revision: 1.1.1.1 $
026     */
027    public class CountStatisticImpl extends StatisticImpl {
028    
029        private final SynchronizedLong counter = new SynchronizedLong(0);
030        private CountStatisticImpl parent;
031    
032        public CountStatisticImpl(CountStatisticImpl parent, String name, String description) {
033            this(name, description);
034            this.parent = parent;
035        }
036    
037        public CountStatisticImpl(String name, String description) {
038            this(name, "count", description);
039        }
040    
041        public CountStatisticImpl(String name, String unit, String description) {
042            super(name, unit, description);
043        }
044    
045        public void reset() {
046            super.reset();
047            counter.set(0);
048        }
049    
050        public long getCount() {
051            return counter.get();
052        }
053    
054        public void setCount(long count) {
055            counter.set(count);
056        }
057    
058        public void add(long amount) {
059            counter.add(amount);
060            updateSampleTime();
061            if (parent != null) {
062                parent.add(amount);
063            }
064        }
065    
066        public void increment() {
067            counter.increment();
068            updateSampleTime();
069            if (parent != null) {
070                parent.increment();
071            }
072        }
073    
074        public void subtract(long amount) {
075            counter.subtract(amount);
076            updateSampleTime();
077            if (parent != null) {
078                parent.subtract(amount);
079            }
080        }
081        
082        public void decrement() {
083            counter.decrement();
084            updateSampleTime();
085            if (parent != null) {
086                parent.decrement();
087            }
088        }
089    
090        public CountStatisticImpl getParent() {
091            return parent;
092        }
093    
094        public void setParent(CountStatisticImpl parent) {
095            this.parent = parent;
096        }
097    
098        protected void appendFieldDescription(StringBuffer buffer) {
099            buffer.append(" count: ");
100            buffer.append(Long.toString(counter.get()));
101            super.appendFieldDescription(buffer);
102        }
103    }