aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: aoqi@0: aoqi@0: package com.sun.org.glassfish.external.statistics.impl; aoqi@0: aoqi@0: import java.util.Map; aoqi@0: import java.lang.reflect.*; aoqi@0: import com.sun.org.glassfish.external.statistics.AverageRangeStatistic; aoqi@0: aoqi@0: /** aoqi@0: * An implementation of AverageRangeStatistic that provides ways to change the aoqi@0: * state externally through mutators. Convenience class that is useful for aoqi@0: * components that gather the statistical data. aoqi@0: * By merely changing the count (which is a mandatory measurement), rest of the statistical aoqi@0: * information could be deduced. aoqi@0: */ aoqi@0: aoqi@0: public final class AverageRangeStatisticImpl extends StatisticImpl implements aoqi@0: AverageRangeStatistic, InvocationHandler { aoqi@0: aoqi@0: private long currentVal = 0L; aoqi@0: private long highWaterMark = Long.MIN_VALUE; aoqi@0: private long lowWaterMark = Long.MAX_VALUE; aoqi@0: private long numberOfSamples = 0L; aoqi@0: private long runningTotal = 0L; aoqi@0: aoqi@0: private final long initCurrentVal; aoqi@0: private final long initHighWaterMark; aoqi@0: private final long initLowWaterMark; aoqi@0: private final long initNumberOfSamples; aoqi@0: private final long initRunningTotal; aoqi@0: aoqi@0: private final AverageRangeStatistic as = aoqi@0: (AverageRangeStatistic) Proxy.newProxyInstance( aoqi@0: AverageRangeStatistic.class.getClassLoader(), aoqi@0: new Class[] { AverageRangeStatistic.class }, aoqi@0: this); aoqi@0: aoqi@0: public AverageRangeStatisticImpl(long curVal, long highMark, long lowMark, aoqi@0: String name, String unit, String desc, aoqi@0: long startTime, long sampleTime) { aoqi@0: super(name, unit, desc, startTime, sampleTime); aoqi@0: currentVal = curVal; aoqi@0: initCurrentVal = curVal; aoqi@0: highWaterMark = highMark; aoqi@0: initHighWaterMark = highMark; aoqi@0: lowWaterMark = lowMark; aoqi@0: initLowWaterMark = lowMark; aoqi@0: numberOfSamples = 0L; aoqi@0: initNumberOfSamples = numberOfSamples; aoqi@0: runningTotal = 0L; aoqi@0: initRunningTotal = runningTotal; aoqi@0: } aoqi@0: aoqi@0: public synchronized AverageRangeStatistic getStatistic() { aoqi@0: return as; aoqi@0: } aoqi@0: aoqi@0: public synchronized String toString() { aoqi@0: return super.toString() + NEWLINE + aoqi@0: "Current: " + getCurrent() + NEWLINE + aoqi@0: "LowWaterMark: " + getLowWaterMark() + NEWLINE + aoqi@0: "HighWaterMark: " + getHighWaterMark() + NEWLINE + aoqi@0: "Average:" + getAverage(); aoqi@0: } aoqi@0: aoqi@0: public synchronized Map getStaticAsMap() { aoqi@0: Map m = super.getStaticAsMap(); aoqi@0: m.put("current", getCurrent()); aoqi@0: m.put("lowwatermark", getLowWaterMark()); aoqi@0: m.put("highwatermark", getHighWaterMark()); aoqi@0: m.put("average", getAverage()); aoqi@0: return m; aoqi@0: } aoqi@0: aoqi@0: public synchronized void reset() { aoqi@0: super.reset(); aoqi@0: currentVal = initCurrentVal; aoqi@0: highWaterMark = initHighWaterMark; aoqi@0: lowWaterMark = initLowWaterMark; aoqi@0: numberOfSamples = initNumberOfSamples; aoqi@0: runningTotal = initRunningTotal; aoqi@0: sampleTime = -1L; aoqi@0: } aoqi@0: aoqi@0: public synchronized long getAverage() { aoqi@0: if(numberOfSamples == 0) { aoqi@0: return -1; aoqi@0: } else { aoqi@0: return runningTotal / numberOfSamples; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public synchronized long getCurrent() { aoqi@0: return currentVal; aoqi@0: } aoqi@0: aoqi@0: public synchronized void setCurrent(long curVal) { aoqi@0: currentVal = curVal; aoqi@0: lowWaterMark = (curVal >= lowWaterMark ? lowWaterMark : curVal); aoqi@0: highWaterMark = (curVal >= highWaterMark ? curVal : highWaterMark); aoqi@0: numberOfSamples++; aoqi@0: runningTotal += curVal; aoqi@0: sampleTime = System.currentTimeMillis(); aoqi@0: } aoqi@0: aoqi@0: public synchronized long getHighWaterMark() { aoqi@0: return highWaterMark; aoqi@0: } aoqi@0: aoqi@0: public synchronized long getLowWaterMark() { aoqi@0: return lowWaterMark; aoqi@0: } aoqi@0: aoqi@0: // todo: equals implementation aoqi@0: public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { aoqi@0: checkMethod(method); aoqi@0: aoqi@0: Object result; aoqi@0: try { aoqi@0: result = method.invoke(this, args); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: throw e.getTargetException(); aoqi@0: } catch (Exception e) { aoqi@0: throw new RuntimeException("unexpected invocation exception: " + aoqi@0: e.getMessage()); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: }