    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 javax.management.j2ee.statistics.Statistic;
    21
    22  /***
    23   * Shamelessly taken from the ActiveMQ project ( http://activemq.com )
    24   *
    25   * Base class for a Statistic implementation
    26   * @version $Revision: 1.1 $
    27   */
    28  public class StatisticImpl implements Statistic {
    29      private String name;
    30      private String unit;
    31      private String description;
    32      private long startTime;
    33      private long lastSampleTime;
    34
    35      public StatisticImpl(String name, String unit, String description) {
    36          this.name = name;
    37          this.unit = unit;
    38          this.description = description;
    39          startTime = System.currentTimeMillis();
    40          lastSampleTime = startTime;
    41      }
    42
    43      public synchronized void reset() {
    44          startTime = System.currentTimeMillis();
    45          lastSampleTime = startTime;
    46      }
    47
    48      protected synchronized void updateSampleTime() {
    49          lastSampleTime = System.currentTimeMillis();
    50      }
    51
    52      public synchronized String toString() {
    53          StringBuffer buffer = new StringBuffer();
    54          buffer.append(name);
    55          buffer.append("{");
    56          appendFieldDescription(buffer);
    57          buffer.append(" }");
    58          return buffer.toString();
    59      }
    60
    61      public String getName() {
    62          return name;
    63      }
    64
    65      public String getUnit() {
    66          return unit;
    67      }
    68
    69      public String getDescription() {
    70          return description;
    71      }
    72
    73      public synchronized long getStartTime() {
    74          return startTime;
    75      }
    76
    77      public synchronized long getLastSampleTime() {
    78          return lastSampleTime;
    79      }
    80
    81      protected synchronized void appendFieldDescription(StringBuffer buffer) {
    82          buffer.append(" unit: ");
    83          buffer.append(unit);
    84          buffer.append(" startTime: ");
    85          //buffer.append(new Date(startTime));
    86          buffer.append(startTime);
    87          buffer.append(" lastSampleTime: ");
    88          //buffer.append(new Date(lastSampleTime));
    89          buffer.append(lastSampleTime);
    90          buffer.append(" description: ");
    91          buffer.append(description);
    92      }
    93  }