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 javax.management.j2ee.statistics.Statistic;
021 /**
022 * Base class for a Statistic implementation
023 *
024 * @version $Revision: 1.1.1.1 $
025 */
026 public class StatisticImpl implements Statistic, Resettable {
027 private String name;
028 private String unit;
029 private String description;
030 private long startTime;
031 private long lastSampleTime;
032
033 public StatisticImpl(String name, String unit, String description) {
034 this.name = name;
035 this.unit = unit;
036 this.description = description;
037 startTime = System.currentTimeMillis();
038 lastSampleTime = startTime;
039 }
040
041 public synchronized void reset() {
042 startTime = System.currentTimeMillis();
043 lastSampleTime = startTime;
044 }
045
046 protected synchronized void updateSampleTime() {
047 lastSampleTime = System.currentTimeMillis();
048 }
049
050 public synchronized String toString() {
051 StringBuffer buffer = new StringBuffer();
052 buffer.append(name);
053 buffer.append("{");
054 appendFieldDescription(buffer);
055 buffer.append(" }");
056 return buffer.toString();
057 }
058
059 public String getName() {
060 return name;
061 }
062
063 public String getUnit() {
064 return unit;
065 }
066
067 public String getDescription() {
068 return description;
069 }
070
071 public synchronized long getStartTime() {
072 return startTime;
073 }
074
075 public synchronized long getLastSampleTime() {
076 return lastSampleTime;
077 }
078
079 protected synchronized void appendFieldDescription(StringBuffer buffer) {
080 buffer.append(" unit: ");
081 buffer.append(unit);
082 buffer.append(" startTime: ");
083 //buffer.append(new Date(startTime));
084 buffer.append(startTime);
085 buffer.append(" lastSampleTime: ");
086 //buffer.append(new Date(lastSampleTime));
087 buffer.append(lastSampleTime);
088 buffer.append(" description: ");
089 buffer.append(description);
090 }
091 }