CountStatisticImpl xref

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.activeio.stats;
    19
    20  import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
    21
    22  import javax.management.j2ee.statistics.CountStatistic;
    23
    24  /***
    25   * Shamelessly taken from the ActiveMQ project ( http://activemq.com )
    26   * A count statistic implementation
    27   *
    28   * @version $Revision: 1.1 $
    29   */
    30  public class CountStatisticImpl extends StatisticImpl implements CountStatistic {
    31
    32      private final SynchronizedLong counter = new SynchronizedLong(0);
    33      private CountStatisticImpl parent;
    34
    35      public CountStatisticImpl(CountStatisticImpl parent, String name, String description) {
    36          this(name, description);
    37          this.parent = parent;
    38      }
    39
    40      public CountStatisticImpl(String name, String description) {
    41          this(name, "count", description);
    42      }
    43
    44      public CountStatisticImpl(String name, String unit, String description) {
    45          super(name, unit, description);
    46      }
    47
    48      public void reset() {
    49          super.reset();
    50          counter.set(0);
    51      }
    52
    53      public long getCount() {
    54          return counter.get();
    55      }
    56
    57      public void setCount(long count) {
    58          counter.set(count);
    59      }
    60
    61      public void add(long amount) {
    62          counter.add(amount);
    63          updateSampleTime();
    64          if (parent != null) {
    65              parent.add(amount);
    66          }
    67      }
    68
    69      public void increment() {
    70          counter.increment();
    71          updateSampleTime();
    72          if (parent != null) {
    73              parent.increment();
    74          }
    75      }
    76
    77      public void subtract(long amount) {
    78          counter.subtract(amount);
    79          updateSampleTime();
    80          if (parent != null) {
    81              parent.subtract(amount);
    82          }
    83      }
    84
    85      public void decrement() {
    86          counter.decrement();
    87          updateSampleTime();
    88          if (parent != null) {
    89              parent.decrement();
    90          }
    91      }
    92
    93      public CountStatisticImpl getParent() {
    94          return parent;
    95      }
    96
    97      public void setParent(CountStatisticImpl parent) {
    98          this.parent = parent;
    99      }
    100
    101     protected void appendFieldDescription(StringBuffer buffer) {
    102         buffer.append(" count: ");
    103         buffer.append(Long.toString(counter.get()));
    104         super.appendFieldDescription(buffer);
    105     }
    106 }