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 import com.sun.org.glassfish.external.statistics.RangeStatistic; 28 import java.util.concurrent.atomic.AtomicLong; 29 import java.util.Map; 30 import java.lang.reflect.*; 31 32 /** 33 * @author Sreenivas Munnangi 34 */ 35 public final class RangeStatisticImpl extends StatisticImpl 36 implements RangeStatistic, InvocationHandler { 37 38 private long currentVal = 0L; 39 private long highWaterMark = Long.MIN_VALUE; 40 private long lowWaterMark = Long.MAX_VALUE; 41 private final long initCurrentVal; 42 private final long initHighWaterMark; 43 private final long initLowWaterMark; 44 45 private final RangeStatistic rs = 46 (RangeStatistic) Proxy.newProxyInstance( 47 RangeStatistic.class.getClassLoader(), 48 new Class[] { RangeStatistic.class }, 49 this); 50 51 public RangeStatisticImpl(long curVal, long highMark, long lowMark, 52 String name, String unit, String desc, 53 long startTime, long sampleTime) { 54 super(name, unit, desc, startTime, sampleTime); 55 currentVal = curVal; 56 initCurrentVal = curVal; 57 highWaterMark = highMark; 58 initHighWaterMark = highMark; 59 lowWaterMark = lowMark; 60 initLowWaterMark = lowMark; 61 } 62 63 public synchronized RangeStatistic getStatistic() { 64 return rs; 65 } 66 67 public synchronized Map getStaticAsMap() { 68 Map m = super.getStaticAsMap(); 69 m.put("current", getCurrent()); 70 m.put("lowwatermark", getLowWaterMark()); 71 m.put("highwatermark", getHighWaterMark()); 72 return m; 73 } 74 75 public synchronized long getCurrent() { 76 return currentVal; 77 } 78 79 public synchronized void setCurrent(long curVal) { 80 currentVal = curVal; 81 lowWaterMark = (curVal >= lowWaterMark ? lowWaterMark : curVal); 82 highWaterMark = (curVal >= highWaterMark ? curVal : highWaterMark); 83 sampleTime = System.currentTimeMillis(); 84 } 85 86 /** 87 * Returns the highest value of this statistic, since measurement started. 88 */ 89 public synchronized long getHighWaterMark() { 90 return highWaterMark; 91 } 92 93 public synchronized void setHighWaterMark(long hwm) { 94 highWaterMark = hwm; 95 } 96 97 /** 98 * Returns the lowest value of this statistic, since measurement started. 99 */ 100 public synchronized long getLowWaterMark() { 101 return lowWaterMark; 102 } 103 104 public synchronized void setLowWaterMark(long lwm) { 105 lowWaterMark = lwm; 106 } 107 108 @Override 109 public synchronized void reset() { 110 super.reset(); 111 currentVal = initCurrentVal; 112 highWaterMark = initHighWaterMark; 113 lowWaterMark = initLowWaterMark; 114 sampleTime = -1L; 115 } 116 117 public synchronized String toString() { 118 return super.toString() + NEWLINE + 119 "Current: " + getCurrent() + NEWLINE + 120 "LowWaterMark: " + getLowWaterMark() + NEWLINE + 121 "HighWaterMark: " + getHighWaterMark(); 122 } 123 124 // todo: equals implementation 125 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { 126 Object result; 127 try { 128 result = m.invoke(this, args); 129 } catch (InvocationTargetException e) { 130 throw e.getTargetException(); 131 } catch (Exception e) { 132 throw new RuntimeException("unexpected invocation exception: " + 133 e.getMessage()); 134 } finally { 135 } 136 return result; 137 } 138 }