1 /* 2 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.org.glassfish.external.statistics.impl; 27 28 import java.util.concurrent.atomic.AtomicLong; 29 import java.util.Map; 30 import java.lang.reflect.*; 31 import com.sun.org.glassfish.external.statistics.AverageRangeStatistic; 32 33 /** 34 * An implementation of AverageRangeStatistic that provides ways to change the 35 * state externally through mutators. Convenience class that is useful for 36 * components that gather the statistical data. 37 * By merely changing the count (which is a mandatory measurement), rest of the statistical 38 * information could be deduced. 39 */ 40 41 public final class AverageRangeStatisticImpl extends StatisticImpl implements 42 AverageRangeStatistic, InvocationHandler { 43 44 private long currentVal = 0L; 45 private long highWaterMark = Long.MIN_VALUE; 46 private long lowWaterMark = Long.MAX_VALUE; 47 private long numberOfSamples = 0L; 48 private long runningTotal = 0L; 49 50 private final long initCurrentVal; 51 private final long initHighWaterMark; 52 private final long initLowWaterMark; 53 private final long initNumberOfSamples; 54 private final long initRunningTotal; 55 56 private final AverageRangeStatistic as = 57 (AverageRangeStatistic) Proxy.newProxyInstance( 58 AverageRangeStatistic.class.getClassLoader(), 59 new Class[] { AverageRangeStatistic.class }, 60 this); 61 62 public AverageRangeStatisticImpl(long curVal, long highMark, long lowMark, 63 String name, String unit, String desc, 64 long startTime, long sampleTime) { 65 super(name, unit, desc, startTime, sampleTime); 66 currentVal = curVal; 67 initCurrentVal = curVal; 68 highWaterMark = highMark; 69 initHighWaterMark = highMark; 70 lowWaterMark = lowMark; 71 initLowWaterMark = lowMark; 72 numberOfSamples = 0L; 73 initNumberOfSamples = numberOfSamples; 74 runningTotal = 0L; 75 initRunningTotal = runningTotal; 76 } 77 78 public synchronized AverageRangeStatistic getStatistic() { 79 return as; 80 } 81 82 public synchronized String toString() { 83 return super.toString() + NEWLINE + 84 "Current: " + getCurrent() + NEWLINE + 85 "LowWaterMark: " + getLowWaterMark() + NEWLINE + 86 "HighWaterMark: " + getHighWaterMark() + NEWLINE + 87 "Average:" + getAverage(); 88 } 89 90 public synchronized Map getStaticAsMap() { 91 Map m = super.getStaticAsMap(); 92 m.put("current", getCurrent()); 93 m.put("lowwatermark", getLowWaterMark()); 94 m.put("highwatermark", getHighWaterMark()); 95 m.put("average", getAverage()); 96 return m; 97 } 98 99 public synchronized void reset() { 100 super.reset(); 101 currentVal = initCurrentVal; 102 highWaterMark = initHighWaterMark; 103 lowWaterMark = initLowWaterMark; 104 numberOfSamples = initNumberOfSamples; 105 runningTotal = initRunningTotal; 106 sampleTime = -1L; 107 } 108 109 public synchronized long getAverage() { 110 if(numberOfSamples == 0) { 111 return -1; 112 } else { 113 return runningTotal / numberOfSamples; 114 } 115 } 116 117 public synchronized long getCurrent() { 118 return currentVal; 119 } 120 121 public synchronized void setCurrent(long curVal) { 122 currentVal = curVal; 123 lowWaterMark = (curVal >= lowWaterMark ? lowWaterMark : curVal); 124 highWaterMark = (curVal >= highWaterMark ? curVal : highWaterMark); 125 numberOfSamples++; 126 runningTotal += curVal; 127 sampleTime = System.currentTimeMillis(); 128 } 129 130 public synchronized long getHighWaterMark() { 131 return highWaterMark; 132 } 133 134 public synchronized long getLowWaterMark() { 135 return lowWaterMark; 136 } 137 138 // todo: equals implementation 139 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 140 Object result; 141 try { 142 result = method.invoke(this, args); 143 } catch (InvocationTargetException e) { 144 throw e.getTargetException(); 145 } catch (Exception e) { 146 throw new RuntimeException("unexpected invocation exception: " + 147 e.getMessage()); 148 } finally { 149 } 150 return result; 151 } 152 153 }