View Javadoc

    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.codehaus.activemq.management;
    19
    20  import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
    21
    22  import javax.management.j2ee.statistics.CountStatistic;
    23
    24  /***
    25   * A count statistic implementation
    26   *
    27   * @version $Revision: 1.5 $
    28   */
    29  public class CountStatisticImpl extends StatisticImpl implements CountStatistic {
    30
    31      private final SynchronizedLong counter = new SynchronizedLong(0);
    32      private CountStatisticImpl parent;
    33
    34      public CountStatisticImpl(CountStatisticImpl parent, String name, String description) {
    35          this(name, description);
    36          this.parent = parent;
    37      }
    38
    39      public CountStatisticImpl(String name, String description) {
    40          this(name, "count", description);
    41      }
    42
    43      public CountStatisticImpl(String name, String unit, String description) {
    44          super(name, unit, description);
    45      }
    46
    47      public void reset() {
    48          super.reset();
    49          counter.set(0);
    50      }
    51
    52      public long getCount() {
    53          return counter.get();
    54      }
    55
    56      public void setCount(long count) {
    57          counter.set(count);
    58      }
    59
    60      public void add(long amount) {
    61          counter.add(amount);
    62          updateSampleTime();
    63          if (parent != null) {
    64              parent.add(amount);
    65          }
    66      }
    67
    68      public void increment() {
    69          counter.increment();
    70          updateSampleTime();
    71          if (parent != null) {
    72              parent.increment();
    73          }
    74      }
    75
    76      public void subtract(long amount) {
    77          counter.subtract(amount);
    78          updateSampleTime();
    79          if (parent != null) {
    80              parent.subtract(amount);
    81          }
    82      }
    83
    84      public void decrement() {
    85          counter.decrement();
    86          updateSampleTime();
    87          if (parent != null) {
    88              parent.decrement();
    89          }
    90      }
    91
    92      public CountStatisticImpl getParent() {
    93          return parent;
    94      }
    95
    96      public void setParent(CountStatisticImpl parent) {
    97          this.parent = parent;
    98      }
    99
    100     protected void appendFieldDescription(StringBuffer buffer) {
    101         buffer.append(" count: ");
    102         buffer.append(Long.toString(counter.get()));
    103         super.appendFieldDescription(buffer);
    104     }
    105 }