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